esql-async-query-api.asciidoc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. [[esql-async-query-api]]
  2. === {esql} async query API
  3. ++++
  4. <titleabbrev>{esql} async query API</titleabbrev>
  5. ++++
  6. .New API reference
  7. [sidebar]
  8. --
  9. For the most up-to-date API details, refer to {api-es}/group/endpoint-esql[ES|QL APIs].
  10. --
  11. Runs an async <<esql,{esql} query>>.
  12. The async query API lets you asynchronously execute a query request,
  13. monitor its progress, and retrieve results when they become available.
  14. The API accepts the same parameters and request body as the synchronous
  15. <<esql-query-api,query API>>, along with additional async related
  16. properties as outlined below.
  17. [source,console]
  18. ----
  19. POST /_query/async
  20. {
  21. "query": """
  22. FROM library
  23. | EVAL year = DATE_TRUNC(1 YEARS, release_date)
  24. | STATS MAX(page_count) BY year
  25. | SORT year
  26. | LIMIT 5
  27. """,
  28. "wait_for_completion_timeout": "2s"
  29. }
  30. ----
  31. // TEST[setup:library]
  32. // TEST[skip:awaitsfix https://github.com/elastic/elasticsearch/issues/104013]
  33. If the results are not available within the given timeout period, 2 seconds
  34. in this case, no results are returned but rather a response that
  35. includes:
  36. * A query ID
  37. * An `is_running` value of _true_, indicating the query is ongoing
  38. The query continues to run in the background without blocking other
  39. requests.
  40. [source,console-result]
  41. ----
  42. {
  43. "id": "FmNJRUZ1YWZCU3dHY1BIOUhaenVSRkEaaXFlZ3h4c1RTWFNocDdnY2FSaERnUTozNDE=",
  44. "is_running": true
  45. }
  46. ----
  47. // TEST[skip: no access to query ID - may return response values]
  48. Otherwise, if the response's `is_running` value is `false`, the async
  49. query has finished and the results are returned.
  50. [source,console-result]
  51. ----
  52. {
  53. "is_running": false,
  54. "columns": ...
  55. }
  56. ----
  57. // TEST[skip: no access to query ID - may return response values]
  58. [[esql-async-query-api-request]]
  59. ==== {api-request-title}
  60. `POST /_query/async`
  61. [[esql-async-query-api-prereqs]]
  62. ==== {api-prereq-title}
  63. * If the {es} {security-features} are enabled, you must have the `read`
  64. <<privileges-list-indices,index privilege>> for the data stream, index,
  65. or alias you query.
  66. [[esql-async-query-api-path-params]]
  67. ==== {api-path-parms-title}
  68. The API accepts the same parameters as the synchronous
  69. <<esql-query-api-query-params,query API>>.
  70. [[esql-async-query-api-request-body]]
  71. ==== {api-request-body-title}
  72. The API accepts the same request body as the synchronous
  73. <<esql-query-api-request-body,query API>>, along with the following
  74. parameters:
  75. [[esql-async-query-api-wait-for-completion-timeout]]
  76. `wait_for_completion_timeout`::
  77. +
  78. --
  79. (Optional, <<time-units,time value>>)
  80. Timeout duration to wait for the request to finish. Defaults to a 1 second,
  81. meaning the request waits for 1 second for the query results.
  82. If the query completes during this period then results will be
  83. returned. Otherwise, a query `id` is returned that can later be used to
  84. retrieve the results.
  85. If the request does not complete during this period, a query
  86. <<esql-async-query-api-response-body-query-id,id>> is returned.
  87. --
  88. [[esql-async-query-api-keep-on-completion]]
  89. `keep_on_completion`::
  90. +
  91. --
  92. (Optional, Boolean)
  93. If `true`, the query and its results are stored in the cluster.
  94. If `false`, the query and its results are stored in the cluster only if the
  95. request does not complete during the period set by the
  96. <<esql-async-query-api-wait-for-completion-timeout,`wait_for_completion_timeout`>>
  97. parameter. Defaults to `false`.
  98. --
  99. `keep_alive`::
  100. +
  101. --
  102. (Optional, <<time-units,time value>>)
  103. Period for which the query and its results are stored in the cluster. Defaults
  104. to `5d` (five days).
  105. When this period expires, the query and its results are deleted, even if the
  106. query is still ongoing.
  107. If the <<esql-async-query-api-keep-on-completion,`keep_on_completion`>> parameter
  108. is `false`, {es} only stores async queries that do not complete within the period
  109. set by the <<esql-async-query-api-wait-for-completion-timeout,`wait_for_completion_timeout`>>
  110. parameter, regardless of this value.
  111. --
  112. [[esql-async-query-api-response-body]]
  113. ==== {api-response-body-title}
  114. The API returns the same response body as the synchronous
  115. <<esql-query-api-response-body,query API>>, along with the following
  116. properties:
  117. [[esql-async-query-api-response-body-query-id]]
  118. `id`::
  119. +
  120. --
  121. (string)
  122. Identifier for the query.
  123. This query ID is only provided if one of the following conditions is met:
  124. * A query request does not return complete results during the
  125. <<esql-async-query-api-wait-for-completion-timeout,`wait_for_completion_timeout`>>
  126. parameter's timeout period.
  127. * The query request's <<esql-async-query-api-keep-on-completion,`keep_on_completion`>>
  128. parameter is `true`.
  129. You can use this ID with the <<esql-async-query-get-api,{esql} async query get
  130. API>> to get the current status and available results for the query.
  131. --
  132. `is_running`::
  133. +
  134. --
  135. (Boolean)
  136. If `true`, the query request is still executing.
  137. --
  138. `is_partial`::
  139. +
  140. --
  141. (Boolean)
  142. If `true`, the query has partial results - for example, as a result of using the <<esql-async-query-stop-api, async query stop API>>.
  143. --