search.asciidoc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. while (true) {
  51. for (SearchHit hit : scrollResp.getHits().getHits()) {
  52. //Handle the hit...
  53. }
  54. scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
  55. //Break condition: No hits are returned
  56. if (scrollResp.getHits().getHits().length == 0) {
  57. break;
  58. }
  59. }
  60. --------------------------------------------------
  61. [[java-search-msearch]]
  62. === MultiSearch API
  63. See {ref}/search-multi-search.html[MultiSearch API Query]
  64. documentation
  65. [source,java]
  66. --------------------------------------------------
  67. SearchRequestBuilder srb1 = node.client()
  68. .prepareSearch().setQuery(QueryBuilders.queryStringQuery("elasticsearch")).setSize(1);
  69. SearchRequestBuilder srb2 = node.client()
  70. .prepareSearch().setQuery(QueryBuilders.matchQuery("name", "kimchy")).setSize(1);
  71. MultiSearchResponse sr = node.client().prepareMultiSearch()
  72. .add(srb1)
  73. .add(srb2)
  74. .execute().actionGet();
  75. // You will get all individual responses from MultiSearchResponse#getResponses()
  76. long nbHits = 0;
  77. for (MultiSearchResponse.Item item : sr.getResponses()) {
  78. SearchResponse response = item.getResponse();
  79. nbHits += response.getHits().getTotalHits();
  80. }
  81. --------------------------------------------------
  82. [[java-search-aggs]]
  83. === Using Aggregations
  84. The following code shows how to add two aggregations within your search:
  85. [source,java]
  86. --------------------------------------------------
  87. SearchResponse sr = node.client().prepareSearch()
  88. .setQuery(QueryBuilders.matchAllQuery())
  89. .addAggregation(
  90. AggregationBuilders.terms("agg1").field("field")
  91. )
  92. .addAggregation(
  93. AggregationBuilders.dateHistogram("agg2")
  94. .field("birth")
  95. .interval(DateHistogramInterval.YEAR)
  96. )
  97. .execute().actionGet();
  98. // Get your facet results
  99. Terms agg1 = sr.getAggregations().get("agg1");
  100. DateHistogram agg2 = sr.getAggregations().get("agg2");
  101. --------------------------------------------------
  102. See <<java-aggs,Aggregations Java API>>
  103. documentation for details.
  104. [[java-search-terminate-after]]
  105. === Terminate After
  106. The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early.
  107. If set, you will be able to check if the operation terminated early by asking for `isTerminatedEarly()` in the
  108. `SearchResponse` onject:
  109. [source,java]
  110. --------------------------------------------------
  111. SearchResponse sr = client.prepareSearch(INDEX)
  112. .setTerminateAfter(1000) <1>
  113. .get();
  114. if (sr.isTerminatedEarly()) {
  115. // We finished early
  116. }
  117. --------------------------------------------------
  118. <1> Finish after 1000 docs