request-body.asciidoc 6.0 KB

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