async-search.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. [[get-async-search]]
  121. ==== Get async search
  122. The get async search API retrieves the results of a previously submitted async
  123. search request given its id. If the {es} {security-features} are enabled, the
  124. access to the results of a specific async search is restricted to
  125. <<can-access-resources-check,the user or API key that submitted it>>.
  126. [source,console,id=get-async-search-date-histogram-example]
  127. --------------------------------------------------
  128. GET /_async_search/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=
  129. --------------------------------------------------
  130. // TEST[continued s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/\${body.id}/]
  131. [source,console-result]
  132. --------------------------------------------------
  133. {
  134. "id" : "FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=",
  135. "is_partial" : true, <1>
  136. "is_running" : true, <2>
  137. "start_time_in_millis" : 1583945890986,
  138. "expiration_time_in_millis" : 1584377890986, <3>
  139. "response" : {
  140. "took" : 12144,
  141. "timed_out" : false,
  142. "num_reduce_phases" : 46, <4>
  143. "_shards" : {
  144. "total" : 562,
  145. "successful" : 188, <5>
  146. "skipped" : 0,
  147. "failed" : 0
  148. },
  149. "hits" : {
  150. "total" : {
  151. "value" : 456433,
  152. "relation" : "eq"
  153. },
  154. "max_score" : null,
  155. "hits" : [ ]
  156. },
  157. "aggregations" : { <6>
  158. "sale_date" : {
  159. "buckets" : []
  160. }
  161. }
  162. }
  163. }
  164. --------------------------------------------------
  165. // TESTRESPONSE[s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/$body.id/]
  166. // TESTRESPONSE[s/"is_partial" : true/"is_partial" : false/]
  167. // TESTRESPONSE[s/"is_running" : true/"is_running" : false/]
  168. // TESTRESPONSE[s/1583945890986/$body.start_time_in_millis/]
  169. // TESTRESPONSE[s/1584377890986/$body.expiration_time_in_millis/]
  170. // TESTRESPONSE[s/"took" : 12144/"took": $body.response.took/]
  171. // TESTRESPONSE[s/"total" : 562/"total": $body.response._shards.total/]
  172. // TESTRESPONSE[s/"successful" : 188/"successful": $body.response._shards.successful/]
  173. // TESTRESPONSE[s/"value" : 456433/"value": $body.response.hits.total.value/]
  174. // TESTRESPONSE[s/"buckets" : \[\]/"buckets": $body.response.aggregations.sale_date.buckets/]
  175. // TESTRESPONSE[s/"num_reduce_phases" : 46,//]
  176. <1> When the query is no longer running, indicates whether the search failed
  177. or was successfully completed on all shards. While the query is being
  178. executed, `is_partial` is always set to `true`
  179. <2> Whether the search is still being executed or it has completed
  180. <3> When the async search will expire
  181. <4> Indicates how many reductions of the results have been performed. If this
  182. number increases compared to the last retrieved results, you can expect
  183. additional results included in the search response
  184. <5> Indicates how many shards have executed the query. Note that in order for
  185. shard results to be included in the search response, they need to be reduced
  186. first.
  187. <6> Partial aggregations results, coming from the shards that have already
  188. completed the execution of the query.
  189. The `wait_for_completion_timeout` parameter can also be provided when calling
  190. the Get Async Search API, in order to wait for the search to be completed up
  191. until the provided timeout. Final results will be returned if available before
  192. the timeout expires, otherwise the currently available results will be returned
  193. once the timeout expires. By default no timeout is set meaning that the
  194. currently available results will be returned without any additional wait.
  195. The `keep_alive` parameter specifies how long the async search should be
  196. available in the cluster. When not specified, the `keep_alive` set with the
  197. corresponding submit async request will be used. Otherwise, it is possible to
  198. override such value and extend the validity of the request. When this period
  199. expires, the search, if still running, is cancelled. If the search is completed,
  200. its saved results are deleted.
  201. [[get-async-search-status]]
  202. ==== Get async search status
  203. The get async search status API, without retrieving search results, shows only
  204. the status of a previously submitted async search request given its `id`. If the
  205. {es} {security-features} are enabled, the access to the get async search status
  206. API is restricted to the <<built-in-roles, monitoring_user role>>.
  207. [source,console,id=get-async-search-status-example]
  208. --------------------------------------------------
  209. GET /_async_search/status/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=
  210. --------------------------------------------------
  211. // TEST[continued s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/\${body.id}/]
  212. [source,console-result]
  213. --------------------------------------------------
  214. {
  215. "id" : "FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=",
  216. "is_running" : true,
  217. "is_partial" : true,
  218. "start_time_in_millis" : 1583945890986,
  219. "expiration_time_in_millis" : 1584377890986,
  220. "_shards" : {
  221. "total" : 562,
  222. "successful" : 188, <1>
  223. "skipped" : 0,
  224. "failed" : 0
  225. }
  226. }
  227. --------------------------------------------------
  228. // TEST[skip: a sample output of a status of a running async search]
  229. <1> Indicates how many shards have executed the query so far.
  230. For an async search that has been completed, the status response has an
  231. additional `completion_status` field that shows the status code of the completed
  232. async search.
  233. [source,console-result]
  234. --------------------------------------------------
  235. {
  236. "id" : "FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=",
  237. "is_running" : false,
  238. "is_partial" : false,
  239. "start_time_in_millis" : 1583945890986,
  240. "expiration_time_in_millis" : 1584377890986,
  241. "_shards" : {
  242. "total" : 562,
  243. "successful" : 562,
  244. "skipped" : 0,
  245. "failed" : 0
  246. },
  247. "completion_status" : 200 <1>
  248. }
  249. --------------------------------------------------
  250. // TEST[skip: a sample output of a status of a completed async search]
  251. <1> Indicates that the async search was successfully completed.
  252. [source,console-result]
  253. --------------------------------------------------
  254. {
  255. "id" : "FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=",
  256. "is_running" : false,
  257. "is_partial" : true,
  258. "start_time_in_millis" : 1583945890986,
  259. "expiration_time_in_millis" : 1584377890986,
  260. "_shards" : {
  261. "total" : 562,
  262. "successful" : 450,
  263. "skipped" : 0,
  264. "failed" : 112
  265. },
  266. "completion_status" : 503 <1>
  267. }
  268. --------------------------------------------------
  269. // TEST[skip: a sample output of a status of a completed async search]
  270. <1> Indicates that the async search was completed with an error.
  271. [[delete-async-search]]
  272. ==== Delete async search
  273. You can use the delete async search API to manually delete an async search by
  274. ID. If the search is still running, the search request will be cancelled.
  275. Otherwise, the saved search results are deleted.
  276. [source,console,id=delete-async-search-date-histogram-example]
  277. --------------------------------------------------
  278. DELETE /_async_search/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=
  279. --------------------------------------------------
  280. // TEST[continued s/FmRldE8zREVEUzA2ZVpUeGs2ejJFUFEaMkZ5QTVrSTZSaVN3WlNFVmtlWHJsdzoxMDc=/\${body.id}/]
  281. If the {es} {security-features} are enabled, the deletion of a specific async
  282. search is restricted to:
  283. * The authenticated user that submitted the original search request.
  284. * Users that have the `cancel_task` cluster privilege.