search.asciidoc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. --
  2. :api: search
  3. :request: SearchRequest
  4. :response: SearchResponse
  5. --
  6. [id="{upid}-{api}"]
  7. === Search API
  8. [id="{upid}-{api}-request"]
  9. ==== Search Request
  10. The +{request}+ is used for any operation that has to do with searching
  11. documents, aggregations, suggestions and also offers ways of requesting
  12. highlighting on the resulting documents.
  13. In its most basic form, we can add a query to the request:
  14. ["source","java",subs="attributes,callouts,macros"]
  15. --------------------------------------------------
  16. include-tagged::{doc-tests-file}[{api}-request-basic]
  17. --------------------------------------------------
  18. <1> Creates the `SeachRequest`. Without arguments this runs against all indices.
  19. <2> Most search parameters are added to the `SearchSourceBuilder`. It offers setters for everything that goes into the search request body.
  20. <3> Add a `match_all` query to the `SearchSourceBuilder`.
  21. <4> Add the `SearchSourceBuilder` to the `SeachRequest`.
  22. [id="{upid}-{api}-request-optional"]
  23. ===== Optional arguments
  24. Let's first look at some of the optional arguments of a +{request}+:
  25. ["source","java",subs="attributes,callouts,macros"]
  26. --------------------------------------------------
  27. include-tagged::{doc-tests-file}[{api}-request-indices]
  28. --------------------------------------------------
  29. <1> Restricts the request to an index
  30. There are a couple of other interesting optional parameters:
  31. ["source","java",subs="attributes,callouts,macros"]
  32. --------------------------------------------------
  33. include-tagged::{doc-tests-file}[{api}-request-routing]
  34. --------------------------------------------------
  35. <1> Set a routing parameter
  36. ["source","java",subs="attributes,callouts,macros"]
  37. --------------------------------------------------
  38. include-tagged::{doc-tests-file}[{api}-request-indicesOptions]
  39. --------------------------------------------------
  40. <1> Setting `IndicesOptions` controls how unavailable indices are resolved and
  41. how wildcard expressions are expanded
  42. ["source","java",subs="attributes,callouts,macros"]
  43. --------------------------------------------------
  44. include-tagged::{doc-tests-file}[{api}-request-preference]
  45. --------------------------------------------------
  46. <1> Use the preference parameter e.g. to execute the search to prefer local
  47. shards. The default is to randomize across shards.
  48. ===== Using the SearchSourceBuilder
  49. Most options controlling the search behavior can be set on the
  50. `SearchSourceBuilder`,
  51. which contains more or less the equivalent of the options in the search request
  52. body of the Rest API.
  53. Here are a few examples of some common options:
  54. ["source","java",subs="attributes,callouts,macros"]
  55. --------------------------------------------------
  56. include-tagged::{doc-tests-file}[{api}-source-basics]
  57. --------------------------------------------------
  58. <1> Create a `SearchSourceBuilder` with default options.
  59. <2> Set the query. Can be any type of `QueryBuilder`
  60. <3> Set the `from` option that determines the result index to start searching
  61. from. Defaults to 0.
  62. <4> Set the `size` option that determines the number of search hits to return.
  63. Defaults to 10.
  64. <5> Set an optional timeout that controls how long the search is allowed to
  65. take.
  66. After this, the `SearchSourceBuilder` only needs to be added to the
  67. +{request}+:
  68. ["source","java",subs="attributes,callouts,macros"]
  69. --------------------------------------------------
  70. include-tagged::{doc-tests-file}[{api}-source-setter]
  71. --------------------------------------------------
  72. [id="{upid}-{api}-request-building-queries"]
  73. ===== Building queries
  74. Search queries are created using `QueryBuilder` objects. A `QueryBuilder` exists
  75. for every search query type supported by Elasticsearch's {ref}/query-dsl.html[Query DSL].
  76. A `QueryBuilder` can be created using its constructor:
  77. ["source","java",subs="attributes,callouts,macros"]
  78. --------------------------------------------------
  79. include-tagged::{doc-tests-file}[{api}-query-builder-ctor]
  80. --------------------------------------------------
  81. <1> Create a full text {ref}/query-dsl-match-query.html[Match Query] that matches
  82. the text "kimchy" over the field "user".
  83. Once created, the `QueryBuilder` object provides methods to configure the options
  84. of the search query it creates:
  85. ["source","java",subs="attributes,callouts,macros"]
  86. --------------------------------------------------
  87. include-tagged::{doc-tests-file}[{api}-query-builder-options]
  88. --------------------------------------------------
  89. <1> Enable fuzzy matching on the match query
  90. <2> Set the prefix length option on the match query
  91. <3> Set the max expansion options to control the fuzzy
  92. process of the query
  93. `QueryBuilder` objects can also be created using the `QueryBuilders` utility class.
  94. This class provides helper methods that can be used to create `QueryBuilder` objects
  95. using a fluent programming style:
  96. ["source","java",subs="attributes,callouts,macros"]
  97. --------------------------------------------------
  98. include-tagged::{doc-tests-file}[{api}-query-builders]
  99. --------------------------------------------------
  100. Whatever the method used to create it, the `QueryBuilder` object must be added
  101. to the `SearchSourceBuilder` as follows:
  102. ["source","java",subs="attributes,callouts,macros"]
  103. --------------------------------------------------
  104. include-tagged::{doc-tests-file}[{api}-query-setter]
  105. --------------------------------------------------
  106. The <<{upid}-query-builders, Building Queries>> page gives a list of all available search queries with
  107. their corresponding `QueryBuilder` objects and `QueryBuilders` helper methods.
  108. ===== Specifying Sorting
  109. The `SearchSourceBuilder` allows to add one or more `SortBuilder` instances. There are four special implementations (Field-, Score-, GeoDistance- and ScriptSortBuilder).
  110. ["source","java",subs="attributes,callouts,macros"]
  111. --------------------------------------------------
  112. include-tagged::{doc-tests-file}[{api}-source-sorting]
  113. --------------------------------------------------
  114. <1> Sort descending by `_score` (the default)
  115. <2> Also sort ascending by `_id` field
  116. ===== Source filtering
  117. By default, search requests return the contents of the document `_source` but like in the Rest API you can overwrite this behavior. For example, you can turn off `_source` retrieval completely:
  118. ["source","java",subs="attributes,callouts,macros"]
  119. --------------------------------------------------
  120. include-tagged::{doc-tests-file}[{api}-source-filtering-off]
  121. --------------------------------------------------
  122. The method also accepts an array of one or more wildcard patterns to control which fields get included or excluded in a more fine grained way:
  123. ["source","java",subs="attributes,callouts,macros"]
  124. --------------------------------------------------
  125. include-tagged::{doc-tests-file}[{api}-source-filtering-includes]
  126. --------------------------------------------------
  127. [id="{upid}-{api}-request-highlighting"]
  128. ===== Requesting Highlighting
  129. Highlighting search results can be achieved by setting a `HighlightBuilder` on the
  130. `SearchSourceBuilder`. Different highlighting behaviour can be defined for each
  131. fields by adding one or more `HighlightBuilder.Field` instances to a `HighlightBuilder`.
  132. ["source","java",subs="attributes,callouts,macros"]
  133. --------------------------------------------------
  134. include-tagged::{doc-tests-file}[{api}-request-highlighting]
  135. --------------------------------------------------
  136. <1> Creates a new `HighlightBuilder`
  137. <2> Create a field highlighter for the `title` field
  138. <3> Set the field highlighter type
  139. <4> Add the field highlighter to the highlight builder
  140. There are many options which are explained in detail in the Rest API documentation. The Rest
  141. API parameters (e.g. `pre_tags`) are usually changed by
  142. setters with a similar name (e.g. `#preTags(String ...)`).
  143. Highlighted text fragments can <<{upid}-{api}-response-highlighting,later be retrieved>> from the +{response}+.
  144. [id="{upid}-{api}-request-building-aggs"]
  145. ===== Requesting Aggregations
  146. Aggregations can be added to the search by first creating the appropriate
  147. `AggregationBuilder` and then setting it on the `SearchSourceBuilder`. In the
  148. following example we create a `terms` aggregation on company names with a
  149. sub-aggregation on the average age of employees in the company:
  150. ["source","java",subs="attributes,callouts,macros"]
  151. --------------------------------------------------
  152. include-tagged::{doc-tests-file}[{api}-request-aggregations]
  153. --------------------------------------------------
  154. The <<{upid}-aggregation-builders, Building Aggregations>> page gives a list of all available aggregations with
  155. their corresponding `AggregationBuilder` objects and `AggregationBuilders` helper methods.
  156. We will later see how to <<{upid}-{api}-response-aggs,access aggregations>> in the +{response}+.
  157. ===== Requesting Suggestions
  158. To add Suggestions to the search request, use one of the `SuggestionBuilder` implementations
  159. that are easily accessible from the `SuggestBuilders` factory class. Suggestion builders
  160. need to be added to the top level `SuggestBuilder`, which itself can be set on the `SearchSourceBuilder`.
  161. ["source","java",subs="attributes,callouts,macros"]
  162. --------------------------------------------------
  163. include-tagged::{doc-tests-file}[{api}-request-suggestion]
  164. --------------------------------------------------
  165. <1> Creates a new `TermSuggestionBuilder` for the `user` field and
  166. the text `kmichy`
  167. <2> Adds the suggestion builder and names it `suggest_user`
  168. We will later see how to <<{upid}-{api}-response-suggestions,retrieve suggestions>> from the
  169. +{response}+.
  170. ===== Profiling Queries and Aggregations
  171. The {ref}/search-profile.html[Profile API] can be used to profile the execution of queries and aggregations for
  172. a specific search request. in order to use it, the profile flag must be set to true on the `SearchSourceBuilder`:
  173. ["source","java",subs="attributes,callouts,macros"]
  174. --------------------------------------------------
  175. include-tagged::{doc-tests-file}[{api}-request-profiling]
  176. --------------------------------------------------
  177. Once the +{request}+ is executed the corresponding +{response}+ will
  178. <<{upid}-{api}-response-profile,contain the profiling results>>.
  179. include::../execution.asciidoc[]
  180. [id="{upid}-{api}-response"]
  181. ==== {response}
  182. The +{response}+ that is returned by executing the search provides details
  183. about the search execution itself as well as access to the documents returned.
  184. First, there is useful information about the request execution itself, like the
  185. HTTP status code, execution time or whether the request terminated early or timed
  186. out:
  187. ["source","java",subs="attributes,callouts,macros"]
  188. --------------------------------------------------
  189. include-tagged::{doc-tests-file}[{api}-response-1]
  190. --------------------------------------------------
  191. Second, the response also provides information about the execution on the
  192. shard level by offering statistics about the total number of shards that were
  193. affected by the search, and the successful vs. unsuccessful shards. Possible
  194. failures can also be handled by iterating over an array off
  195. `ShardSearchFailures` like in the following example:
  196. ["source","java",subs="attributes,callouts,macros"]
  197. --------------------------------------------------
  198. include-tagged::{doc-tests-file}[{api}-response-2]
  199. --------------------------------------------------
  200. [id="{upid}-{api}-response-search-hits"]
  201. ===== Retrieving SearchHits
  202. To get access to the returned documents, we need to first get the `SearchHits`
  203. contained in the response:
  204. ["source","java",subs="attributes,callouts,macros"]
  205. --------------------------------------------------
  206. include-tagged::{doc-tests-file}[{api}-hits-get]
  207. --------------------------------------------------
  208. The `SearchHits` provides global information about all hits, like total number
  209. of hits or the maximum score:
  210. ["source","java",subs="attributes,callouts,macros"]
  211. --------------------------------------------------
  212. include-tagged::{doc-tests-file}[{api}-hits-info]
  213. --------------------------------------------------
  214. Nested inside the `SearchHits` are the individual search results that can
  215. be iterated over:
  216. ["source","java",subs="attributes,callouts,macros"]
  217. --------------------------------------------------
  218. include-tagged::{doc-tests-file}[{api}-hits-singleHit]
  219. --------------------------------------------------
  220. The `SearchHit` provides access to basic information like index, document ID
  221. and score of each search hit:
  222. ["source","java",subs="attributes,callouts,macros"]
  223. --------------------------------------------------
  224. include-tagged::{doc-tests-file}[{api}-hits-singleHit-properties]
  225. --------------------------------------------------
  226. Furthermore, it lets you get back the document source, either as a simple
  227. JSON-String or as a map of key/value pairs. In this map, regular fields
  228. are keyed by the field name and contain the field value. Multi-valued fields are
  229. returned as lists of objects, nested objects as another key/value map. These
  230. cases need to be cast accordingly:
  231. ["source","java",subs="attributes,callouts,macros"]
  232. --------------------------------------------------
  233. include-tagged::{doc-tests-file}[{api}-hits-singleHit-source]
  234. --------------------------------------------------
  235. [id="{upid}-{api}-response-highlighting"]
  236. ===== Retrieving Highlighting
  237. If <<{upid}-{api}-request-highlighting,requested>>, highlighted text fragments can be retrieved from each `SearchHit` in the result. The hit object offers
  238. access to a map of field names to `HighlightField` instances, each of which contains one
  239. or many highlighted text fragments:
  240. ["source","java",subs="attributes,callouts,macros"]
  241. --------------------------------------------------
  242. include-tagged::{doc-tests-file}[{api}-request-highlighting-get]
  243. --------------------------------------------------
  244. <1> Get the highlighting for the `title` field
  245. <2> Get one or many fragments containing the highlighted field content
  246. [id="{upid}-{api}-response-aggs"]
  247. ===== Retrieving Aggregations
  248. Aggregations can be retrieved from the +{response}+ by first getting the
  249. root of the aggregation tree, the `Aggregations` object, and then getting the
  250. aggregation by name.
  251. ["source","java",subs="attributes,callouts,macros"]
  252. --------------------------------------------------
  253. include-tagged::{doc-tests-file}[{api}-request-aggregations-get]
  254. --------------------------------------------------
  255. <1> Get the `by_company` terms aggregation
  256. <2> Get the buckets that is keyed with `Elastic`
  257. <3> Get the `average_age` sub-aggregation from that bucket
  258. Note that if you access aggregations by name, you need to specify the
  259. aggregation interface according to the type of aggregation you requested,
  260. otherwise a `ClassCastException` will be thrown:
  261. ["source","java",subs="attributes,callouts,macros"]
  262. --------------------------------------------------
  263. include-tagged::{doc-tests-file}[search-request-aggregations-get-wrongCast]
  264. --------------------------------------------------
  265. <1> This will throw an exception because "by_company" is a `terms` aggregation
  266. but we try to retrieve it as a `range` aggregation
  267. It is also possible to access all aggregations as a map that is keyed by the
  268. aggregation name. In this case, the cast to the proper aggregation interface
  269. needs to happen explicitly:
  270. ["source","java",subs="attributes,callouts,macros"]
  271. --------------------------------------------------
  272. include-tagged::{doc-tests-file}[{api}-request-aggregations-asMap]
  273. --------------------------------------------------
  274. There are also getters that return all top level aggregations as a list:
  275. ["source","java",subs="attributes,callouts,macros"]
  276. --------------------------------------------------
  277. include-tagged::{doc-tests-file}[{api}-request-aggregations-asList]
  278. --------------------------------------------------
  279. And last but not least you can iterate over all aggregations and then e.g.
  280. decide how to further process them based on their type:
  281. ["source","java",subs="attributes,callouts,macros"]
  282. --------------------------------------------------
  283. include-tagged::{doc-tests-file}[{api}-request-aggregations-iterator]
  284. --------------------------------------------------
  285. [id="{upid}-{api}-response-suggestions"]
  286. ===== Retrieving Suggestions
  287. To get back the suggestions from a +{response}+, use the `Suggest` object as an entry point and then retrieve the nested suggestion objects:
  288. ["source","java",subs="attributes,callouts,macros"]
  289. --------------------------------------------------
  290. include-tagged::{doc-tests-file}[{api}-request-suggestion-get]
  291. --------------------------------------------------
  292. <1> Use the `Suggest` class to access suggestions
  293. <2> Suggestions can be retrieved by name. You need to assign them to the correct
  294. type of Suggestion class (here `TermSuggestion`), otherwise a `ClassCastException` is thrown
  295. <3> Iterate over the suggestion entries
  296. <4> Iterate over the options in one entry
  297. [id="{upid}-{api}-response-profile"]
  298. ===== Retrieving Profiling Results
  299. Profiling results are retrieved from a +{response}+ using the `getProfileResults()` method. This
  300. method returns a `Map` containing a `ProfileShardResult` object for every shard involved in the
  301. +{request}+ execution. `ProfileShardResult` are stored in the `Map` using a key that uniquely
  302. identifies the shard the profile result corresponds to.
  303. Here is a sample code that shows how to iterate over all the profiling results of every shard:
  304. ["source","java",subs="attributes,callouts,macros"]
  305. --------------------------------------------------
  306. include-tagged::{doc-tests-file}[{api}-request-profiling-get]
  307. --------------------------------------------------
  308. <1> Retrieve the `Map` of `ProfileShardResult` from the +{response}+
  309. <2> Profiling results can be retrieved by shard's key if the key is known, otherwise it might be simpler
  310. to iterate over all the profiling results
  311. <3> Retrieve the key that identifies which shard the `ProfileShardResult` belongs to
  312. <4> Retrieve the `ProfileShardResult` for the given shard
  313. The `ProfileShardResult` object itself contains one or more query profile results, one for each query
  314. executed against the underlying Lucene index:
  315. ["source","java",subs="attributes,callouts,macros"]
  316. --------------------------------------------------
  317. include-tagged::{doc-tests-file}[{api}-request-profiling-queries]
  318. --------------------------------------------------
  319. <1> Retrieve the list of `QueryProfileShardResult`
  320. <2> Iterate over each `QueryProfileShardResult`
  321. Each `QueryProfileShardResult` gives access to the detailed query tree execution, returned as a list of
  322. `ProfileResult` objects:
  323. ["source","java",subs="attributes,callouts,macros"]
  324. --------------------------------------------------
  325. include-tagged::{doc-tests-file}[{api}-request-profiling-queries-results]
  326. --------------------------------------------------
  327. <1> Iterate over the profile results
  328. <2> Retrieve the name of the Lucene query
  329. <3> Retrieve the time in millis spent executing the Lucene query
  330. <4> Retrieve the profile results for the sub-queries (if any)
  331. The Rest API documentation contains more information about {ref}/_profiling_queries.html[Profiling Queries] with
  332. a description of the {ref}/_profiling_queries.html#_literal_query_literal_section[query profiling information]
  333. The `QueryProfileShardResult` also gives access to the profiling information for the Lucene collectors:
  334. ["source","java",subs="attributes,callouts,macros"]
  335. --------------------------------------------------
  336. include-tagged::{doc-tests-file}[{api}-request-profiling-queries-collectors]
  337. --------------------------------------------------
  338. <1> Retrieve the profiling result of the Lucene collector
  339. <2> Retrieve the name of the Lucene collector
  340. <3> Retrieve the time in millis spent executing the Lucene collector
  341. <4> Retrieve the profile results for the sub-collectors (if any)
  342. The Rest API documentation contains more information about profiling information
  343. {ref}/_profiling_queries.html#_literal_collectors_literal_section[for Lucene collectors].
  344. In a very similar manner to the query tree execution, the `QueryProfileShardResult` objects gives access
  345. to the detailed aggregations tree execution:
  346. ["source","java",subs="attributes,callouts,macros"]
  347. --------------------------------------------------
  348. include-tagged::{doc-tests-file}[{api}-request-profiling-aggs]
  349. --------------------------------------------------
  350. <1> Retrieve the `AggregationProfileShardResult`
  351. <2> Iterate over the aggregation profile results
  352. <3> Retrieve the type of the aggregation (corresponds to Java class used to execute the aggregation)
  353. <4> Retrieve the time in millis spent executing the Lucene collector
  354. <5> Retrieve the profile results for the sub-aggregations (if any)
  355. The Rest API documentation contains more information about {ref}/_profiling_aggregations.html[Profiling Aggregations]