esql-async-query-api.asciidoc 4.6 KB

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