Elasticsearch Java REST高级客户端 MultiSearch Api
程序员文章站
2022-07-05 14:19:31
...
Elasticsearch Java REST高级客户端 MultiSearch Api
使用 MultiSearch Api 在一个http请求中并行执行多个Search请求。
public static void multiSearch() {
var client = getClient();
MultiSearchRequest msRequest = new MultiSearchRequest();
SearchRequest request1 = new SearchRequest("movies");
SearchSourceBuilder builder1 = new SearchSourceBuilder();
builder1.query(QueryBuilders.matchQuery("title", "you"));
request1.source(builder1);
msRequest.add(request1);
SearchRequest request2 = new SearchRequest("movies");
SearchSourceBuilder builder2 = new SearchSourceBuilder();
builder2.query(QueryBuilders.matchQuery("title", "life"));
request2.source(builder2);
msRequest.add(request2);
try {
MultiSearchResponse responses = client.msearch(msRequest, RequestOptions.DEFAULT);
Arrays.asList(responses.getResponses()).stream()
.map(item -> item.getResponse())
.map(response -> response.getHits())
.filter(hit -> hit != null)
.forEach(hit -> hit.forEach(System.out::println));
} catch (IOException e) {
e.printStackTrace();
}finally {
close(client);
}
}
/**
* 关闭客户端
*
* @param client
*/
private static void close(RestHighLevelClient client) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取客户端
*
* @return
*/
private static RestHighLevelClient getClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("elk-node01", 9200, "http"), new HttpHost("elk-node02", 9200, "http"),
new HttpHost("elk-node03", 9200, "http")));
return client;
}
上一篇: 愉快买买买:我劝你别买30系显卡除非
下一篇: 都秃了
推荐阅读
-
Elasticsearch Java Rest Client API 整理总结 (一)
-
Elasticsearch Java Rest Client API 整理总结 (二) —— SearchAPI
-
Elasticsearch Java Rest Client API 整理总结 (三)——Building Queries
-
Elasticsearch 5.X Java API 创建客户端
-
Elasticsearch Java REST高级客户端 MultiSearch Api
-
elasticsearch java客户端api使用(一)
-
elasticsearch Java API 客户端(Node Client)
-
Elasticsearch Java API 的使用(1)—创建客户端
-
ElasticSearch的java高级客户端demo
-
Elasticsearch Java高级客户端