async-search.asciidoc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[async-search]]
  4. === Async search
  5. The async search API let you asynchronously execute a
  6. search request, monitor its progress, and retrieve partial results
  7. as they become available.
  8. [[submit-async-search]]
  9. ==== Submit async search API
  10. Executes a search request asynchronously. It accepts the same
  11. parameters and request body as the <<search-search,search API>>.
  12. [source,console,id=submit-async-search-date-histogram-example]
  13. --------------------------------------------------
  14. POST /sales*/_async_search?size=0
  15. {
  16. "sort" : [
  17. { "date" : {"order" : "asc"} }
  18. ],
  19. "aggs" : {
  20. "sale_date" : {
  21. "date_histogram" : {
  22. "field" : "date",
  23. "calendar_interval": "1d"
  24. }
  25. }
  26. }
  27. }
  28. --------------------------------------------------
  29. // TEST[setup:sales]
  30. // TEST[s/size=0/size=0&wait_for_completion_timeout=10s&keep_on_completion=true/]
  31. The response contains an identifier of the search being executed.
  32. You can use this ID to later retrieve the search's final results.
  33. The currently available search
  34. results are returned as part of the <<search-api-response-body,`response`>> object.
  35. [source,console-result]
  36. --------------------------------------------------
  37. {
  38. "id" : "FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=", <1>
  39. "is_partial" : true, <2>
  40. "is_running" : true, <3>
  41. "start_time_in_millis" : 1583945890986,
  42. "expiration_time_in_millis" : 1584377890986,
  43. "response" : {
  44. "took" : 1122,
  45. "timed_out" : false,
  46. "num_reduce_phases" : 0,
  47. "_shards" : {
  48. "total" : 562, <4>
  49. "successful" : 3, <5>
  50. "skipped" : 0,
  51. "failed" : 0
  52. },
  53. "hits" : {
  54. "total" : {
  55. "value" : 157483, <6>
  56. "relation" : "gte"
  57. },
  58. "max_score" : null,
  59. "hits" : [ ]
  60. }
  61. }
  62. }
  63. --------------------------------------------------
  64. // TESTRESPONSE[s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/$body.id/]
  65. // TESTRESPONSE[s/"is_partial" : true/"is_partial": $body.is_partial/]
  66. // TESTRESPONSE[s/"is_running" : true/"is_running": $body.is_running/]
  67. // TESTRESPONSE[s/1583945890986/$body.start_time_in_millis/]
  68. // TESTRESPONSE[s/1584377890986/$body.expiration_time_in_millis/]
  69. // TESTRESPONSE[s/"took" : 1122/"took": $body.response.took/]
  70. // TESTRESPONSE[s/"num_reduce_phases" : 0,//]
  71. // TESTRESPONSE[s/"total" : 562/"total": $body.response._shards.total/]
  72. // TESTRESPONSE[s/"successful" : 3/"successful": $body.response._shards.successful/]
  73. // TESTRESPONSE[s/"value" : 157483/"value": $body.response.hits.total.value/]
  74. // TESTRESPONSE[s/"relation" : "gte"/"relation": $body.response.hits.total.relation/]
  75. // TESTRESPONSE[s/"hits" : \[ \]\n\s\s\s\s\}/"hits" : \[\]},"aggregations": $body.response.aggregations/]
  76. <1> Identifier of the async search that can be used to monitor its progress, retrieve its results, and/or delete it.
  77. <2> Whether the returned search results are partial or final
  78. <3> Whether the search is still being executed or it has completed
  79. <4> How many shards the search will be executed on, overall
  80. <5> How many shards have successfully completed the search
  81. <6> How many documents are currently matching the query, which belong to the shards that have already completed the search
  82. It is possible to block and wait until the search is completed up to a certain
  83. timeout by providing the `wait_for_completion_timeout` parameter, which
  84. defaults to `1` second.
  85. You can also specify how long the async search needs to be
  86. available through the `keep_alive` parameter, which defaults to `5d` (five days).
  87. Ongoing async searches and any saved search results are deleted after this
  88. period.
  89. NOTE: When the primary sort of the results is an indexed field, shards get
  90. sorted based on minimum and maximum value that they hold for that field,
  91. hence partial results become available following the sort criteria that
  92. was requested.
  93. The submit async search API supports the same <<search-search-api-query-params,parameters>>
  94. as the search API, though some have different default values:
  95. * `batched_reduce_size` defaults to `5`: this affects how often partial results
  96. become available, which happens whenever shard results are reduced. A partial
  97. reduction is performed every time the coordinating node has received a certain
  98. number of new shard responses (`5` by default).
  99. * `request_cache` defaults to `true`
  100. * `pre_filter_shard_size` defaults to `1` and cannot be changed: this is to
  101. enforce the execution of a pre-filter roundtrip to retrieve statistics from
  102. each shard so that the ones that surely don't hold any document matching the
  103. query get skipped.
  104. * `ccs_minimize_roundtrips` defaults to `false`, which is also the only
  105. supported value
  106. WARNING: Async search does not support <<request-body-search-scroll,scroll>>
  107. nor search requests that only include the <<search-suggesters,suggest section>>.
  108. {ccs} is supported only with <<ccs-min-roundtrips,`ccs_minimize_roundtrips`>>
  109. set to `false`.
  110. [[get-async-search]]
  111. ==== Get async search
  112. The get async search API retrieves the results of a previously submitted
  113. async search request given its id. If the {es} {security-features} are enabled.
  114. the access to the results of a specific async search is restricted to the user
  115. that submitted it in the first place.
  116. [source,console,id=get-async-search-date-histogram-example]
  117. --------------------------------------------------
  118. GET /_async_search/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=
  119. --------------------------------------------------
  120. // TEST[continued s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/\${body.id}/]
  121. [source,console-result]
  122. --------------------------------------------------
  123. {
  124. "id" : "FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=",
  125. "is_partial" : true, <1>
  126. "is_running" : true, <2>
  127. "start_time_in_millis" : 1583945890986,
  128. "expiration_time_in_millis" : 1584377890986, <3>
  129. "response" : {
  130. "took" : 12144,
  131. "timed_out" : false,
  132. "num_reduce_phases" : 46, <4>
  133. "_shards" : {
  134. "total" : 562, <5>
  135. "successful" : 188,
  136. "skipped" : 0,
  137. "failed" : 0
  138. },
  139. "hits" : {
  140. "total" : {
  141. "value" : 456433,
  142. "relation" : "eq"
  143. },
  144. "max_score" : null,
  145. "hits" : [ ]
  146. },
  147. "aggregations" : { <6>
  148. "sale_date" : {
  149. "buckets" : []
  150. }
  151. }
  152. }
  153. }
  154. --------------------------------------------------
  155. // TESTRESPONSE[s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/$body.id/]
  156. // TESTRESPONSE[s/"is_partial" : true/"is_partial" : false/]
  157. // TESTRESPONSE[s/"is_running" : true/"is_running" : false/]
  158. // TESTRESPONSE[s/1583945890986/$body.start_time_in_millis/]
  159. // TESTRESPONSE[s/1584377890986/$body.expiration_time_in_millis/]
  160. // TESTRESPONSE[s/"took" : 12144/"took": $body.response.took/]
  161. // TESTRESPONSE[s/"total" : 562/"total": $body.response._shards.total/]
  162. // TESTRESPONSE[s/"successful" : 188/"successful": $body.response._shards.successful/]
  163. // TESTRESPONSE[s/"value" : 456433/"value": $body.response.hits.total.value/]
  164. // TESTRESPONSE[s/"buckets" : \[\]/"buckets": $body.response.aggregations.sale_date.buckets/]
  165. // TESTRESPONSE[s/"num_reduce_phases" : 46,//]
  166. <1> Whether the returned search results are partial or final
  167. <2> Whether the search is still being executed or it has completed
  168. <3> When the async search will expire
  169. <4> Indicates how many reduction of the results have been performed. If this
  170. number increases compared to the last retrieved results, you can expect
  171. additional results included in the search response
  172. <5> Indicates how many shards have executed the query. Note that in order for
  173. shard results to be included in the search response, they need to be reduced
  174. first.
  175. <6> Partial aggregations results, coming from the shards that have already
  176. completed the execution of the query.
  177. The `wait_for_completion_timeout` parameter can also be provided when calling
  178. the Get Async Search API, in order to wait for the search to be completed up
  179. until the provided timeout. Final results will be returned if available before
  180. the timeout expires, otherwise the currently available results will be
  181. returned once the timeout expires. By default no timeout is set meaning that
  182. the currently available results will be returned without any additional wait.
  183. The `keep_alive` parameter specifies how long the async search should be
  184. available in the cluster. When not specified, the `keep_alive` set with the
  185. corresponding submit async request will be used. Otherwise, it is possible to
  186. override such value and extend the validity of the request. When this period
  187. expires, the search, if still running, is cancelled. If the search is
  188. completed, its saved results are deleted.
  189. [[delete-async-search]]
  190. ==== Delete async search
  191. You can use the delete async search API to manually delete an async search
  192. by ID. If the search is still running, the search request will be cancelled.
  193. Otherwise, the saved search results are deleted.
  194. [source,console,id=delete-async-search-date-histogram-example]
  195. --------------------------------------------------
  196. DELETE /_async_search/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=
  197. --------------------------------------------------
  198. // TEST[continued s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/\${body.id}/]