search.asciidoc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. {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. scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
  54. for (SearchHit hit : scrollResp.getHits()) {
  55. //Handle the hit...
  56. }
  57. //Break condition: No hits are returned
  58. if (scrollResp.hits().hits().length == 0) {
  59. break;
  60. }
  61. }
  62. --------------------------------------------------
  63. === Operation Threading
  64. The search API allows to set the threading model the operation will be
  65. performed when the actual execution of the API is performed on the same
  66. node (the API is executed on a shard that is allocated on the same
  67. server).
  68. There are three threading modes.The `NO_THREADS` mode means that the
  69. search operation will be executed on the calling thread. The
  70. `SINGLE_THREAD` mode means that the search operation will be executed on
  71. a single different thread for all local shards. The `THREAD_PER_SHARD`
  72. mode means that the search operation will be executed on a different
  73. thread for each local shard.
  74. The default mode is `THREAD_PER_SHARD`.
  75. [[msearch]]
  76. === MultiSearch API
  77. See {ref}/search-multi-search.html[MultiSearch API Query]
  78. documentation
  79. [source,java]
  80. --------------------------------------------------
  81. SearchRequestBuilder srb1 = node.client()
  82. .prepareSearch().setQuery(QueryBuilders.queryString("elasticsearch")).setSize(1);
  83. SearchRequestBuilder srb2 = node.client()
  84. .prepareSearch().setQuery(QueryBuilders.matchQuery("name", "kimchy")).setSize(1);
  85. MultiSearchResponse sr = node.client().prepareMultiSearch()
  86. .add(srb1)
  87. .add(srb2)
  88. .execute().actionGet();
  89. // You will get all individual responses from MultiSearchResponse#responses()
  90. long nbHits = 0;
  91. for (MultiSearchResponse.Item item : sr.responses()) {
  92. SearchResponse response = item.response();
  93. nbHits += response.hits().totalHits();
  94. }
  95. --------------------------------------------------
  96. [[facets]]
  97. === Using Facets
  98. The following code shows how to add two facets within your search:
  99. [source,java]
  100. --------------------------------------------------
  101. SearchResponse sr = node.client().prepareSearch()
  102. .setQuery(QueryBuilders.matchAllQuery())
  103. .addFacet(FacetBuilders.termsFacet("f1").field("field"))
  104. .addFacet(FacetBuilders.dateHistogramFacet("f2").field("birth").interval("year"))
  105. .execute().actionGet();
  106. // Get your facet results
  107. TermsFacet f1 = (TermsFacet) sr.facets().facetsAsMap().get("f1");
  108. DateHistogramFacet f2 = (DateHistogramFacet) sr.facets().facetsAsMap().get("f2");
  109. --------------------------------------------------
  110. See <<facets,Facets Java API>>
  111. documentation for details.