search.asciidoc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. [[search-search]]
  2. === Search API
  3. ++++
  4. <titleabbrev>Search</titleabbrev>
  5. ++++
  6. Returns search hits that match the query defined in the request.
  7. [source,console]
  8. ----
  9. GET /my-index-000001/_search
  10. ----
  11. // TEST[setup:my_index]
  12. [[search-search-api-request]]
  13. ==== {api-request-title}
  14. `GET /<target>/_search`
  15. `GET /_search`
  16. `POST /<target>/_search`
  17. `POST /_search`
  18. [[search-search-api-desc]]
  19. ==== {api-description-title}
  20. Allows you to execute a search query and get back search hits that match the
  21. query. You can provide search queries using the <<search-api-query-params-q,`q`
  22. query string parameter>> or <<search-request-body,request body>>.
  23. [[search-search-api-path-params]]
  24. ==== {api-path-parms-title}
  25. `<target>`::
  26. (Optional, string)
  27. Comma-separated list of data streams, indices, and index aliases to search.
  28. Wildcard (`*`) expressions are supported.
  29. +
  30. To search all data streams and indices in a cluster, omit this parameter or use
  31. `_all` or `*`.
  32. [role="child_attributes"]
  33. [[search-search-api-query-params]]
  34. ==== {api-query-parms-title}
  35. IMPORTANT: Several options for this API can be specified using a query parameter
  36. or a request body parameter. If both parameters are specified, only the query
  37. parameter is used.
  38. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
  39. +
  40. Defaults to `true`.
  41. [[search-partial-responses]]
  42. `allow_partial_search_results`::
  43. (Optional, boolean)
  44. If `true`, returns partial results if there are request timeouts or
  45. <<shard-failures,shard failures>>. If `false`, returns an error with
  46. no partial results. Defaults to `true`.
  47. +
  48. To override the default for this field, set the
  49. `search.default_allow_partial_results` cluster setting to `false`.
  50. `batched_reduce_size`::
  51. (Optional, integer) The number of shard results that should be reduced at once
  52. on the coordinating node. This value should be used as a protection mechanism
  53. to reduce the memory overhead per search request if the potential number of
  54. shards in the request can be large. Defaults to `512`.
  55. [[ccs-minimize-roundtrips]]
  56. `ccs_minimize_roundtrips`::
  57. (Optional, boolean) If `true`, network round-trips between the
  58. coordinating node and the remote clusters are minimized when executing
  59. {ccs} (CCS) requests. See <<ccs-network-delays>>. Defaults to `true`.
  60. `docvalue_fields`::
  61. (Optional, string) A comma-separated list of fields to return as the docvalue
  62. representation of a field for each hit.
  63. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
  64. +
  65. Defaults to `open`.
  66. `explain`::
  67. (Optional, boolean) If `true`, returns detailed information about score
  68. computation as part of a hit. Defaults to `false`.
  69. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=from]
  70. +
  71. By default, you cannot page through more than 10,000 hits using the `from` and
  72. `size` parameters. To page through more hits, use the
  73. <<search-after,`search_after`>> parameter.
  74. `ignore_throttled`::
  75. (Optional, boolean) If `true`, concrete, expanded or aliased indices will be
  76. ignored when frozen. Defaults to `true`.
  77. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
  78. `max_concurrent_shard_requests`::
  79. (Optional, integer) Defines the number of concurrent shard requests per node
  80. this search executes concurrently. This value should be used to limit the
  81. impact of the search on the cluster in order to limit the number of concurrent
  82. shard requests. Defaults to `5`.
  83. `pre_filter_shard_size`::
  84. (Optional, integer) Defines a threshold that enforces a pre-filter roundtrip
  85. to prefilter search shards based on query rewriting if the number of shards
  86. the search request expands to exceeds the threshold. This filter roundtrip can
  87. limit the number of shards significantly if for instance a shard can not match
  88. any documents based on its rewrite method ie. if date filters are mandatory
  89. to match but the shard bounds and the query are disjoint.
  90. When unspecified, the pre-filter phase is executed if any of these conditions is met:
  91. - The request targets more than `128` shards.
  92. - The request targets one or more read-only index.
  93. - The primary sort of the query targets an indexed field.
  94. [[search-preference]]
  95. `preference`::
  96. (Optional, string)
  97. Nodes and shards used for the search. By default, {es} selects from eligible
  98. nodes and shards using <<search-adaptive-replica,adaptive replica selection>>,
  99. accounting for <<shard-allocation-awareness,allocation awareness>>.
  100. +
  101. .Valid values for `preference`
  102. [%collapsible%open]
  103. ====
  104. `_only_local`::
  105. Run the search only on shards on the local node.
  106. `_local`::
  107. If possible, run the search on shards on the local node. If not, select shards
  108. using the default method.
  109. `_only_nodes:<node-id>,<node-id>`::
  110. Run the search on only the specified nodes IDs. If suitable shards exist on more
  111. than one selected nodes, use shards on those nodes using the default method. If
  112. none of the specified nodes are available, select shards from any available node
  113. using the default method.
  114. `_prefer_nodes:<node-id>,<node-id>`::
  115. If possible, run the search on the specified nodes IDs. If not, select shards
  116. using the default method.
  117. `_shards:<shard>,<shard>`::
  118. Run the search only on the specified shards. This value can be combined with
  119. other `preference` values, but this value must come first. For example:
  120. `_shards:2,3|_local`
  121. <custom-string>::
  122. Any string that does not start with `_`. If the cluster state and selected
  123. shards do not change, searches using the same `<custom-string>` value are routed
  124. to the same shards in the same order.
  125. ====
  126. [[search-api-query-params-q]]
  127. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=search-q]
  128. +
  129. You can use the `q` parameter to run a query parameter search. Query parameter
  130. searches do not support the full {es} <<query-dsl,Query DSL>> but are handy for
  131. testing.
  132. +
  133. IMPORTANT: The `q` parameter overrides the <<request-body-search-query,`query`>>
  134. parameter in the request body. If both parameters are specified, documents
  135. matching the `query` request body parameter are not returned.
  136. `request_cache`::
  137. (Optional, boolean) If `true`, the caching of search results is enabled for
  138. requests where `size` is `0`. See <<shard-request-cache>>. Defaults to index
  139. level settings.
  140. `rest_total_hits_as_int`::
  141. (Optional, boolean) Indicates whether hits.total should be rendered as an
  142. integer or an object in the rest search response. Defaults to `false`.
  143. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=routing]
  144. [[search-api-scroll-query-param]]
  145. `scroll`::
  146. (Optional, <<time-units,time value>>)
  147. Period to retain the <<scroll-search-context,search context>> for scrolling. See
  148. <<scroll-search-results>>.
  149. +
  150. By default, this value cannot exceed `1d` (24 hours). You can change
  151. this limit using the `search.max_keep_alive` cluster-level setting.
  152. [[search-type]]
  153. `search_type`::
  154. (Optional, string)
  155. How {wikipedia}/Tf–idf[distributed term frequencies] are calculated for
  156. <<relevance-scores,relevance scoring>>.
  157. +
  158. .Valid values for `search_type`
  159. [%collapsible%open]
  160. ====
  161. `query_then_fetch`::
  162. (Default)
  163. Distributed term frequencies are calculated locally for each shard running the
  164. search. We recommend this option for faster searches with potentially less
  165. accurate scoring.
  166. [[dfs-query-then-fetch]]
  167. `dfs_query_then_fetch`::
  168. Distributed term frequencies are calculated globally, using information gathered
  169. from all shards running the search. While this option increases the accuracy of
  170. scoring, it adds a round-trip to each shard, which can result in slower
  171. searches.
  172. ====
  173. `seq_no_primary_term`::
  174. (Optional, boolean) If `true`, returns sequence number and primary term of the
  175. last modification of each hit. See <<optimistic-concurrency-control>>.
  176. `size`::
  177. (Optional, integer) Defines the number of hits to return. Defaults to `10`.
  178. +
  179. By default, you cannot page through more than 10,000 hits using the `from` and
  180. `size` parameters. To page through more hits, use the
  181. <<search-after,`search_after`>> parameter.
  182. `sort`::
  183. (Optional, string) A comma-separated list of <field>:<direction> pairs.
  184. `_source`::
  185. (Optional)
  186. Indicates which <<mapping-source-field,source fields>> are returned for matching
  187. documents. These fields are returned in the `hits._source` property of
  188. the search response. Defaults to `true`.
  189. +
  190. .Valid values for `_source`
  191. [%collapsible%open]
  192. ====
  193. `true`::
  194. (boolean)
  195. The entire document source is returned.
  196. `false`::
  197. (boolean)
  198. The document source is not returned.
  199. `<string>`::
  200. (string)
  201. Comma-separated list of source fields to return.
  202. Wildcard (`*`) patterns are supported.
  203. ====
  204. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=source_excludes]
  205. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=source_includes]
  206. `stats`::
  207. (Optional, string) Specific `tag` of the request for logging and statistical
  208. purposes.
  209. `stored_fields`::
  210. (Optional, string) A comma-separated list of stored fields to return as part
  211. of a hit. If no fields are specified, no stored fields are included in the
  212. response.
  213. +
  214. If this field is specified, the `_source` parameter defaults to `false`. You can
  215. pass `_source: true` to return both source fields and
  216. stored fields in the search response.
  217. `suggest_field`::
  218. (Optional, string) Specifies which field to use for suggestions.
  219. `suggest_text`::
  220. (Optional, string) The source text for which the suggestions should be
  221. returned.
  222. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=terminate_after]
  223. +
  224. Defaults to `0`, which does not terminate query execution early.
  225. `timeout`::
  226. (Optional, <<time-units, time units>>) Specifies the period of time to wait
  227. for a response. If no response is received before the timeout expires, the
  228. request fails and returns an error. Defaults to no timeout.
  229. `track_scores`::
  230. (Optional, boolean) If `true`, calculate and return document scores, even if
  231. the scores are not used for sorting. Defaults to `false`.
  232. `track_total_hits`::
  233. (Optional, integer or boolean)
  234. Number of hits matching the query to count accurately. Defaults to `10000`.
  235. +
  236. If `true`, the default value is used. If `false`, the response does not
  237. include the total number of hits matching the query.
  238. `typed_keys`::
  239. (Optional, boolean) If `true`, aggregation and suggester names are be prefixed
  240. by their respective types in the response. Defaults to `true`.
  241. `version`::
  242. (Optional, boolean)
  243. If `true`, returns document version as part of a hit. Defaults to `false`.
  244. [role="child_attributes"]
  245. [[search-search-api-request-body]]
  246. ==== {api-request-body-title}
  247. [[search-docvalue-fields-param]]
  248. `docvalue_fields`::
  249. (Optional, array of strings and objects)
  250. Array of wildcard (`*`) patterns. The request returns doc values for field names
  251. matching these patterns in the `hits.fields` property of the response.
  252. +
  253. You can specify items in the array as a string or object.
  254. See <<docvalue-fields>>.
  255. +
  256. .Properties of `docvalue_fields` objects
  257. [%collapsible%open]
  258. ====
  259. `field`::
  260. (Required, string)
  261. Wildcard pattern. The request returns doc values for field names matching this
  262. pattern.
  263. `format`::
  264. (Optional, string)
  265. Format in which the doc values are returned.
  266. +
  267. For <<date,date fields>>, you can specify a date <<mapping-date-format,date
  268. `format`>>. For <<number,numeric fields>> fields, you can specify a
  269. https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html[DecimalFormat
  270. pattern].
  271. +
  272. For other field data types, this parameter is not supported.
  273. ====
  274. [[request-body-search-explain]]
  275. `explain`::
  276. (Optional, boolean) If `true`, returns detailed information about score
  277. computation as part of a hit. Defaults to `false`.
  278. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=from]
  279. +
  280. By default, you cannot page through more than 10,000 hits using the `from` and
  281. `size` parameters. To page through more hits, use the
  282. <<search-after,`search_after`>> parameter.
  283. `indices_boost`::
  284. (Optional, array of objects)
  285. Boosts the <<relevance-scores,`_score`>> of documents from specified indices.
  286. +
  287. .Properties of `indices_boost` objects
  288. [%collapsible%open]
  289. ====
  290. `<index>: <boost-value>`::
  291. (Required, float)
  292. `<index>` is the name of the index or index alias. Wildcard (`*`) expressions
  293. are supported.
  294. +
  295. `<boost-value>` is the factor by which scores are multiplied.
  296. +
  297. A boost value greater than `1.0` increases the score. A boost value between
  298. `0` and `1.0` decreases the score.
  299. ====
  300. [[search-api-min-score]]
  301. `min_score`::
  302. (Optional, float)
  303. Minimum <<relevance-scores,`_score`>> for matching documents. Documents with a
  304. lower `_score` are not included in the search results.
  305. [[request-body-search-query]]
  306. `query`::
  307. (Optional, <<query-dsl,query object>>) Defines the search definition using the
  308. <<query-dsl,Query DSL>>.
  309. [[request-body-search-seq-no-primary-term]]
  310. `seq_no_primary_term`::
  311. (Optional, boolean) If `true`, returns sequence number and primary term of the
  312. last modification of each hit. See <<optimistic-concurrency-control>>.
  313. `size`::
  314. (Optional, integer) The number of hits to return. Needs to be non-negative and defaults to `10`.
  315. +
  316. By default, you cannot page through more than 10,000 hits using the `from` and
  317. `size` parameters. To page through more hits, use the
  318. <<search-after,`search_after`>> parameter.
  319. `_source`::
  320. (Optional)
  321. Indicates which <<mapping-source-field,source fields>> are returned for matching
  322. documents. These fields are returned in the `hits._source` property of
  323. the search response. Defaults to `true`.
  324. +
  325. .Valid values for `_source`
  326. [%collapsible%open]
  327. ====
  328. `true`::
  329. (boolean)
  330. The entire document source is returned.
  331. `false`::
  332. (boolean)
  333. The document source is not returned.
  334. `<wildcard_pattern>`::
  335. (string or array of strings)
  336. Wildcard (`*`) pattern or array of patterns containing source fields to return.
  337. `<object>`::
  338. (object)
  339. Object containing a list of source fields to include or exclude.
  340. +
  341. .Properties for `<object>`
  342. [%collapsible%open]
  343. =====
  344. `excludes`::
  345. (string or array of strings)
  346. Wildcard (`*`) pattern or array of patterns containing source fields to exclude
  347. from the response.
  348. +
  349. You can also use this property to exclude fields from the subset specified in
  350. `includes` property.
  351. `includes`::
  352. (string or array of strings)
  353. Wildcard (`*`) pattern or array of patterns containing source fields to return.
  354. +
  355. If this property is specified, only these source fields are returned. You can
  356. exclude fields from this subset using the `excludes` property.
  357. =====
  358. ====
  359. [[stats-groups]]
  360. `stats`::
  361. (Optional, array of strings)
  362. Stats groups to associate with the search. Each group maintains a statistics
  363. aggregation for its associated searches. You can retrieve these stats using the
  364. <<indices-stats,indices stats API>>.
  365. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=terminate_after]
  366. +
  367. Defaults to `0`, which does not terminate query execution early.
  368. `timeout`::
  369. (Optional, <<time-units, time units>>) Specifies the period of time to wait
  370. for a response. If no response is received before the timeout expires, the
  371. request fails and returns an error. Defaults to no timeout.
  372. [[request-body-search-version]]
  373. `version`::
  374. (Optional, boolean)
  375. If `true`, returns document version as part of a hit. Defaults to `false`.
  376. [role="child_attributes"]
  377. [[search-api-response-body]]
  378. ==== {api-response-body-title}
  379. `_scroll_id`::
  380. (string)
  381. Identifier for the search and its <<scroll-search-context,search context>>.
  382. +
  383. You can use this scroll ID with the <<scroll-api,scroll API>> to retrieve the
  384. next batch of search results for the request. See
  385. <<scroll-search-results>>.
  386. +
  387. This parameter is only returned if the <<search-api-scroll-query-param,`scroll`
  388. query parameter>> is specified in the request.
  389. `took`::
  390. +
  391. --
  392. (integer)
  393. Milliseconds it took {es} to execute the request.
  394. This value is calculated by measuring the time elapsed
  395. between receipt of a request on the coordinating node
  396. and the time at which the coordinating node is ready to send the response.
  397. Took time includes:
  398. * Communication time between the coordinating node and data nodes
  399. * Time the request spends in the `search` <<modules-threadpool,thread pool>>,
  400. queued for execution
  401. * Actual execution time
  402. Took time does *not* include:
  403. * Time needed to send the request to {es}
  404. * Time needed to serialize the JSON response
  405. * Time needed to send the response to a client
  406. --
  407. `timed_out`::
  408. (boolean)
  409. If `true`,
  410. the request timed out before completion;
  411. returned results may be partial or empty.
  412. `_shards`::
  413. (object)
  414. Contains a count of shards used for the request.
  415. +
  416. .Properties of `_shards`
  417. [%collapsible%open]
  418. ====
  419. `total`::
  420. (integer)
  421. Total number of shards that require querying,
  422. including unallocated shards.
  423. `successful`::
  424. (integer)
  425. Number of shards that executed the request successfully.
  426. `skipped`::
  427. (integer)
  428. Number of shards that skipped the request because a lightweight check
  429. helped realize that no documents could possibly match on this shard. This
  430. typically happens when a search request includes a range filter and the
  431. shard only has values that fall outside of that range.
  432. `failed`::
  433. (integer)
  434. Number of shards that failed to execute the request. Note that shards
  435. that are not allocated will be considered neither successful nor failed.
  436. Having `failed+successful` less than `total` is thus an indication that
  437. some of the shards were not allocated.
  438. ====
  439. `hits`::
  440. (object)
  441. Contains returned documents and metadata.
  442. +
  443. .Properties of `hits`
  444. [%collapsible%open]
  445. ====
  446. `total`::
  447. (object)
  448. Metadata about the number of returned documents.
  449. +
  450. .Properties of `total`
  451. [%collapsible%open]
  452. =====
  453. `value`::
  454. (integer)
  455. Total number of returned documents.
  456. `relation`::
  457. (string)
  458. Indicates whether the number of returned documents in the `value`
  459. parameter is accurate or a lower bound.
  460. +
  461. .Values of `relation`:
  462. [%collapsible%open]
  463. ======
  464. `eq`:: Accurate
  465. `gte`:: Lower bound, including returned documents
  466. ======
  467. =====
  468. `max_score`::
  469. (float)
  470. Highest returned <<search-api-response-body-score,document `_score`>>.
  471. +
  472. This value is `null` for requests that do not sort by `_score`.
  473. [[search-api-response-body-hits]]
  474. `hits`::
  475. (array of objects)
  476. Array of returned document objects.
  477. +
  478. .Properties of `hits` objects
  479. [%collapsible%open]
  480. =====
  481. `_index`::
  482. (string)
  483. Name of the index containing the returned document.
  484. `_id`::
  485. (string)
  486. Unique identifier for the returned document.
  487. This ID is only unique within the returned index.
  488. [[search-api-response-body-score]]
  489. `_score`::
  490. (float)
  491. Positive 32-bit floating point number used to determine the relevance of the
  492. returned document.
  493. [[search-api-response-body-source]]
  494. `_source`::
  495. (object)
  496. Original JSON body passed for the document at index time.
  497. +
  498. You can use the `_source` parameter to exclude this property from the response
  499. or specify which source fields to return.
  500. `fields`::
  501. +
  502. --
  503. (object)
  504. Contains field values for the documents. These fields must be specified in the
  505. request using one or more of the following request parameters:
  506. * <<search-docvalue-fields-param,`docvalue_fields`>>
  507. * <<script-fields,`script_fields`>>
  508. * <<stored-fields,`stored_fields`>>
  509. This property is returned only if one or more of these parameters are set.
  510. --
  511. +
  512. .Properties of `fields`
  513. [%collapsible%open]
  514. ======
  515. `<field>`::
  516. (array)
  517. Key is the field name. Value is the value for the field.
  518. ======
  519. =====
  520. ====
  521. [[search-search-api-example]]
  522. ==== {api-examples-title}
  523. [source,console]
  524. ----
  525. GET /my-index-000001/_search
  526. {
  527. "query": {
  528. "term": {
  529. "user.id": "kimchy"
  530. }
  531. }
  532. }
  533. ----
  534. // TEST[setup:my_index]
  535. The API returns the following response:
  536. [source,console-result]
  537. ----
  538. {
  539. "took": 5,
  540. "timed_out": false,
  541. "_shards": {
  542. "total": 1,
  543. "successful": 1,
  544. "skipped": 0,
  545. "failed": 0
  546. },
  547. "hits": {
  548. "total": {
  549. "value": 1,
  550. "relation": "eq"
  551. },
  552. "max_score": 1.3862942,
  553. "hits": [
  554. {
  555. "_index": "my-index-000001",
  556. "_id": "0",
  557. "_score": 1.3862942,
  558. "_source": {
  559. "@timestamp": "2099-11-15T14:12:12",
  560. "http": {
  561. "request": {
  562. "method": "get"
  563. },
  564. "response": {
  565. "status_code": 200,
  566. "bytes": 1070000
  567. },
  568. "version": "1.1"
  569. },
  570. "source": {
  571. "ip": "127.0.0.1"
  572. },
  573. "message": "GET /search HTTP/1.1 200 1070000",
  574. "user": {
  575. "id": "kimchy"
  576. }
  577. }
  578. }
  579. ]
  580. }
  581. }
  582. ----
  583. // TESTRESPONSE[s/"took": 5/"took": $body.took/]