request-body.asciidoc 7.0 KB

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