search.asciidoc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. [[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 either be provided using the
  6. <<query-dsl-queries,query Java API>> or
  7. the <<query-dsl-filters,filter Java API>>.
  8. The body of the search request is built using the
  9. `SearchSourceBuilder`. Here is an example:
  10. [source,java]
  11. --------------------------------------------------
  12. import org.elasticsearch.action.search.SearchResponse;
  13. import org.elasticsearch.action.search.SearchType;
  14. import org.elasticsearch.index.query.FilterBuilders.*;
  15. import org.elasticsearch.index.query.QueryBuilders.*;
  16. --------------------------------------------------
  17. [source,java]
  18. --------------------------------------------------
  19. SearchResponse response = client.prepareSearch("index1", "index2")
  20. .setTypes("type1", "type2")
  21. .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
  22. .setQuery(QueryBuilders.termQuery("multi", "test")) // Query
  23. .setPostFilter(FilterBuilders.rangeFilter("age").from(12).to(18)) // Filter
  24. .setFrom(0).setSize(60).setExplain(true)
  25. .execute()
  26. .actionGet();
  27. --------------------------------------------------
  28. Note that all parameters are optional. Here is the smallest search call
  29. you can write:
  30. [source,java]
  31. --------------------------------------------------
  32. // MatchAll on the whole cluster with all default options
  33. SearchResponse response = client.prepareSearch().execute().actionGet();
  34. --------------------------------------------------
  35. For more information on the search operation, check out the REST
  36. {ref}/search.html[search] docs.
  37. [[scrolling]]
  38. === Using scrolls in Java
  39. Read the {ref}/search-request-scroll.html[scroll documentation]
  40. first!
  41. [source,java]
  42. --------------------------------------------------
  43. import static org.elasticsearch.index.query.FilterBuilders.*;
  44. import static org.elasticsearch.index.query.QueryBuilders.*;
  45. QueryBuilder qb = termQuery("multi", "test");
  46. SearchResponse scrollResp = client.prepareSearch(test)
  47. .setSearchType(SearchType.SCAN)
  48. .setScroll(new TimeValue(60000))
  49. .setQuery(qb)
  50. .setSize(100).execute().actionGet(); //100 hits per shard will be returned for each scroll
  51. //Scroll until no hits are returned
  52. while (true) {
  53. for (SearchHit hit : scrollResp.getHits().getHits()) {
  54. //Handle the hit...
  55. }
  56. scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
  57. //Break condition: No hits are returned
  58. if (scrollResp.getHits().getHits().length == 0) {
  59. break;
  60. }
  61. }
  62. --------------------------------------------------
  63. [[msearch]]
  64. === MultiSearch API
  65. See {ref}/search-multi-search.html[MultiSearch API Query]
  66. documentation
  67. [source,java]
  68. --------------------------------------------------
  69. SearchRequestBuilder srb1 = node.client()
  70. .prepareSearch().setQuery(QueryBuilders.queryString("elasticsearch")).setSize(1);
  71. SearchRequestBuilder srb2 = node.client()
  72. .prepareSearch().setQuery(QueryBuilders.matchQuery("name", "kimchy")).setSize(1);
  73. MultiSearchResponse sr = node.client().prepareMultiSearch()
  74. .add(srb1)
  75. .add(srb2)
  76. .execute().actionGet();
  77. // You will get all individual responses from MultiSearchResponse#getResponses()
  78. long nbHits = 0;
  79. for (MultiSearchResponse.Item item : sr.getResponses()) {
  80. SearchResponse response = item.getResponse();
  81. nbHits += response.getHits().getTotalHits();
  82. }
  83. --------------------------------------------------
  84. [[java-search-aggs]]
  85. === Using Aggregations
  86. The following code shows how to add two aggregations within your search:
  87. [source,java]
  88. --------------------------------------------------
  89. SearchResponse sr = node.client().prepareSearch()
  90. .setQuery(QueryBuilders.matchAllQuery())
  91. .addAggregation(
  92. AggregationBuilders.terms("agg1").field("field")
  93. )
  94. .addAggregation(
  95. AggregationBuilders.dateHistogram("agg2")
  96. .field("birth")
  97. .interval(DateHistogram.Interval.YEAR)
  98. )
  99. .execute().actionGet();
  100. // Get your facet results
  101. Terms agg1 = sr.getAggregations().get("agg1");
  102. DateHistogram agg2 = sr.getAggregations().get("agg2");
  103. --------------------------------------------------
  104. See <<java-aggs,Aggregations Java API>>
  105. documentation for details.
  106. [[java-search-template]]
  107. === Using Search Templates
  108. See {ref}/search-template.html[Search Template] documentation
  109. Define your template parameters as a `Map<String,Object>`:
  110. [source,java]
  111. --------------------------------------------------
  112. Map<String, Object> template_params = new HashMap<>();
  113. template_params.put("param_gender", "male");
  114. --------------------------------------------------
  115. You can use your stored search templates in `config/scripts`.
  116. For example, if you have a file named `config/scripts/template_gender.mustache` containing:
  117. [source,js]
  118. --------------------------------------------------
  119. {
  120. "template" : {
  121. "query" : {
  122. "match" : {
  123. "gender" : "{{param_gender}}"
  124. }
  125. }
  126. }
  127. }
  128. --------------------------------------------------
  129. Execute it with:
  130. [source,java]
  131. --------------------------------------------------
  132. SearchResponse sr = client.prepareSearch()
  133. .setTemplateName("template_gender")
  134. .setTemplateType(ScriptService.ScriptType.FILE)
  135. .setTemplateParams(template_params)
  136. .get();
  137. --------------------------------------------------
  138. You can also store your template in a special index named `.scripts`:
  139. [source,java]
  140. --------------------------------------------------
  141. client.preparePutIndexedScript("mustache", "template_gender",
  142. "{\n" +
  143. " \"template\" : {\n" +
  144. " \"query\" : {\n" +
  145. " \"match\" : {\n" +
  146. " \"gender\" : \"{{param_gender}}\"\n" +
  147. " }\n" +
  148. " }\n" +
  149. " }\n" +
  150. "}").get();
  151. --------------------------------------------------
  152. To execute an indexed templates, use `ScriptService.ScriptType.INDEXED`:
  153. [source,java]
  154. --------------------------------------------------
  155. SearchResponse sr = client.prepareSearch()
  156. .setTemplateName("template_gender")
  157. .setTemplateType(ScriptService.ScriptType.INDEXED)
  158. .setTemplateParams(template_params)
  159. .get();
  160. --------------------------------------------------