async-search.asciidoc 13 KB

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