esql-async-query-api.asciidoc 4.6 KB

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