request-body.asciidoc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. Out of the above, the `search_type`, `request_cache` and the `allow_partial_search_results`
  95. settings must be passed as query-string parameters. The rest of the search request should
  96. be passed within the body itself. The body content can also be passed as a REST
  97. parameter named `source`.
  98. Both HTTP GET and HTTP POST can be used to execute search with body. Since not
  99. all clients support GET with body, POST is allowed as well.
  100. [float]
  101. === Fast check for any matching docs
  102. NOTE: `terminate_after` is always applied **after** the `post_filter` and stops
  103. the query as well as the aggregation executions when enough hits have been
  104. collected on the shard. Though the doc count on aggregations may not reflect
  105. the `hits.total` in the response since aggregations are applied **before** the
  106. post filtering.
  107. In case we only want to know if there are any documents matching a
  108. specific query, we can set the `size` to `0` to indicate that we are not
  109. interested in the search results. Also we can set `terminate_after` to `1`
  110. to indicate that the query execution can be terminated whenever the first
  111. matching document was found (per shard).
  112. [source,js]
  113. --------------------------------------------------
  114. GET /_search?q=message:number&size=0&terminate_after=1
  115. --------------------------------------------------
  116. // CONSOLE
  117. // TEST[setup:twitter]
  118. The response will not contain any hits as the `size` was set to `0`. The
  119. `hits.total` will be either equal to `0`, indicating that there were no
  120. matching documents, or greater than `0` meaning that there were at least
  121. as many documents matching the query when it was early terminated.
  122. Also if the query was terminated early, the `terminated_early` flag will
  123. be set to `true` in the response.
  124. [source,js]
  125. --------------------------------------------------
  126. {
  127. "took": 3,
  128. "timed_out": false,
  129. "terminated_early": true,
  130. "_shards": {
  131. "total": 1,
  132. "successful": 1,
  133. "skipped" : 0,
  134. "failed": 0
  135. },
  136. "hits": {
  137. "total" : {
  138. "value": 1,
  139. "relation": "eq"
  140. },
  141. "max_score": null,
  142. "hits": []
  143. }
  144. }
  145. --------------------------------------------------
  146. // TESTRESPONSE[s/"took": 3/"took": $body.took/]
  147. The `took` time in the response contains the milliseconds that this request
  148. took for processing, beginning quickly after the node received the query, up
  149. until all search related work is done and before the above JSON is returned
  150. to the client. This means it includes the time spent waiting in thread pools,
  151. executing a distributed search across the whole cluster and gathering all the
  152. results.
  153. include::request/query.asciidoc[]
  154. include::request/from-size.asciidoc[]
  155. include::request/sort.asciidoc[]
  156. include::request/track-total-hits.asciidoc[]
  157. include::request/source-filtering.asciidoc[]
  158. include::request/stored-fields.asciidoc[]
  159. include::request/script-fields.asciidoc[]
  160. include::request/docvalue-fields.asciidoc[]
  161. include::request/post-filter.asciidoc[]
  162. include::request/highlighting.asciidoc[]
  163. include::request/rescore.asciidoc[]
  164. include::request/search-type.asciidoc[]
  165. include::request/scroll.asciidoc[]
  166. include::request/preference.asciidoc[]
  167. include::request/explain.asciidoc[]
  168. include::request/version.asciidoc[]
  169. include::request/index-boost.asciidoc[]
  170. include::request/min-score.asciidoc[]
  171. include::request/named-queries-and-filters.asciidoc[]
  172. include::request/inner-hits.asciidoc[]
  173. include::request/collapse.asciidoc[]
  174. include::request/search-after.asciidoc[]