esql-async-query-api.asciidoc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 outline 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 this parameter is specified and the request completes during this period,
  77. complete results are returned.
  78. If the request does not complete during this period, a query
  79. <<esql-async-query-api-response-body-query-id,id>> is returned.
  80. --
  81. [[esql-async-query-api-keep-on-completion]]
  82. `keep_on_completion`::
  83. +
  84. --
  85. (Optional, Boolean)
  86. If `true`, the query and its results are stored in the cluster.
  87. If `false`, the query and its results are stored in the cluster only if the
  88. request does not complete during the period set by the
  89. <<esql-async-query-api-wait-for-completion-timeout,`wait_for_completion_timeout`>>
  90. parameter. Defaults to `false`.
  91. --
  92. `keep_alive`::
  93. +
  94. --
  95. (Optional, <<time-units,time value>>)
  96. Period for which the query and its results are stored in the cluster. Defaults
  97. to `5d` (five days).
  98. When this period expires, the query and its results are deleted, even if the
  99. query is still ongoing.
  100. If the <<esql-async-query-api-keep-on-completion,`keep_on_completion`>> parameter
  101. is `false`, {es} only stores async queries that do not complete within the period
  102. set by the <<esql-async-query-api-wait-for-completion-timeout,`wait_for_completion_timeout`>>
  103. parameter, regardless of this value.
  104. --
  105. [[esql-async-query-api-response-body]]
  106. ==== {api-response-body-title}
  107. The API returns the same response body as the synchronous
  108. <<esql-query-api-response-body,query API>>, along with the following
  109. properties:
  110. [[esql-async-query-api-response-body-query-id]]
  111. `id`::
  112. +
  113. --
  114. (string)
  115. Identifier for the query.
  116. This query ID is only provided if one of the following conditions is met:
  117. * A query request does not return complete results during the
  118. <<esql-async-query-api-wait-for-completion-timeout,`wait_for_completion_timeout`>>
  119. parameter's timeout period.
  120. * The query request's <<esql-async-query-api-keep-on-completion,`keep_on_completion`>>
  121. parameter is `true`.
  122. You can use this ID with the <<esql-async-query-get-api,{esql} async query get
  123. API>> to get the current status and available results for the query.
  124. --
  125. `is_running`::
  126. +
  127. --
  128. (Boolean)
  129. If `true`, the query request is still executing.
  130. --