search.asciidoc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. [[java-search]]
  2. == Search API
  3. The search API allows one to execute a search query and get back search hits
  4. that match the query. It can be executed across one or more indices and
  5. across one or more types. The query can provided using the <<java-query-dsl,query Java API>>.
  6. The body of the search request is built using the `SearchSourceBuilder`. Here is an example:
  7. [source,java]
  8. --------------------------------------------------
  9. import org.elasticsearch.action.search.SearchResponse;
  10. import org.elasticsearch.action.search.SearchType;
  11. import org.elasticsearch.index.query.QueryBuilders.*;
  12. --------------------------------------------------
  13. [source,java]
  14. --------------------------------------------------
  15. SearchResponse response = client.prepareSearch("index1", "index2")
  16. .setTypes("type1", "type2")
  17. .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
  18. .setQuery(QueryBuilders.termQuery("multi", "test")) // Query
  19. .setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18)) // Filter
  20. .setFrom(0).setSize(60).setExplain(true)
  21. .execute()
  22. .actionGet();
  23. --------------------------------------------------
  24. Note that all parameters are optional. Here is the smallest search call
  25. you can write:
  26. [source,java]
  27. --------------------------------------------------
  28. // MatchAll on the whole cluster with all default options
  29. SearchResponse response = client.prepareSearch().execute().actionGet();
  30. --------------------------------------------------
  31. NOTE: Although the Java API defines the additional search types QUERY_AND_FETCH and
  32. DFS_QUERY_AND_FETCH, these modes are internal optimizations and should not
  33. be specified explicitly by users of the API.
  34. For more information on the search operation, check out the REST
  35. {ref}/search.html[search] docs.
  36. [[java-search-scrolling]]
  37. === Using scrolls in Java
  38. Read the {ref}/search-request-scroll.html[scroll documentation]
  39. first!
  40. [source,java]
  41. --------------------------------------------------
  42. import static org.elasticsearch.index.query.QueryBuilders.*;
  43. QueryBuilder qb = termQuery("multi", "test");
  44. SearchResponse scrollResp = client.prepareSearch(test)
  45. .setSearchType(SearchType.SCAN)
  46. .setScroll(new TimeValue(60000))
  47. .setQuery(qb)
  48. .setSize(100).execute().actionGet(); //100 hits per shard will be returned for each scroll
  49. //Scroll until no hits are returned
  50. do {
  51. for (SearchHit hit : scrollResp.getHits().getHits()) {
  52. //Handle the hit...
  53. }
  54. scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
  55. } while(scrollResp.getHits().getHits().length != 0); // Zero hits mark the end of the scroll and the while loop.
  56. --------------------------------------------------
  57. [[java-search-msearch]]
  58. === MultiSearch API
  59. See {ref}/search-multi-search.html[MultiSearch API Query]
  60. documentation
  61. [source,java]
  62. --------------------------------------------------
  63. SearchRequestBuilder srb1 = node.client()
  64. .prepareSearch().setQuery(QueryBuilders.queryStringQuery("elasticsearch")).setSize(1);
  65. SearchRequestBuilder srb2 = node.client()
  66. .prepareSearch().setQuery(QueryBuilders.matchQuery("name", "kimchy")).setSize(1);
  67. MultiSearchResponse sr = node.client().prepareMultiSearch()
  68. .add(srb1)
  69. .add(srb2)
  70. .execute().actionGet();
  71. // You will get all individual responses from MultiSearchResponse#getResponses()
  72. long nbHits = 0;
  73. for (MultiSearchResponse.Item item : sr.getResponses()) {
  74. SearchResponse response = item.getResponse();
  75. nbHits += response.getHits().getTotalHits();
  76. }
  77. --------------------------------------------------
  78. [[java-search-aggs]]
  79. === Using Aggregations
  80. The following code shows how to add two aggregations within your search:
  81. [source,java]
  82. --------------------------------------------------
  83. SearchResponse sr = node.client().prepareSearch()
  84. .setQuery(QueryBuilders.matchAllQuery())
  85. .addAggregation(
  86. AggregationBuilders.terms("agg1").field("field")
  87. )
  88. .addAggregation(
  89. AggregationBuilders.dateHistogram("agg2")
  90. .field("birth")
  91. .interval(DateHistogramInterval.YEAR)
  92. )
  93. .execute().actionGet();
  94. // Get your facet results
  95. Terms agg1 = sr.getAggregations().get("agg1");
  96. DateHistogram agg2 = sr.getAggregations().get("agg2");
  97. --------------------------------------------------
  98. See <<java-aggs,Aggregations Java API>>
  99. documentation for details.
  100. [[java-search-terminate-after]]
  101. === Terminate After
  102. The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early.
  103. If set, you will be able to check if the operation terminated early by asking for `isTerminatedEarly()` in the
  104. `SearchResponse` onject:
  105. [source,java]
  106. --------------------------------------------------
  107. SearchResponse sr = client.prepareSearch(INDEX)
  108. .setTerminateAfter(1000) <1>
  109. .get();
  110. if (sr.isTerminatedEarly()) {
  111. // We finished early
  112. }
  113. --------------------------------------------------
  114. <1> Finish after 1000 docs