Skip to content

Commit 6b21cac

Browse files
committed
Restructure the SQL Language section to have proper sub-sections (#43007)
Rest docs page update - have the section be on separate pages - add an Overview page - add other formats examples (cherry picked from commit 309bd69)
1 parent 0ea18b2 commit 6b21cac

File tree

5 files changed

+167
-30
lines changed

5 files changed

+167
-30
lines changed

docs/reference/sql/endpoints/rest.asciidoc

Lines changed: 146 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
[[sql-rest]]
44
== SQL REST API
55

6+
* <<sql-rest-overview>>
7+
* <<sql-rest-format>>
8+
* <<sql-pagination>>
9+
* <<sql-rest-filtering>>
10+
* <<sql-rest-columnar>>
11+
* <<sql-rest-fields>>
12+
13+
[[sql-rest-overview]]
14+
=== Overview
15+
616
The SQL REST API accepts SQL in a JSON document, executes it,
717
and returns the results.
818
For example:
@@ -34,14 +44,13 @@ James S.A. Corey |Leviathan Wakes |561 |2011-06-02T00:00:00.000Z
3444

3545
[[sql-kibana-console]]
3646
.Using Kibana Console
37-
If you are using {kibana-ref}/console-kibana.html[Kibana Console].
47+
If you are using {kibana-ref}/console-kibana.html[Kibana Console]
3848
(which is highly recommended), take advantage of the
3949
triple quotes `"""` when creating the query. This not only automatically escapes double
4050
quotes (`"`) inside the query string but also support multi-line as shown below:
4151
image:images/sql/rest/console-triple-quotes.png[]
4252

4353
[[sql-rest-format]]
44-
[float]
4554
=== Response Data Formats
4655

4756
While the textual format is nice for humans, computers prefer something
@@ -94,6 +103,35 @@ s|Description
94103

95104
|===
96105

106+
Here are some examples for the human readable formats:
107+
108+
==== CSV
109+
110+
[source,js]
111+
--------------------------------------------------
112+
POST /_sql?format=csv
113+
{
114+
"query": "SELECT * FROM library ORDER BY page_count DESC",
115+
"fetch_size": 5
116+
}
117+
--------------------------------------------------
118+
// CONSOLE
119+
// TEST[setup:library]
120+
121+
Which returns:
122+
123+
[source,text]
124+
--------------------------------------------------
125+
author,name,page_count,release_date
126+
Peter F. Hamilton,Pandora's Star,768,2004-03-02T00:00:00.000Z
127+
Vernor Vinge,A Fire Upon the Deep,613,1992-06-01T00:00:00.000Z
128+
Frank Herbert,Dune,604,1965-06-01T00:00:00.000Z
129+
Alastair Reynolds,Revelation Space,585,2000-03-15T00:00:00.000Z
130+
James S.A. Corey,Leviathan Wakes,561,2011-06-02T00:00:00.000Z
131+
--------------------------------------------------
132+
// TESTRESPONSE[_cat]
133+
134+
==== JSON
97135

98136
[source,js]
99137
--------------------------------------------------
@@ -129,8 +167,113 @@ Which returns:
129167
--------------------------------------------------
130168
// TESTRESPONSE[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl\+v\/\/\/w8=/$body.cursor/]
131169

170+
==== TSV
171+
172+
[source,js]
173+
--------------------------------------------------
174+
POST /_sql?format=tsv
175+
{
176+
"query": "SELECT * FROM library ORDER BY page_count DESC",
177+
"fetch_size": 5
178+
}
179+
--------------------------------------------------
180+
// CONSOLE
181+
// TEST[setup:library]
182+
183+
Which returns:
184+
185+
[source,text]
186+
--------------------------------------------------
187+
author name page_count release_date
188+
Peter F. Hamilton Pandora's Star 768 2004-03-02T00:00:00.000Z
189+
Vernor Vinge A Fire Upon the Deep 613 1992-06-01T00:00:00.000Z
190+
Frank Herbert Dune 604 1965-06-01T00:00:00.000Z
191+
Alastair Reynolds Revelation Space 585 2000-03-15T00:00:00.000Z
192+
James S.A. Corey Leviathan Wakes 561 2011-06-02T00:00:00.000Z
193+
--------------------------------------------------
194+
// TESTRESPONSE[s/\t/ /]
195+
// TESTRESPONSE[_cat]
196+
197+
==== TXT
198+
199+
[source,js]
200+
--------------------------------------------------
201+
POST /_sql?format=txt
202+
{
203+
"query": "SELECT * FROM library ORDER BY page_count DESC",
204+
"fetch_size": 5
205+
}
206+
--------------------------------------------------
207+
// CONSOLE
208+
// TEST[setup:library]
209+
210+
Which returns:
211+
212+
[source,text]
213+
--------------------------------------------------
214+
author | name | page_count | release_date
215+
-----------------+--------------------+---------------+------------------------
216+
Peter F. Hamilton|Pandora's Star |768 |2004-03-02T00:00:00.000Z
217+
Vernor Vinge |A Fire Upon the Deep|613 |1992-06-01T00:00:00.000Z
218+
Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
219+
Alastair Reynolds|Revelation Space |585 |2000-03-15T00:00:00.000Z
220+
James S.A. Corey |Leviathan Wakes |561 |2011-06-02T00:00:00.000Z
221+
--------------------------------------------------
222+
// TESTRESPONSE[s/\|/\\|/ s/\+/\\+/]
223+
// TESTRESPONSE[_cat]
224+
225+
==== YAML
226+
227+
[source,js]
228+
--------------------------------------------------
229+
POST /_sql?format=yaml
230+
{
231+
"query": "SELECT * FROM library ORDER BY page_count DESC",
232+
"fetch_size": 5
233+
}
234+
--------------------------------------------------
235+
// CONSOLE
236+
// TEST[setup:library]
237+
238+
Which returns:
239+
240+
[source,yaml]
241+
--------------------------------------------------
242+
columns:
243+
- name: "author"
244+
type: "text"
245+
- name: "name"
246+
type: "text"
247+
- name: "page_count"
248+
type: "short"
249+
- name: "release_date"
250+
type: "datetime"
251+
rows:
252+
- - "Peter F. Hamilton"
253+
- "Pandora's Star"
254+
- 768
255+
- "2004-03-02T00:00:00.000Z"
256+
- - "Vernor Vinge"
257+
- "A Fire Upon the Deep"
258+
- 613
259+
- "1992-06-01T00:00:00.000Z"
260+
- - "Frank Herbert"
261+
- "Dune"
262+
- 604
263+
- "1965-06-01T00:00:00.000Z"
264+
- - "Alastair Reynolds"
265+
- "Revelation Space"
266+
- 585
267+
- "2000-03-15T00:00:00.000Z"
268+
- - "James S.A. Corey"
269+
- "Leviathan Wakes"
270+
- 561
271+
- "2011-06-02T00:00:00.000Z"
272+
cursor: "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl+v///w8="
273+
--------------------------------------------------
274+
// TESTRESPONSE[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl\+v\/\/\/w8=/$body.cursor/]
275+
132276
[[sql-pagination]]
133-
[float]
134277
=== Paginating through a large response
135278

136279
Using the example above, one can continue to the next page by sending back the `cursor` field. In
@@ -198,7 +341,6 @@ Which will like return the
198341

199342

200343
[[sql-rest-filtering]]
201-
[float]
202344
=== Filtering using {es} query DSL
203345

204346
You can filter the results that SQL will run on using a standard
@@ -236,7 +378,6 @@ Douglas Adams |The Hitchhiker's Guide to the Galaxy|180 |1979-10-12T
236378
// TESTRESPONSE[non_json]
237379

238380
[[sql-rest-columnar]]
239-
[float]
240381
=== Columnar results
241382

242383
The most well known way of displaying the results of an SQL query result in general is the one where each
@@ -311,7 +452,6 @@ Which looks like:
311452
// TESTRESPONSE[s/46ToAwFzQERYRjFaWEo1UVc1a1JtVjBZMmdCQUFBQUFBQUFBQUVXWjBaNlFXbzNOV0pVY21Wa1NUZDJhV2t3V2xwblp3PT3\/\/\/\/\/DwQBZgZhdXRob3IBBHRleHQAAAFmBG5hbWUBBHRleHQAAAFmCnBhZ2VfY291bnQBBGxvbmcBAAFmDHJlbGVhc2VfZGF0ZQEIZGF0ZXRpbWUBAAEP/$body.cursor/]
312453

313454
[[sql-rest-fields]]
314-
[float]
315455
=== Supported REST parameters
316456

317457
In addition to the `query` and `fetch_size`, a request a number of user-defined fields for specifying

docs/reference/sql/language/data-types.asciidoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[role="xpack"]
22
[testenv="basic"]
33
[[sql-data-types]]
4-
== Data Types
4+
=== Data Types
55

66
[cols="^,^m,^,^"]
77

@@ -89,7 +89,7 @@ s|SQL precision
8989

9090
[[sql-multi-field]]
9191
[float]
92-
=== SQL and multi-fields
92+
==== SQL and multi-fields
9393

9494
A core concept in {es} is that of an `analyzed` field, that is a full-text value that is interpreted in order
9595
to be effectively indexed. These fields are of type <<text, `text`>> and are not used for sorting or aggregations as their actual value depends on the <<analyzer, `analyzer`>> used hence why {es} also offers the <<keyword, `keyword`>> type for storing the _exact_

docs/reference/sql/language/index-patterns.asciidoc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
[role="xpack"]
22
[testenv="basic"]
33
[[sql-index-patterns]]
4-
== Index patterns
4+
=== Index patterns
55

66
{es-sql} supports two types of patterns for matching multiple indices or tables:
77

8-
* {es} multi-index
8+
[[sql-index-patterns-multi]]
9+
[float]
10+
==== {es} multi-index
911

1012
The {es} notation for enumerating, including or excluding <<multi-index,multi index syntax>>
1113
is supported _as long_ as it is quoted or escaped as a table identifier.
@@ -33,7 +35,9 @@ include-tagged::{sql-specs}/docs/docs.csv-spec[fromTablePatternQuoted]
3335

3436
NOTE: There is the restriction that all resolved concrete tables have the exact same mapping.
3537

36-
* SQL `LIKE` notation
38+
[[sql-index-patterns-like]]
39+
[float]
40+
==== SQL `LIKE` notation
3741

3842
The common `LIKE` statement (including escaping if needed) to match a wildcard pattern, based on one `_`
3943
or multiple `%` characters.

docs/reference/sql/language/syntax/commands/index.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[role="xpack"]
22
[testenv="basic"]
33
[[sql-commands]]
4-
== SQL Commands
4+
=== SQL Commands
55

66
This section contains the list of SQL commands supported by {es-sql} along with their syntax:
77

docs/reference/sql/language/syntax/lexic/index.asciidoc

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[role="xpack"]
22
[testenv="basic"]
33
[[sql-lexical-structure]]
4-
== Lexical Structure
4+
=== Lexical Structure
55

66
This section covers the major lexical structure of SQL, which for the most part, is going to resemble that of ANSI SQL itself hence why low-levels details are not discussed in depth.
77

@@ -10,8 +10,7 @@ This section covers the major lexical structure of SQL, which for the most part,
1010
A token can be a __key word__, an _identifier_ (_quoted_ or _unquoted_), a _literal_ (or constant) or a special character symbol (typically a delimiter). Tokens are typically separated by whitespace (be it space, tab) though in some cases, where there is no ambiguity (typically due to a character symbol) this is not needed - however for readability purposes this should be avoided.
1111

1212
[[sql-syntax-keywords]]
13-
[float]
14-
=== Key Words
13+
==== Key Words
1514

1615
Take the following example:
1716

@@ -35,8 +34,7 @@ Identifiers however are not - as {es} is case sensitive, {es-sql} uses the recei
3534
To help differentiate between the two, through-out the documentation the SQL key words are upper-cased a convention we find increases readability and thus recommend to others.
3635

3736
[[sql-syntax-identifiers]]
38-
[float]
39-
=== Identifiers
37+
==== Identifiers
4038

4139
Identifiers can be of two types: __quoted__ and __unquoted__:
4240

@@ -59,14 +57,13 @@ The first identifier from needs to quoted as otherwise it clashes with the `FROM
5957
Hence why in general, *especially* when dealing with user input it is *highly* recommended to use quotes for identifiers. It adds minimal increase to your queries and in return offers clarity and disambiguation.
6058

6159
[[sql-syntax-literals]]
62-
[float]
63-
=== Literals (Constants)
60+
==== Literals (Constants)
6461

6562
{es-sql} supports two kind of __implicitly-typed__ literals: strings and numbers.
6663

6764
[[sql-syntax-string-literals]]
6865
[float]
69-
==== String Literals
66+
===== String Literals
7067

7168
A string literal is an arbitrary number of characters bounded by single quotes `'`: `'Giant Robot'`.
7269
To include a single quote in the string, escape it using another single quote: `'Captain EO''s Voyage'`.
@@ -75,7 +72,7 @@ NOTE: An escaped single quote is *not* a double quote (`"`), but a single quote
7572

7673
[sql-syntax-numeric-literals]
7774
[float]
78-
==== Numeric Literals
75+
===== Numeric Literals
7976

8077
Numeric literals are accepted both in decimal and scientific notation with exponent marker (`e` or `E`), starting either with a digit or decimal point `.`:
8178

@@ -92,7 +89,7 @@ Numeric literals that contain a decimal point are always interpreted as being of
9289

9390
[[sql-syntax-generic-literals]]
9491
[float]
95-
==== Generic Literals
92+
===== Generic Literals
9693

9794
When dealing with arbitrary type literal, one creates the object by casting, typically, the string representation to the desired type. This can be achieved through the dedicated <<sql-operators-cast, cast operator>> and <<sql-functions-type-conversion, functions>>:
9895

@@ -106,8 +103,7 @@ CONVERT('10.0.0.1', IP) -- cast '10.0.0.1' to an IP
106103
Do note that {es-sql} provides functions that out of the box return popular literals (like `E()`) or provide dedicated parsing for certain strings.
107104

108105
[[sql-syntax-single-vs-double-quotes]]
109-
[float]
110-
=== Single vs Double Quotes
106+
==== Single vs Double Quotes
111107

112108
It is worth pointing out that in SQL, single quotes `'` and double quotes `"` have different meaning and *cannot* be used interchangeably.
113109
Single quotes are used to declare a <<sql-syntax-string-literals, string literal>> while double quotes for <<sql-syntax-identifiers, identifiers>>.
@@ -126,8 +122,7 @@ SELECT "first_name" <1>
126122
<2> Single quotes `'` used for a string literal
127123

128124
[[sql-syntax-special-chars]]
129-
[float]
130-
=== Special characters
125+
==== Special characters
131126

132127
A few characters that are not alphanumeric have a dedicated meaning different from that of an operator. For completeness these are specified below:
133128

@@ -146,8 +141,7 @@ s|Description
146141
|===
147142

148143
[[sql-syntax-operators]]
149-
[float]
150-
=== Operators
144+
==== Operators
151145

152146
Most operators in {es-sql} have the same precedence and are left-associative. As this is done at parsing time, parenthesis need to be used to enforce a different precedence.
153147

@@ -205,8 +199,7 @@ s|Description
205199

206200

207201
[[sql-syntax-comments]]
208-
[float]
209-
=== Comments
202+
==== Comments
210203

211204
{es-sql} allows comments which are sequence of characters ignored by the parsers.
212205

0 commit comments

Comments
 (0)