request-body.asciidoc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. [[search-request-body]]
  2. === Request Body Search
  3. Specifies search criteria as request body parameters.
  4. [source,console]
  5. --------------------------------------------------
  6. GET /twitter/_search
  7. {
  8. "query" : {
  9. "term" : { "user" : "kimchy" }
  10. }
  11. }
  12. --------------------------------------------------
  13. // TEST[setup:twitter]
  14. [[search-request-body-api-request]]
  15. ==== {api-request-title}
  16. `GET /<index>/_search
  17. {
  18. "query": {<parameters>}
  19. }`
  20. [[search-request-body-api-desc]]
  21. ==== {api-description-title}
  22. The search request can be executed with a search DSL, which includes the
  23. <<query-dsl,Query DSL>>, within its body.
  24. [[search-request-body-api-path-params]]
  25. ==== {api-path-parms-title}
  26. include::{docdir}/rest-api/common-parms.asciidoc[tag=index]
  27. [[search-request-body-api-request-body]]
  28. ==== {api-request-body-title}
  29. `allow_partial_search_results`::
  30. (Optional, boolean) Set to `false` to fail the request if only partial results
  31. are available. Defaults to `true`, which returns partial results in the event
  32. of timeouts or partial failures You can override the default behavior for all
  33. requests by setting `search.default_allow_partial_results` to `false` in the
  34. cluster settings.
  35. `batched_reduce_size`::
  36. (Optional, integer) The number of shard results that should be reduced at once
  37. on the coordinating node. This value should be used as a protection mechanism
  38. to reduce the memory overhead per search request if the potential number of
  39. shards in the request can be large.
  40. [[ccs-minimize-roundtrips]]
  41. `ccs_minimize_roundtrips`::
  42. (Optional, boolean) If `true`, the network round-trips between the
  43. coordinating node and the remote clusters ewill be minimized when executing
  44. {ccs} requests. See <<ccs-reduction>> for more. Defaults to `true`.
  45. include::{docdir}/rest-api/common-parms.asciidoc[tag=from]
  46. `request_cache`::
  47. (Optional, boolean) If `true`, the caching of search results is enabled for
  48. requests where `size` is `0`. See <<shard-request-cache>>.
  49. include::{docdir}/rest-api/common-parms.asciidoc[tag=search_type]
  50. `size`::
  51. (Optional, integer) The number of hits to return. Defaults to `10`.
  52. include::{docdir}/rest-api/common-parms.asciidoc[tag=terminate_after]
  53. include::{docdir}/rest-api/common-parms.asciidoc[tag=search_timeout]
  54. Out of the above, the `search_type`, `request_cache` and the
  55. `allow_partial_search_results` settings must be passed as query-string
  56. parameters. The rest of the search request should be passed within the body
  57. itself. The body content can also be passed as a REST parameter named `source`.
  58. Both HTTP GET and HTTP POST can be used to execute search with body. Since not
  59. all clients support GET with body, POST is allowed as well.
  60. [[search-request-body-api-example]]
  61. ==== {api-examples-title}
  62. [source,console]
  63. --------------------------------------------------
  64. GET /twitter/_search
  65. {
  66. "query" : {
  67. "term" : { "user" : "kimchy" }
  68. }
  69. }
  70. --------------------------------------------------
  71. // TEST[setup:twitter]
  72. The API returns the following response:
  73. [source,console-result]
  74. --------------------------------------------------
  75. {
  76. "took": 1,
  77. "timed_out": false,
  78. "_shards":{
  79. "total" : 1,
  80. "successful" : 1,
  81. "skipped" : 0,
  82. "failed" : 0
  83. },
  84. "hits":{
  85. "total" : {
  86. "value": 1,
  87. "relation": "eq"
  88. },
  89. "max_score": 1.3862942,
  90. "hits" : [
  91. {
  92. "_index" : "twitter",
  93. "_id" : "0",
  94. "_score": 1.3862942,
  95. "_source" : {
  96. "user" : "kimchy",
  97. "message": "trying out Elasticsearch",
  98. "date" : "2009-11-15T14:12:12",
  99. "likes" : 0
  100. }
  101. }
  102. ]
  103. }
  104. }
  105. --------------------------------------------------
  106. // TESTRESPONSE[s/"took": 1/"took": $body.took/]
  107. ==== Fast check for any matching docs
  108. NOTE: `terminate_after` is always applied **after** the `post_filter` and stops
  109. the query as well as the aggregation executions when enough hits have been
  110. collected on the shard. Though the doc count on aggregations may not reflect
  111. the `hits.total` in the response since aggregations are applied **before** the
  112. post filtering.
  113. In case we only want to know if there are any documents matching a
  114. specific query, we can set the `size` to `0` to indicate that we are not
  115. interested in the search results. Also we can set `terminate_after` to `1`
  116. to indicate that the query execution can be terminated whenever the first
  117. matching document was found (per shard).
  118. [source,console]
  119. --------------------------------------------------
  120. GET /_search?q=message:number&size=0&terminate_after=1
  121. --------------------------------------------------
  122. // TEST[setup:twitter]
  123. The response will not contain any hits as the `size` was set to `0`. The
  124. `hits.total` will be either equal to `0`, indicating that there were no
  125. matching documents, or greater than `0` meaning that there were at least
  126. as many documents matching the query when it was early terminated.
  127. Also if the query was terminated early, the `terminated_early` flag will
  128. be set to `true` in the response.
  129. [source,console-result]
  130. --------------------------------------------------
  131. {
  132. "took": 3,
  133. "timed_out": false,
  134. "terminated_early": true,
  135. "_shards": {
  136. "total": 1,
  137. "successful": 1,
  138. "skipped" : 0,
  139. "failed": 0
  140. },
  141. "hits": {
  142. "total" : {
  143. "value": 1,
  144. "relation": "eq"
  145. },
  146. "max_score": null,
  147. "hits": []
  148. }
  149. }
  150. --------------------------------------------------
  151. // TESTRESPONSE[s/"took": 3/"took": $body.took/]
  152. The `took` time in the response contains the milliseconds that this request
  153. took for processing, beginning quickly after the node received the query, up
  154. until all search related work is done and before the above JSON is returned
  155. to the client. This means it includes the time spent waiting in thread pools,
  156. executing a distributed search across the whole cluster and gathering all the
  157. results.
  158. include::request/docvalue-fields.asciidoc[]
  159. include::request/explain.asciidoc[]
  160. include::request/collapse.asciidoc[]
  161. include::request/from-size.asciidoc[]
  162. include::request/highlighting.asciidoc[]
  163. include::request/index-boost.asciidoc[]
  164. include::request/inner-hits.asciidoc[]
  165. include::request/min-score.asciidoc[]
  166. include::request/named-queries-and-filters.asciidoc[]
  167. include::request/post-filter.asciidoc[]
  168. include::request/preference.asciidoc[]
  169. include::request/query.asciidoc[]
  170. include::request/rescore.asciidoc[]
  171. include::request/script-fields.asciidoc[]
  172. include::request/scroll.asciidoc[]
  173. include::request/search-after.asciidoc[]
  174. include::request/search-type.asciidoc[]
  175. include::request/seq-no.asciidoc[]
  176. include::request/sort.asciidoc[]
  177. include::request/source-filtering.asciidoc[]
  178. include::request/stored-fields.asciidoc[]
  179. include::request/track-total-hits.asciidoc[]
  180. include::request/version.asciidoc[]