1
0

esql-async-query-api.asciidoc 4.5 KB

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