es在java环境中使用
Created At : 2022-09-01 22:11
Views 👀 :
一、java api 实现索引管理 1.1 创建索引 1.1.1 获取客户端 RestClientBuilder restClientBuilder = RestClient . builder(new HttpHost("113.31.112.15" , 9200, "http" ) ).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder ) { CredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider() ; basicCredentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic" , "zCAgwD2wXL7uDMFnVsNQ" ) ); return httpAsyncClientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider ) ; } }); RestHighLevelClient client = new RestHighLevelClient(restClientBuilder ) ;
1.1.2 setting final CreateIndexRequest my_index = new CreateIndexRequest ("my_index" ); my_index.settings (Settings.builder () .put ("number_of_shards" , "1" ) .put ("number_of_replicas" , "1" ) .build ());
1.1.3 mapping my_index.mapping("json" , XContentType.JSON); Map<String, Object> f1 = new HashMap<>() ; Map<String, Object> f2 = new HashMap<>() ; f1.put("type" , "text" ); f2.put("type" , "text" ); Map<String, Object> properties = new HashMap<>() ; properties.put("field1" , f1); properties.put("field2" , f2); Map<String, Object> mapping = new HashMap<>() ; mapping.put("properites" , properties); my_index.mapping(mapping); XContentBuilder builder = XContentFactory . jsonBuilder() ; builder.startObject() ; { builder.startObject("properties" ) ; { builder.startObject("field1" ) ; { builder.field("type" , "text" ); } builder.end Object() ; builder.startObject("field2" ) ; { builder.field("type" , "text" ); } builder.end Object() ; } builder.end Object() ; } builder.end Object() ; my_index.mapping(builder);
1.1.4 别名 my_index.alias (new Alias ("hello"));
1.1.5 其他配置 my_index.setTimeout(TimeValue.timeValueSeconds (10) ); my_index.setMasterTimeout(TimeValue.timeValueSeconds (5) ); my_index.waitForActiveShards(ActiveShardCount.from (1) );
1.1.6 结果 CreateIndexResponse createIndexResponse = client.indices ().create (my_index, RequestOptions.DEFAULT );final boolean acknowledged = createIndexResponse.isAcknowledged ();final boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged ();
1.1.7 异步执行 ActionListener<CreateIndexResponse> actionListener = new ActionListener<CreateIndexResponse>() { @Override public void onResponse (CreateIndexResponse createIndexResponse ) { } @Override public void onFailure (Exception e ) { } }; client.indices().createAsync(my_index, RequestOptions.DEFAULT, actionListener);
1.2 删除索引 final DeleteIndexRequest my_index1 = new DeleteIndexRequest ("my_index" );final AcknowledgedResponse delete = client.indices ().delete (my_index1, RequestOptions.DEFAULT );final boolean acknowledged1 = delete .isAcknowledged ();
1.3 查看索引 final GetIndexRequest my_index2 = new GetIndexRequest ("my_index" ); my_index2.local (true ); my_index2.humanReadable (true ); my_index2.includeDefaults (false );final boolean exists = client.indices ().exists (my_index2, RequestOptions.DEFAULT );
1.4 关闭索引 final CloseIndexRequest my_index3 = new CloseIndexRequest ("my_index" );final CloseIndexResponse close = client.indices ().close (my_index3, RequestOptions.DEFAULT );final boolean acknowledged2 = close.isAcknowledged ();
1.5 开启索引 final OpenIndexRequest my_index4 = new OpenIndexRequest ("my_index" );final OpenIndexResponse open = client.indices ().open (my_index4, RequestOptions.DEFAULT );final boolean acknowledged3 = open.isAcknowledged ();
二、搜索语法入门 2.1 query string searc 无条件搜索
took: 花费多长时间
time_out:是否超时
_shards:
total:总共分片数
successful:成功分片数
skipped:跳过分片数
failed:失败分片数
total:查询总数
max_score:就是docuemnt对于一个search的相关度的匹配分数,越相关,就越匹配,分数就越高。
2.2 传参 与http请求传参类似GET book/_search?q =name:java&sort=price:desc
类比select * from book where name like '%java%' order by price desc
2.3 图解timeout机制 指定每个shard只能在给定的时间内查询数据,能有几条就返回几条,返回给客户端,保住了业务。
请求设置GET book/_search?timeout =10ms
全局设置 配置文件中设置search.default_search_timeout: 100ms
,默认不超时。
三、multi-index多索引搜索 3.1 搜索模式 /_search 查询索引下所有数据/index1/ _search 查询指定索引下所有数据/index1,index2/ _search 查询两个索引下的数据/index*/ _search 匹配规则下的所有索引
应用场景:生产环境log可以按照日志分开。
3.2 图解简单的搜索原理 四、分页搜索 4.1 分页搜索的语法 sql : select * from book limit 1 ,5 size, from
例子GET /book/_search?size =10
五、query string基础语法 六、query DSL入门 七、Fiter 八、定位错误语法和定制排序规则 九、Text字段排序问题 十一、java api实现搜索 十二、tfidf算法 十三、Doc value 十四、搜索参数小结 十五、聚合查询 十六、es7 sql新特性