request-body.asciidoc 6.6 KB

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