async-search.asciidoc 8.7 KB

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