esql-async-query-api.asciidoc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. [[esql-async-query-api]]
  2. == {esql} async query API
  3. ++++
  4. <titleabbrev>{esql} async query API</titleabbrev>
  5. ++++
  6. Runs an async {esql} search.
  7. The async query API lets you asynchronously execute a search request,
  8. monitor its progress, and retrieve results as they become available.
  9. Executing an <<esql,ES|QL ({es} query language)>> is commonly quite fast,
  10. however searches across large data sets or frozen data can take some time.
  11. To avoid long waits, run an async {esql} search.
  12. Searches initiated by this API may return search results or not. The
  13. `wait_for_completion_timeout` property determines how long to wait for
  14. the search results. The default value is 1 second. If the results are
  15. not available by this time, a search id is return which can be later
  16. used to retrieve the results.
  17. Initiates an async search for an <<esql,ES|QL ({es} query language)>>
  18. query. The API accepts the same parameters and request body as the
  19. <<esql-query-api,query API>>.
  20. [source,console]
  21. ----
  22. POST /_query/async
  23. {
  24. "query": """
  25. FROM library
  26. | EVAL year = DATE_TRUNC(1 YEARS, release_date)
  27. | STATS MAX(page_count) BY year
  28. | SORT year
  29. | LIMIT 5
  30. """,
  31. "wait_for_completion_timeout": "2s"
  32. }
  33. ----
  34. // TEST[setup:library]
  35. If the results are not available within the timeout period, 2 seconds in
  36. this case, the search returns no results but rather a response that
  37. includes:
  38. * A search ID
  39. * An `is_running` value of true, indicating the search is ongoing
  40. The query continues to run in the background without blocking other
  41. requests.
  42. [source,console-result]
  43. ----
  44. {
  45. "id": "FmNJRUZ1YWZCU3dHY1BIOUhaenVSRkEaaXFlZ3h4c1RTWFNocDdnY2FSaERnUTozNDE=",
  46. "is_running": true
  47. }
  48. ----
  49. // TEST[skip: no access to search ID - may return response values]
  50. To check the progress of an async search, use the <<get-async-esql-query-api,get
  51. async ES|QL query API>> with the search ID. Specify how long you'd like for
  52. complete results in the `wait_for_completion_timeout` parameter.
  53. [source,console]
  54. ----
  55. GET /_query/async/get/FmNJRUZ1YWZCU3dHY1BIOUhaenVSRkEaaXFlZ3h4c1RTWFNocDdnY2FSaERnUTozNDE=?wait_for_completion_timeout=30s
  56. ----
  57. // TEST[skip: no access to search ID - may return response values]
  58. If the response's `is_running` value is `false`, the async search has
  59. finished, and the results are returned.
  60. [source,console-result]
  61. ----
  62. {
  63. "id": "FmNJRUZ1YWZCU3dHY1BIOUhaenVSRkEaaXFlZ3h4c1RTWFNocDdnY2FSaERnUTozNDE=",
  64. "is_running": false,
  65. "columns": ...
  66. }
  67. ----
  68. // TEST[skip: no access to search ID - may return response values]