Skip to content

Commit 0f5a0e7

Browse files
authored
[DOCS] SQL: Document async SQL search (#74975) (#75037)
Documents async SQL search functionality. I plan to add formal API documentation for the async APIs with a later PR. Relates to #73991 and #74845. # Conflicts: # docs/reference/release-notes/highlights.asciidoc
1 parent 04308b5 commit 0f5a0e7

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed

docs/reference/sql/endpoints/rest.asciidoc

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* <<sql-rest-params>>
1212
* <<sql-runtime-fields>>
1313
* <<sql-rest-fields>>
14+
* <<sql-async>>
1415

1516
[[sql-rest-overview]]
1617
=== Overview
@@ -561,6 +562,170 @@ Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z|TUESDAY
561562
----
562563
// TESTRESPONSE[non_json]
563564

565+
[[sql-async]]
566+
=== Run an async SQL search
567+
568+
By default, SQL searches are synchronous. They wait for complete results before
569+
returning a response. However, results can take longer for searches across large
570+
data sets or <<data-tiers,frozen data>>.
571+
572+
To avoid long waits, run an async SQL search. Set `wait_for_completion_timeout`
573+
to a duration you’d like to wait for synchronous results.
574+
575+
[source,console]
576+
----
577+
POST _sql?format=json
578+
{
579+
"wait_for_completion_timeout": "2s",
580+
"query": "SELECT * FROM library ORDER BY page_count DESC",
581+
"fetch_size": 5
582+
}
583+
----
584+
// TEST[setup:library]
585+
// TEST[s/"wait_for_completion_timeout": "2s"/"wait_for_completion_timeout": "0"/]
586+
587+
If the search doesn’t finish within this period, the search becomes async. The
588+
API returns:
589+
590+
* An `id` for the search.
591+
* An `is_partial` value of `true`, indicating the search results are incomplete.
592+
* An `is_running` value of `true`, indicating the search is still running in the
593+
background.
594+
595+
For CSV, TSV, and TXT responses, the API returns these values in the respective
596+
`Async-ID`, `Async-partial`, and `Async-running` HTTP headers instead.
597+
598+
[source,console-result]
599+
----
600+
{
601+
"id": "FnR0TDhyWUVmUmVtWXRWZER4MXZiNFEad2F5UDk2ZVdTVHV1S0xDUy00SklUdzozMTU=",
602+
"is_partial": true,
603+
"is_running": true,
604+
"rows": [ ]
605+
}
606+
----
607+
// TESTRESPONSE[s/FnR0TDhyWUVmUmVtWXRWZER4MXZiNFEad2F5UDk2ZVdTVHV1S0xDUy00SklUdzozMTU=/$body.id/]
608+
// TESTRESPONSE[s/"is_partial": true/"is_partial": $body.is_partial/]
609+
// TESTRESPONSE[s/"is_running": true/"is_running": $body.is_running/]
610+
611+
To check the progress of an async search, use the search ID with the get async
612+
SQL search status API.
613+
614+
[source,console]
615+
----
616+
GET _sql/async/status/FnR0TDhyWUVmUmVtWXRWZER4MXZiNFEad2F5UDk2ZVdTVHV1S0xDUy00SklUdzozMTU=
617+
----
618+
// TEST[skip: no access to search ID]
619+
620+
If `is_running` and `is_partial` are `false`, the async search has finished with
621+
complete results.
622+
623+
[source,console-result]
624+
----
625+
{
626+
"id": "FnR0TDhyWUVmUmVtWXRWZER4MXZiNFEad2F5UDk2ZVdTVHV1S0xDUy00SklUdzozMTU=",
627+
"is_running": false,
628+
"is_partial": false,
629+
"expiration_time_in_millis": 1611690295000,
630+
"completion_status": 200
631+
}
632+
----
633+
// TESTRESPONSE[s/FnR0TDhyWUVmUmVtWXRWZER4MXZiNFEad2F5UDk2ZVdTVHV1S0xDUy00SklUdzozMTU=/$body.id/]
634+
// TESTRESPONSE[s/"expiration_time_in_millis": 1611690295000/"expiration_time_in_millis": $body.expiration_time_in_millis/]
635+
636+
To get the results, use the search ID with the get async SQL search API. If the
637+
search is still running, specify how long you’d like to wait using
638+
`wait_for_completion_timeout`. You can also specify the response `format`.
639+
640+
[source,console]
641+
----
642+
GET _sql/async/FnR0TDhyWUVmUmVtWXRWZER4MXZiNFEad2F5UDk2ZVdTVHV1S0xDUy00SklUdzozMTU=?wait_for_completion_timeout=2s&format=json
643+
----
644+
// TEST[skip: no access to search ID]
645+
646+
[discrete]
647+
[[sql-async-retention]]
648+
==== Change the search retention period
649+
650+
By default, {es} stores async SQL searches for five days. After this period,
651+
{es} deletes the search and its results, even if the search is still running. To
652+
change this retention period, use the `keep_alive` parameter.
653+
654+
[source,console]
655+
----
656+
POST _sql?format=json
657+
{
658+
"keep_alive": "2d",
659+
"wait_for_completion_timeout": "2s",
660+
"query": "SELECT * FROM library ORDER BY page_count DESC",
661+
"fetch_size": 5
662+
}
663+
----
664+
// TEST[setup:library]
665+
666+
You can use the get async SQL search API's `keep_alive` parameter to later
667+
change the retention period. The new period starts after the request runs.
668+
669+
[source,console]
670+
----
671+
GET _sql/async/FmdMX2pIang3UWhLRU5QS0lqdlppYncaMUpYQ05oSkpTc3kwZ21EdC1tbFJXQToxOTI=?keep_alive=5d&wait_for_completion_timeout=2s&format=json
672+
----
673+
// TEST[skip: no access to search ID]
674+
675+
Use the delete async SQL search API to delete an async search before the
676+
`keep_alive` period ends. If the search is still running, {es} cancels it.
677+
678+
[source,console]
679+
----
680+
DELETE _sql/async/delete/FmdMX2pIang3UWhLRU5QS0lqdlppYncaMUpYQ05oSkpTc3kwZ21EdC1tbFJXQToxOTI=
681+
----
682+
// TEST[skip: no access to search ID]
683+
684+
[discrete]
685+
[[sql-store-searches]]
686+
==== Store synchronous SQL searches
687+
688+
By default, {es} only stores async SQL searches. To save a synchronous search,
689+
specify `wait_for_completion_timeout` and set `keep_on_completion` to `true`.
690+
691+
[source,console]
692+
----
693+
POST _sql?format=json
694+
{
695+
"keep_on_completion": true,
696+
"wait_for_completion_timeout": "2s",
697+
"query": "SELECT * FROM library ORDER BY page_count DESC",
698+
"fetch_size": 5
699+
}
700+
----
701+
// TEST[setup:library]
702+
703+
If `is_partial` and `is_running` are `false`, the search was synchronous and
704+
returned complete results.
705+
706+
[source,console-result]
707+
----
708+
{
709+
"id": "Fnc5UllQdUVWU0NxRFNMbWxNYXplaFEaMUpYQ05oSkpTc3kwZ21EdC1tbFJXQTo0NzA=",
710+
"is_partial": false,
711+
"is_running": false,
712+
"rows": ...,
713+
"columns": ...,
714+
"cursor": ...
715+
}
716+
----
717+
// TESTRESPONSE[s/Fnc5UllQdUVWU0NxRFNMbWxNYXplaFEaMUpYQ05oSkpTc3kwZ21EdC1tbFJXQTo0NzA=/$body.id/]
718+
// TESTRESPONSE[s/"rows": \.\.\./"rows": $body.rows/]
719+
// TESTRESPONSE[s/"columns": \.\.\./"columns": $body.columns/]
720+
// TESTRESPONSE[s/"cursor": \.\.\./"cursor": $body.cursor/]
721+
722+
You can get the same results later using the search ID with the get async SQL
723+
search API.
724+
725+
Saved synchronous searches are still subject to the `keep_alive` retention
726+
period. When this period ends, {es} deletes the search results. You can also
727+
delete saved searches using the delete async SQL search API.
728+
564729
[[sql-rest-fields]]
565730
=== Supported REST parameters
566731

@@ -623,6 +788,32 @@ More information available https://docs.oracle.com/javase/8/docs/api/java/time/Z
623788
|Defines one or more <<runtime-search-request,runtime fields>> in the search
624789
request. These fields take precedence over mapped fields with the same name.
625790

791+
|keep_alive
792+
|`5d`
793+
a|Period {es} stores the search and its results. Must be greater than `1m` (one
794+
minute).
795+
796+
When this period end, {es} deletes the search and its results, even if the
797+
search is still running.
798+
799+
If `keep_on_completion` is `false`, {es} only stores searches that don't finish
800+
within the `wait_for_completion_timeout`, regardless of this value.
801+
802+
|keep_on_completion
803+
|`false`
804+
|If `true`, {es} stores the search and its results. If `false`, {es} stores the
805+
search and its results only if it doesn't finish before the
806+
`wait_for_completion_timeout`.
807+
808+
|wait_for_completion_timeout
809+
|None
810+
a|Duration to wait for the search to finish. Defaults to no timeout, meaning the
811+
request waits for complete results.
812+
813+
If you specify this parameter and the search finishes during the timeout, the
814+
request returns complete results. If the search doesn't finish during this
815+
period, the search becomes async. See <<sql-async>>.
816+
626817
|===
627818

628819
Do note that most parameters (outside the timeout and `columnar` ones) make sense only during the initial query - any follow-up pagination request only requires the `cursor` parameter as explained in the <<sql-pagination, pagination>> chapter.

0 commit comments

Comments
 (0)