Skip to content

Commit 79bc6ab

Browse files
committed
SQL: Polish the rest chapter (#38971)
Organize the text a bit and add tip on triple quotes in Kibana Console
1 parent e78f70e commit 79bc6ab

File tree

3 files changed

+118
-25
lines changed

3 files changed

+118
-25
lines changed
53.4 KB
Loading

docs/reference/sql/endpoints/rest.asciidoc

Lines changed: 117 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
== SQL REST API
55

66
The SQL REST API accepts SQL in a JSON document, executes it,
7-
and returns the results. For example:
8-
7+
and returns the results.
8+
For example:
99

1010
[source,js]
1111
--------------------------------------------------
@@ -32,19 +32,68 @@ James S.A. Corey |Leviathan Wakes |561 |2011-06-02T00:00:00.000Z
3232
// TESTRESPONSE[s/\|/\\|/ s/\+/\\+/]
3333
// TESTRESPONSE[_cat]
3434

35-
While the `text/plain` format is nice for humans, computers prefer something
36-
more structured. You can replace the value of `format` with:
37-
- `json` aka `application/json`
38-
- `yaml` aka `application/yaml`
39-
- `smile` aka `application/smile`
40-
- `cbor` aka `application/cbor`
41-
- `txt` aka `text/plain`
42-
- `csv` aka `text/csv`
43-
- `tsv` aka `text/tab-separated-values`
35+
[[sql-kibana-console]]
36+
.Using Kibana Console
37+
If you are using {kibana-ref}/console-kibana.html[Kibana Console].
38+
(which is highly recommended), take advantage of the
39+
triple quotes `"""` when creating the query. This not only automatically escapes double
40+
quotes (`"`) inside the query string but also support multi-line as shown below:
41+
image:images/sql/rest/console-triple-quotes.png[]
42+
43+
[[sql-rest-format]]
44+
[float]
45+
=== Response Data Formats
46+
47+
While the textual format is nice for humans, computers prefer something
48+
more structured.
49+
50+
{es-sql} can return the data in the following formats which can be set
51+
either through the `format` property in the URL or by setting the `Accept` HTTP header:
52+
53+
NOTE: The URL parameter takes precedence over the `Accept` HTTP header.
54+
If neither is specified then the response is returned in the same format as the request.
55+
56+
[cols="^m,^4m,^8"]
57+
58+
|===
59+
s|format
60+
s|`Accept` HTTP header
61+
s|Description
62+
63+
3+h| Human Readable
64+
65+
|csv
66+
|text/csv
67+
|https://en.wikipedia.org/wiki/Comma-separated_values[Comma-separated values]
68+
69+
|json
70+
|application/json
71+
|https://www.json.org/[JSON] (JavaScript Object Notation) human-readable format
72+
73+
|tsv
74+
|text/tab-separated-values
75+
|https://en.wikipedia.org/wiki/Tab-separated_values[Tab-separated values]
76+
77+
|txt
78+
|text/plain
79+
|CLI-like representation
80+
81+
|yaml
82+
|application/yaml
83+
|https://en.wikipedia.org/wiki/YAML[YAML] (YAML Ain't Markup Language) human-readable format
84+
85+
3+h| Binary Formats
86+
87+
|cbor
88+
|application/cbor
89+
|http://cbor.io/[Concise Binary Object Representation]
90+
91+
|smile
92+
|application/smile
93+
|https://en.wikipedia.org/wiki/Smile_(data_interchange_format)[Smile] binary data format similar to CBOR
94+
95+
|===
4496

45-
Alternatively you can set the `Accept` HTTP header to the appropriate media
46-
format. The GET parameter takes precedence over the header. If neither is
47-
specified then the response is returned in the same format as the request.
4897

4998
[source,js]
5099
--------------------------------------------------
@@ -80,7 +129,11 @@ Which returns:
80129
--------------------------------------------------
81130
// TESTRESPONSE[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl\+v\/\/\/w8=/$body.cursor/]
82131

83-
You can continue to the next page by sending back the `cursor` field. In
132+
[[sql-pagination]]
133+
[float]
134+
=== Paginating through a large response
135+
136+
Using the example above, onu can continue to the next page by sending back the `cursor` field. In
84137
case of text format the cursor is returned as `Cursor` http header.
85138

86139
[source,js]
@@ -111,7 +164,7 @@ Which looks like:
111164
--------------------------------------------------
112165
// TESTRESPONSE[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWODRMaXBUaVlRN21iTlRyWHZWYUdrdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl9f\/\/\/w8=/$body.cursor/]
113166

114-
Note that the `column` object is only part of the first page.
167+
Note that the `columns` object is only part of the first page.
115168

116169
You've reached the last page when there is no `cursor` returned
117170
in the results. Like Elasticsearch's <<search-request-scroll,scroll>>,
@@ -145,9 +198,11 @@ Which will like return the
145198

146199

147200
[[sql-rest-filtering]]
201+
[float]
202+
=== Filtering using {es} query DSL
148203

149204
You can filter the results that SQL will run on using a standard
150-
Elasticsearch query DSL by specifying the query in the filter
205+
{es} query DSL by specifying the query in the filter
151206
parameter.
152207

153208
[source,js]
@@ -181,10 +236,48 @@ Douglas Adams |The Hitchhiker's Guide to the Galaxy|180 |1979-10-12T
181236
// TESTRESPONSE[_cat]
182237

183238
[[sql-rest-fields]]
184-
In addition to the `query` and `cursor` fields, the request can
185-
contain `fetch_size` and `time_zone`. `fetch_size` is a hint for how
186-
many results to return in each page. SQL might chose to return more
187-
or fewer results though. `time_zone` is the time zone to use for datetime
188-
functions and datetime parsing. `time_zone` defaults to `utc` and can take
189-
any values documented
190-
http://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeZone.html[here].
239+
[float]
240+
=== Supported REST parameters
241+
242+
In addition to the `query` and `fetch_size`, a request a number of user-defined fields for specifying
243+
the request time-outs or localization information (such as timezone).
244+
245+
The table below lists the supported parameters:
246+
247+
[cols="^m,^m,^5"]
248+
249+
|===
250+
251+
s|name
252+
s|Default value
253+
s|Description
254+
255+
|query
256+
|Mandatory
257+
|SQL query to execute
258+
259+
|fetch_size
260+
|1000
261+
|The maximum number of rows (or entries) to return in one response
262+
263+
|filter
264+
|none
265+
|Optional {es} query DSL for additional <<sql-rest-filtering, filtering>>.
266+
267+
|request_timeout
268+
|90s
269+
|The timeout before the request fails.
270+
271+
|page_timeout
272+
|45s
273+
|The timeout before a pagination request fails.
274+
275+
|time_zone
276+
|`Z` (or `UTC`)
277+
|Time-zone in ISO 8601 used for executing the query on the server.
278+
More information available https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html[here].
279+
280+
|===
281+
282+
Do note that most parameters (outside the timeout 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.
283+
That's because the query has already been executed and the calls are simply about returning the found results - thus the parameters are simply ignored.

docs/reference/sql/endpoints/translate.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
== SQL Translate API
55

66
The SQL Translate API accepts SQL in a JSON document and translates it
7-
into native Elasticsearch queries. For example:
7+
into native {es} queries. For example:
88

99
[source,js]
1010
--------------------------------------------------

0 commit comments

Comments
 (0)