async-search.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. [role="xpack"]
  2. [[async-search]]
  3. === Async search
  4. The async search API let you asynchronously execute a search request, monitor
  5. its progress, and retrieve partial results as they become available.
  6. [[submit-async-search]]
  7. ==== Submit async search API
  8. Executes a search request asynchronously. It accepts the same parameters and
  9. request body as the <<search-search,search API>>.
  10. [source,console,id=submit-async-search-date-histogram-example]
  11. --------------------------------------------------
  12. POST /sales*/_async_search?size=0
  13. {
  14. "sort": [
  15. { "date": { "order": "asc" } }
  16. ],
  17. "aggs": {
  18. "sale_date": {
  19. "date_histogram": {
  20. "field": "date",
  21. "calendar_interval": "1d"
  22. }
  23. }
  24. }
  25. }
  26. --------------------------------------------------
  27. // TEST[setup:sales]
  28. // TEST[s/size=0/size=0&wait_for_completion_timeout=10s&keep_on_completion=true/]
  29. The response contains an identifier of the search being executed. You can use
  30. this ID to later retrieve the search's final results. The currently available
  31. search results are returned as part of the
  32. <<search-api-response-body,`response`>> object.
  33. [source,console-result]
  34. --------------------------------------------------
  35. {
  36. "id" : "FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=", <1>
  37. "is_partial" : true, <2>
  38. "is_running" : true, <3>
  39. "start_time_in_millis" : 1583945890986,
  40. "expiration_time_in_millis" : 1584377890986,
  41. "response" : {
  42. "took" : 1122,
  43. "timed_out" : false,
  44. "num_reduce_phases" : 0,
  45. "_shards" : {
  46. "total" : 562, <4>
  47. "successful" : 3, <5>
  48. "skipped" : 0,
  49. "failed" : 0
  50. },
  51. "hits" : {
  52. "total" : {
  53. "value" : 157483, <6>
  54. "relation" : "gte"
  55. },
  56. "max_score" : null,
  57. "hits" : [ ]
  58. }
  59. }
  60. }
  61. --------------------------------------------------
  62. // TESTRESPONSE[s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/$body.id/]
  63. // TESTRESPONSE[s/"is_partial" : true/"is_partial": $body.is_partial/]
  64. // TESTRESPONSE[s/"is_running" : true/"is_running": $body.is_running/]
  65. // TESTRESPONSE[s/1583945890986/$body.start_time_in_millis/]
  66. // TESTRESPONSE[s/1584377890986/$body.expiration_time_in_millis/]
  67. // TESTRESPONSE[s/"took" : 1122/"took": $body.response.took/]
  68. // TESTRESPONSE[s/"num_reduce_phases" : 0,//]
  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. // TESTRESPONSE[s/"relation" : "gte"/"relation": $body.response.hits.total.relation/]
  73. // TESTRESPONSE[s/"hits" : \[ \]\n\s\s\s\s\}/"hits" : \[\]},"aggregations": $body.response.aggregations/]
  74. <1> Identifier of the async search that can be used to monitor its progress,
  75. retrieve its results, and/or delete it
  76. <2> When the query is no longer running, indicates whether the search failed
  77. or was successfully completed on all shards. While the query is being
  78. executed, `is_partial` is always set to `true`
  79. <3> Whether the search is still being executed or it has completed
  80. <4> How many shards the search will be executed on, overall
  81. <5> How many shards have successfully completed the search
  82. <6> How many documents are currently matching the query, which belong to the
  83. shards that have already completed the search
  84. NOTE: Although the query is no longer running, hence `is_running` is set to
  85. `false`, results may be partial. That happens in case the search failed after
  86. some shards returned their results, or when the node that is coordinating the
  87. async search dies.
  88. It is possible to block and wait until the search is completed up to a certain
  89. timeout by providing the `wait_for_completion_timeout` parameter, which
  90. defaults to `1` second. When the async search completes within such timeout,
  91. the response won't include the ID as the results are not stored in the cluster.
  92. The `keep_on_completion` parameter, which defaults to `false`, can be set to
  93. `true` to request that results are stored for later retrieval also when the
  94. search completes within the `wait_for_completion_timeout`.
  95. You can also specify how long the async search needs to be available through the
  96. `keep_alive` parameter, which defaults to `5d` (five days). Ongoing async
  97. searches and any saved search results are deleted after this period.
  98. NOTE: When the primary sort of the results is an indexed field, shards get
  99. sorted based on minimum and maximum value that they hold for that field, hence
  100. partial results become available following the sort criteria that was requested.
  101. The submit async search API supports the same
  102. <<search-search-api-query-params,parameters>> as the search API, though some
  103. have different default values:
  104. * `batched_reduce_size` defaults to `5`: this affects how often partial results
  105. become available, which happens whenever shard results are reduced. A partial
  106. reduction is performed every time the coordinating node has received a certain
  107. number of new shard responses (`5` by default).
  108. * `request_cache` defaults to `true`
  109. * `pre_filter_shard_size` defaults to `1` and cannot be changed: this is to
  110. enforce the execution of a pre-filter roundtrip to retrieve statistics from
  111. each shard so that the ones that surely don't hold any document matching the
  112. query get skipped.
  113. * `ccs_minimize_roundtrips` defaults to `false`, which is also the only
  114. supported value
  115. WARNING: Async search does not support <<scroll-search-results,scroll>>
  116. nor search requests that only include the <<search-suggesters,suggest section>>.
  117. {ccs-cap} is supported only with
  118. <<ccs-min-roundtrips,`ccs_minimize_roundtrips`>> set to `false`.
  119. WARNING: By default, {es} doesn't allow to store an async search response
  120. larger than 10Mb, and an attempt to do this results in an error. The maximum
  121. allowed size for a stored async search response can be set by changing the
  122. `search.max_async_search_response_size` cluster level setting.
  123. [[get-async-search]]
  124. ==== Get async search
  125. The get async search API retrieves the results of a previously submitted async
  126. search request given its id. If the {es} {security-features} are enabled, the
  127. access to the results of a specific async search is restricted to
  128. <<can-access-resources-check,the user or API key that submitted it>>.
  129. [source,console,id=get-async-search-date-histogram-example]
  130. --------------------------------------------------
  131. GET /_async_search/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=
  132. --------------------------------------------------
  133. // TEST[continued s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/\${body.id}/]
  134. [source,console-result]
  135. --------------------------------------------------
  136. {
  137. "id" : "FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=",
  138. "is_partial" : true, <1>
  139. "is_running" : true, <2>
  140. "start_time_in_millis" : 1583945890986,
  141. "expiration_time_in_millis" : 1584377890986, <3>
  142. "response" : {
  143. "took" : 12144,
  144. "timed_out" : false,
  145. "num_reduce_phases" : 46, <4>
  146. "_shards" : {
  147. "total" : 562,
  148. "successful" : 188, <5>
  149. "skipped" : 0,
  150. "failed" : 0
  151. },
  152. "hits" : {
  153. "total" : {
  154. "value" : 456433,
  155. "relation" : "eq"
  156. },
  157. "max_score" : null,
  158. "hits" : [ ]
  159. },
  160. "aggregations" : { <6>
  161. "sale_date" : {
  162. "buckets" : []
  163. }
  164. }
  165. }
  166. }
  167. --------------------------------------------------
  168. // TESTRESPONSE[s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/$body.id/]
  169. // TESTRESPONSE[s/"is_partial" : true/"is_partial" : false/]
  170. // TESTRESPONSE[s/"is_running" : true/"is_running" : false/]
  171. // TESTRESPONSE[s/1583945890986/$body.start_time_in_millis/]
  172. // TESTRESPONSE[s/1584377890986/$body.expiration_time_in_millis/]
  173. // TESTRESPONSE[s/"took" : 12144/"took": $body.response.took/]
  174. // TESTRESPONSE[s/"total" : 562/"total": $body.response._shards.total/]
  175. // TESTRESPONSE[s/"successful" : 188/"successful": $body.response._shards.successful/]
  176. // TESTRESPONSE[s/"value" : 456433/"value": $body.response.hits.total.value/]
  177. // TESTRESPONSE[s/"buckets" : \[\]/"buckets": $body.response.aggregations.sale_date.buckets/]
  178. // TESTRESPONSE[s/"num_reduce_phases" : 46,//]
  179. <1> When the query is no longer running, indicates whether the search failed
  180. or was successfully completed on all shards. While the query is being
  181. executed, `is_partial` is always set to `true`
  182. <2> Whether the search is still being executed or it has completed
  183. <3> When the async search will expire
  184. <4> Indicates how many reductions of the results have been performed. If this
  185. number increases compared to the last retrieved results, you can expect
  186. additional results included in the search response
  187. <5> Indicates how many shards have executed the query. Note that in order for
  188. shard results to be included in the search response, they need to be reduced
  189. first.
  190. <6> Partial aggregations results, coming from the shards that have already
  191. completed the execution of the query.
  192. The `wait_for_completion_timeout` parameter can also be provided when calling
  193. the Get Async Search API, in order to wait for the search to be completed up
  194. until the provided timeout. Final results will be returned if available before
  195. the timeout expires, otherwise the currently available results will be returned
  196. once the timeout expires. By default no timeout is set meaning that the
  197. currently available results will be returned without any additional wait.
  198. The `keep_alive` parameter specifies how long the async search should be
  199. available in the cluster. When not specified, the `keep_alive` set with the
  200. corresponding submit async request will be used. Otherwise, it is possible to
  201. override such value and extend the validity of the request. When this period
  202. expires, the search, if still running, is cancelled. If the search is completed,
  203. its saved results are deleted.
  204. [[get-async-search-status]]
  205. ==== Get async search status
  206. The get async search status API, without retrieving search results, shows only
  207. the status of a previously submitted async search request given its `id`. If the
  208. {es} {security-features} are enabled, the access to the get async search status
  209. API is restricted to the <<built-in-roles, monitoring_user role>>.
  210. [source,console,id=get-async-search-status-example]
  211. --------------------------------------------------
  212. GET /_async_search/status/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=
  213. --------------------------------------------------
  214. // TEST[continued s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/\${body.id}/]
  215. [source,console-result]
  216. --------------------------------------------------
  217. {
  218. "id" : "FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=",
  219. "is_running" : true,
  220. "is_partial" : true,
  221. "start_time_in_millis" : 1583945890986,
  222. "expiration_time_in_millis" : 1584377890986,
  223. "_shards" : {
  224. "total" : 562,
  225. "successful" : 188, <1>
  226. "skipped" : 0,
  227. "failed" : 0
  228. }
  229. }
  230. --------------------------------------------------
  231. // TEST[skip: a sample output of a status of a running async search]
  232. <1> Indicates how many shards have executed the query so far.
  233. For an async search that has been completed, the status response has an
  234. additional `completion_status` field that shows the status code of the completed
  235. async search.
  236. [source,console-result]
  237. --------------------------------------------------
  238. {
  239. "id" : "FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=",
  240. "is_running" : false,
  241. "is_partial" : false,
  242. "start_time_in_millis" : 1583945890986,
  243. "expiration_time_in_millis" : 1584377890986,
  244. "_shards" : {
  245. "total" : 562,
  246. "successful" : 562,
  247. "skipped" : 0,
  248. "failed" : 0
  249. },
  250. "completion_status" : 200 <1>
  251. }
  252. --------------------------------------------------
  253. // TEST[skip: a sample output of a status of a completed async search]
  254. <1> Indicates that the async search was successfully completed.
  255. [source,console-result]
  256. --------------------------------------------------
  257. {
  258. "id" : "FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=",
  259. "is_running" : false,
  260. "is_partial" : true,
  261. "start_time_in_millis" : 1583945890986,
  262. "expiration_time_in_millis" : 1584377890986,
  263. "_shards" : {
  264. "total" : 562,
  265. "successful" : 450,
  266. "skipped" : 0,
  267. "failed" : 112
  268. },
  269. "completion_status" : 503 <1>
  270. }
  271. --------------------------------------------------
  272. // TEST[skip: a sample output of a status of a completed async search]
  273. <1> Indicates that the async search was completed with an error.
  274. [[delete-async-search]]
  275. ==== Delete async search
  276. You can use the delete async search API to manually delete an async search by
  277. ID. If the search is still running, the search request will be cancelled.
  278. Otherwise, the saved search results are deleted.
  279. [source,console,id=delete-async-search-date-histogram-example]
  280. --------------------------------------------------
  281. DELETE /_async_search/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=
  282. --------------------------------------------------
  283. // TEST[continued s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/\${body.id}/]
  284. If the {es} {security-features} are enabled, the deletion of a specific async
  285. search is restricted to:
  286. * The authenticated user that submitted the original search request.
  287. * Users that have the `cancel_task` cluster privilege.