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`. When doing a cross-cluster search,
  114. setting it to `true` may improve overall search latency, particularly when
  115. searching clusters with a large number of nodes.
  116. WARNING: Async search does not support <<scroll-search-results,scroll>>
  117. nor search requests that only include the <<search-suggesters,suggest section>>.
  118. WARNING: By default, {es} doesn't allow to store an async search response
  119. larger than 10Mb, and an attempt to do this results in an error. The maximum
  120. allowed size for a stored async search response can be set by changing the
  121. `search.max_async_search_response_size` cluster level setting.
  122. [[get-async-search]]
  123. ==== Get async search
  124. The get async search API retrieves the results of a previously submitted async
  125. search request given its id. If the {es} {security-features} are enabled, the
  126. access to the results of a specific async search is restricted to
  127. <<can-access-resources-check,the user or API key that submitted it>>.
  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,
  147. "successful" : 188, <5>
  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 returned
  195. once the timeout expires. By default no timeout is set meaning that the
  196. 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 completed,
  202. its saved results are deleted.
  203. [[get-async-search-status]]
  204. ==== Get async search status
  205. The get async search status API, without retrieving search results, shows only
  206. the status of a previously submitted async search request given its `id`. If the
  207. {es} {security-features} are enabled, the access to the get async search status
  208. API is restricted to the <<built-in-roles, monitoring_user role>>.
  209. [source,console,id=get-async-search-status-example]
  210. --------------------------------------------------
  211. GET /_async_search/status/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=
  212. --------------------------------------------------
  213. // TEST[continued s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/\${body.id}/]
  214. [source,console-result]
  215. --------------------------------------------------
  216. {
  217. "id" : "FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=",
  218. "is_running" : true,
  219. "is_partial" : true,
  220. "start_time_in_millis" : 1583945890986,
  221. "expiration_time_in_millis" : 1584377890986,
  222. "_shards" : {
  223. "total" : 562,
  224. "successful" : 188, <1>
  225. "skipped" : 0,
  226. "failed" : 0
  227. }
  228. }
  229. --------------------------------------------------
  230. // TEST[skip: a sample output of a status of a running async search]
  231. <1> Indicates how many shards have executed the query so far.
  232. For an async search that has been completed, the status response has an
  233. additional `completion_status` field that shows the status code of the completed
  234. async search.
  235. [source,console-result]
  236. --------------------------------------------------
  237. {
  238. "id" : "FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=",
  239. "is_running" : false,
  240. "is_partial" : false,
  241. "start_time_in_millis" : 1583945890986,
  242. "expiration_time_in_millis" : 1584377890986,
  243. "_shards" : {
  244. "total" : 562,
  245. "successful" : 562,
  246. "skipped" : 0,
  247. "failed" : 0
  248. },
  249. "completion_status" : 200 <1>
  250. }
  251. --------------------------------------------------
  252. // TEST[skip: a sample output of a status of a completed async search]
  253. <1> Indicates that the async search was successfully completed.
  254. [source,console-result]
  255. --------------------------------------------------
  256. {
  257. "id" : "FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=",
  258. "is_running" : false,
  259. "is_partial" : true,
  260. "start_time_in_millis" : 1583945890986,
  261. "expiration_time_in_millis" : 1584377890986,
  262. "_shards" : {
  263. "total" : 562,
  264. "successful" : 450,
  265. "skipped" : 0,
  266. "failed" : 112
  267. },
  268. "completion_status" : 503 <1>
  269. }
  270. --------------------------------------------------
  271. // TEST[skip: a sample output of a status of a completed async search]
  272. <1> Indicates that the async search was completed with an error.
  273. [[delete-async-search]]
  274. ==== Delete async search
  275. You can use the delete async search API to manually delete an async search by
  276. ID. If the search is still running, the search request will be cancelled.
  277. Otherwise, the saved search results are deleted.
  278. [source,console,id=delete-async-search-date-histogram-example]
  279. --------------------------------------------------
  280. DELETE /_async_search/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=
  281. --------------------------------------------------
  282. // TEST[continued s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/\${body.id}/]
  283. If the {es} {security-features} are enabled, the deletion of a specific async
  284. search is restricted to:
  285. * The authenticated user that submitted the original search request.
  286. * Users that have the `cancel_task` cluster privilege.