async-search.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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,
  77. retrieve its results, and/or delete it
  78. <2> When the query is no longer running, indicates whether the search failed
  79. or was successfully completed on all shards. While the query is being
  80. executed, `is_partial` is always set to `true`
  81. <3> Whether the search is still being executed or it has completed
  82. <4> How many shards the search will be executed on, overall
  83. <5> How many shards have successfully completed the search
  84. <6> How many documents are currently matching the query, which belong to the
  85. shards that have already completed the search
  86. NOTE: Although the query is no longer running, hence `is_running` is set to
  87. `false`, results may be partial. That happens in case the search failed after
  88. some shards returned their results, or when the node that is coordinating the
  89. async search dies.
  90. It is possible to block and wait until the search is completed up to a certain
  91. timeout by providing the `wait_for_completion_timeout` parameter, which
  92. defaults to `1` second. When the async search completes within such timeout,
  93. the response won't include the ID as the results are not stored in the cluster.
  94. The `keep_on_completion` parameter, which defaults to `false`, can be set to
  95. `true` to request that results are stored for later retrieval also when the
  96. search completes within the `wait_for_completion_timeout`.
  97. You can also specify how long the async search needs to be
  98. available through the `keep_alive` parameter, which defaults to `5d` (five days).
  99. Ongoing async searches and any saved search results are deleted after this
  100. period.
  101. NOTE: When the primary sort of the results is an indexed field, shards get
  102. sorted based on minimum and maximum value that they hold for that field,
  103. hence partial results become available following the sort criteria that
  104. was requested.
  105. The submit async search API supports the same <<search-search-api-query-params,parameters>>
  106. as the search API, though some have different default values:
  107. * `batched_reduce_size` defaults to `5`: this affects how often partial results
  108. become available, which happens whenever shard results are reduced. A partial
  109. reduction is performed every time the coordinating node has received a certain
  110. number of new shard responses (`5` by default).
  111. * `request_cache` defaults to `true`
  112. * `pre_filter_shard_size` defaults to `1` and cannot be changed: this is to
  113. enforce the execution of a pre-filter roundtrip to retrieve statistics from
  114. each shard so that the ones that surely don't hold any document matching the
  115. query get skipped.
  116. * `ccs_minimize_roundtrips` defaults to `false`, which is also the only
  117. supported value
  118. WARNING: Async search does not support <<request-body-search-scroll,scroll>>
  119. nor search requests that only include the <<search-suggesters,suggest section>>.
  120. {ccs} is supported only with <<ccs-min-roundtrips,`ccs_minimize_roundtrips`>>
  121. set to `false`.
  122. [[get-async-search]]
  123. ==== Get async search
  124. The get async search API retrieves the results of a previously submitted
  125. async search request given its id. If the {es} {security-features} are enabled.
  126. the access to the results of a specific async search is restricted to the user
  127. that submitted it in the first place.
  128. [source,console,id=get-async-search-date-histogram-example]
  129. --------------------------------------------------
  130. GET /_async_search/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=
  131. --------------------------------------------------
  132. // TEST[continued s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/\${body.id}/]
  133. [source,console-result]
  134. --------------------------------------------------
  135. {
  136. "id" : "FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=",
  137. "is_partial" : true, <1>
  138. "is_running" : true, <2>
  139. "start_time_in_millis" : 1583945890986,
  140. "expiration_time_in_millis" : 1584377890986, <3>
  141. "response" : {
  142. "took" : 12144,
  143. "timed_out" : false,
  144. "num_reduce_phases" : 46, <4>
  145. "_shards" : {
  146. "total" : 562, <5>
  147. "successful" : 188,
  148. "skipped" : 0,
  149. "failed" : 0
  150. },
  151. "hits" : {
  152. "total" : {
  153. "value" : 456433,
  154. "relation" : "eq"
  155. },
  156. "max_score" : null,
  157. "hits" : [ ]
  158. },
  159. "aggregations" : { <6>
  160. "sale_date" : {
  161. "buckets" : []
  162. }
  163. }
  164. }
  165. }
  166. --------------------------------------------------
  167. // TESTRESPONSE[s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/$body.id/]
  168. // TESTRESPONSE[s/"is_partial" : true/"is_partial" : false/]
  169. // TESTRESPONSE[s/"is_running" : true/"is_running" : false/]
  170. // TESTRESPONSE[s/1583945890986/$body.start_time_in_millis/]
  171. // TESTRESPONSE[s/1584377890986/$body.expiration_time_in_millis/]
  172. // TESTRESPONSE[s/"took" : 12144/"took": $body.response.took/]
  173. // TESTRESPONSE[s/"total" : 562/"total": $body.response._shards.total/]
  174. // TESTRESPONSE[s/"successful" : 188/"successful": $body.response._shards.successful/]
  175. // TESTRESPONSE[s/"value" : 456433/"value": $body.response.hits.total.value/]
  176. // TESTRESPONSE[s/"buckets" : \[\]/"buckets": $body.response.aggregations.sale_date.buckets/]
  177. // TESTRESPONSE[s/"num_reduce_phases" : 46,//]
  178. <1> When the query is no longer running, indicates whether the search failed
  179. or was successfully completed on all shards. While the query is being
  180. executed, `is_partial` is always set to `true`
  181. <2> Whether the search is still being executed or it has completed
  182. <3> When the async search will expire
  183. <4> Indicates how many reductions of the results have been performed. If this
  184. number increases compared to the last retrieved results, you can expect
  185. additional results included in the search response
  186. <5> Indicates how many shards have executed the query. Note that in order for
  187. shard results to be included in the search response, they need to be reduced
  188. first.
  189. <6> Partial aggregations results, coming from the shards that have already
  190. completed the execution of the query.
  191. The `wait_for_completion_timeout` parameter can also be provided when calling
  192. the Get Async Search API, in order to wait for the search to be completed up
  193. until the provided timeout. Final results will be returned if available before
  194. the timeout expires, otherwise the currently available results will be
  195. returned once the timeout expires. By default no timeout is set meaning that
  196. the currently available results will be returned without any additional wait.
  197. The `keep_alive` parameter specifies how long the async search should be
  198. available in the cluster. When not specified, the `keep_alive` set with the
  199. corresponding submit async request will be used. Otherwise, it is possible to
  200. override such value and extend the validity of the request. When this period
  201. expires, the search, if still running, is cancelled. If the search is
  202. completed, its saved results are deleted.
  203. [[delete-async-search]]
  204. ==== Delete async search
  205. You can use the delete async search API to manually delete an async search
  206. by ID. If the search is still running, the search request will be cancelled.
  207. Otherwise, the saved search results are deleted.
  208. [source,console,id=delete-async-search-date-histogram-example]
  209. --------------------------------------------------
  210. DELETE /_async_search/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=
  211. --------------------------------------------------
  212. // TEST[continued s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/\${body.id}/]