Search API - MultiSearch API

优质
小牛编辑
128浏览
2023-12-01

MultiSearch API

multi search API 允许在同一API中执行多个搜索请求。它的端点(endpoint)是 _msearch 。

首先请看MultiSearch API Query 文档

  1. SearchRequestBuilder srb1 = client
  2. .prepareSearch().setQuery(QueryBuilders.queryStringQuery("elasticsearch")).setSize(1);
  3. SearchRequestBuilder srb2 = client
  4. .prepareSearch().setQuery(QueryBuilders.matchQuery("name", "kimchy")).setSize(1);
  5. MultiSearchResponse sr = client.prepareMultiSearch()
  6. .add(srb1)
  7. .add(srb2)
  8. .get();
  9. // You will get all individual responses from MultiSearchResponse#getResponses()
  10. long nbHits = 0;
  11. for (MultiSearchResponse.Item item : sr.getResponses()) {
  12. SearchResponse response = item.getResponse();
  13. nbHits += response.getHits().getTotalHits();
  14. }

实例