search.asciidoc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. [[search]]
  2. == Search API
  3. The search API allows 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. .setFilter(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. link:{ref}/search.html[search] docs.
  37. === Using scrolls in Java
  38. Read the link:{ref}/search-request-scroll.html[scroll documentation]
  39. first!
  40. [source,java]
  41. --------------------------------------------------
  42. import static org.elasticsearch.index.query.FilterBuilders.*;
  43. import static org.elasticsearch.index.query.QueryBuilders.*;
  44. QueryBuilder qb = termQuery("multi", "test");
  45. SearchResponse scrollResp = client.prepareSearch(test)
  46. .setSearchType(SearchType.SCAN)
  47. .setScroll(new TimeValue(60000))
  48. .setQuery(qb)
  49. .setSize(100).execute().actionGet(); //100 hits per shard will be returned for each scroll
  50. //Scroll until no hits are returned
  51. while (true) {
  52. scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
  53. for (SearchHit hit : scrollResp.getHits()) {
  54. //Handle the hit...
  55. }
  56. //Break condition: No hits are returned
  57. if (scrollResp.hits().hits().length == 0) {
  58. break;
  59. }
  60. }
  61. --------------------------------------------------
  62. === Operation Threading
  63. The search API allows to set the threading model the operation will be
  64. performed when the actual execution of the API is performed on the same
  65. node (the API is executed on a shard that is allocated on the same
  66. server).
  67. There are three threading modes.The `NO_THREADS` mode means that the
  68. search operation will be executed on the calling thread. The
  69. `SINGLE_THREAD` mode means that the search operation will be executed on
  70. a single different thread for all local shards. The `THREAD_PER_SHARD`
  71. mode means that the search operation will be executed on a different
  72. thread for each local shard.
  73. The default mode is `SINGLE_THREAD`.
  74. === MultiSearch API
  75. See link:{ref}/search-multi-search.html[MultiSearch API Query]
  76. documentation
  77. [source,java]
  78. --------------------------------------------------
  79. SearchRequestBuilder srb1 = node.client()
  80. .prepareSearch().setQuery(QueryBuilders.queryString("elasticsearch")).setSize(1);
  81. SearchRequestBuilder srb2 = node.client()
  82. .prepareSearch().setQuery(QueryBuilders.matchQuery("name", "kimchy")).setSize(1);
  83. MultiSearchResponse sr = node.client().prepareMultiSearch()
  84. .add(srb1)
  85. .add(srb2)
  86. .execute().actionGet();
  87. // You will get all individual responses from MultiSearchResponse#responses()
  88. long nbHits = 0;
  89. for (MultiSearchResponse.Item item : sr.responses()) {
  90. SearchResponse response = item.response();
  91. nbHits += response.hits().totalHits();
  92. }
  93. --------------------------------------------------
  94. === Using Facets
  95. The following code shows how to add two facets within your search:
  96. [source,java]
  97. --------------------------------------------------
  98. SearchResponse sr = node.client().prepareSearch()
  99. .setQuery(QueryBuilders.matchAllQuery())
  100. .addFacet(FacetBuilders.termsFacet("f1").field("field"))
  101. .addFacet(FacetBuilders.dateHistogramFacet("f2").field("birth").interval("year"))
  102. .execute().actionGet();
  103. // Get your facet results
  104. TermsFacet f1 = (TermsFacet) sr.facets().facetsAsMap().get("f1");
  105. DateHistogramFacet f2 = (DateHistogramFacet) sr.facets().facetsAsMap().get("f2");
  106. --------------------------------------------------
  107. See <<facets,Facets Java API>>
  108. documentation for details.