search.asciidoc 20 KB

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