ElasticSearch High Level REST API(4)多搜索
Multi-Search多搜索请求
Multi-Search
可同时添加多个search搜索请求,并行地在一个http请求中执行多个搜索请求,相较多次单请求查询可提升查询效率。ES客户端通过mget方法实现多请求搜索:
@GetMapping("multiSearch")
public void multiSearch() {
MultiSearchRequest request = new MultiSearchRequest();
SearchRequest firstRequest = new SearchRequest("appblog");
SearchSourceBuilder firstBuilder = new SearchSourceBuilder();
firstBuilder.query(QueryBuilders.matchQuery("loglvl", "ERROR"));
firstBuilder.sort("@timestamp", SortOrder.DESC);
firstBuilder.from(0);
firstBuilder.size(5);
firstRequest.source(firstBuilder);
request.add(firstRequest);
SearchRequest secondRequest = new SearchRequest();
SearchSourceBuilder secondBuild = new SearchSourceBuilder();
secondBuild.query(QueryBuilders.matchQuery("name", "Joe.Ye"));
secondRequest.source(secondBuild);
request.add(secondRequest);
try {
MultiSearchResponse multiResponse = client.msearch(request, RequestOptions.DEFAULT);
MultiSearchResponse.Item firstItem = multiResponse.getResponses()[0];
if (firstItem.getFailure() != null) {
System.out.println("First search request fail!");
}
SearchResponse firstResponse = firstItem.getResponse();
for (SearchHit documentFields : firstResponse.getHits().getHits()) {
System.out.println("First search response: " + documentFields.getSourceAsString());
}
MultiSearchResponse.Item secondItem = multiResponse.getResponses()[1];
if (secondItem.getFailure() != null) {
System.out.println("Second search request fail!");
}
SearchResponse secondResponse = secondItem.getResponse();
for (SearchHit documentFields : secondResponse.getHits().getHits()) {
System.out.println("Second search response: " + documentFields.getSourceAsString());
}
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/19/elasticsearch-high-level-rest-api-multiple-search/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
THE END
0
二维码
打赏
海报
ElasticSearch High Level REST API(4)多搜索
Multi-Search多搜索请求
Multi-Search可同时添加多个search搜索请求,并行地在一个http请求中执行多个搜索请求,相较多次单请求查询可提升查询效率。ES客户端……
文章目录
关闭
共有 0 条评论