Skip to content

Commit 1cbbe82

Browse files
committed
Restructure the SQL Language section to have proper sub-sections (elastic#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 a5326a2 commit 1cbbe82

File tree

5 files changed

+166
-29
lines changed

5 files changed

+166
-29
lines changed

docs/reference/sql/endpoints/rest.asciidoc

Lines changed: 145 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
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-fields>>
11+
12+
[[sql-rest-overview]]
13+
=== Overview
14+
615
The SQL REST API accepts SQL in a JSON document, executes it,
716
and returns the results.
817
For example:
@@ -34,14 +43,13 @@ James S.A. Corey |Leviathan Wakes |561 |2011-06-02T00:00:00.000Z
3443

3544
[[sql-kibana-console]]
3645
.Using Kibana Console
37-
If you are using {kibana-ref}/console-kibana.html[Kibana Console].
46+
If you are using {kibana-ref}/console-kibana.html[Kibana Console]
3847
(which is highly recommended), take advantage of the
3948
triple quotes `"""` when creating the query. This not only automatically escapes double
4049
quotes (`"`) inside the query string but also support multi-line as shown below:
4150
image:images/sql/rest/console-triple-quotes.png[]
4251

4352
[[sql-rest-format]]
44-
[float]
4553
=== Response Data Formats
4654

4755
While the textual format is nice for humans, computers prefer something
@@ -94,6 +102,35 @@ s|Description
94102

95103
|===
96104

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

98135
[source,js]
99136
--------------------------------------------------
@@ -129,8 +166,113 @@ Which returns:
129166
--------------------------------------------------
130167
// TESTRESPONSE[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl\+v\/\/\/w8=/$body.cursor/]
131168

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

136278
Using the example above, onu can continue to the next page by sending back the `cursor` field. In
@@ -198,7 +340,6 @@ Which will like return the
198340

199341

200342
[[sql-rest-filtering]]
201-
[float]
202343
=== Filtering using {es} query DSL
203344

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

238379
[[sql-rest-fields]]
239-
[float]
240380
=== Supported REST parameters
241381

242382
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

@@ -85,7 +85,7 @@ s|SQL precision
8585

8686
[[sql-multi-field]]
8787
[float]
88-
=== SQL and multi-fields
88+
==== SQL and multi-fields
8989

9090
A core concept in {es} is that of an `analyzed` field, that is a full-text value that is interpreted in order
9191
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.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-functions-type-conversion, functions>>:
9895

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

107104
[[sql-syntax-single-vs-double-quotes]]
108-
[float]
109-
=== Single vs Double Quotes
105+
==== Single vs Double Quotes
110106

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

127123
[[sql-syntax-special-chars]]
128-
[float]
129-
=== Special characters
124+
==== Special characters
130125

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

@@ -145,8 +140,7 @@ s|Description
145140
|===
146141

147142
[[sql-syntax-operators]]
148-
[float]
149-
=== Operators
143+
==== Operators
150144

151145
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.
152146

@@ -200,8 +194,7 @@ s|Description
200194

201195

202196
[[sql-syntax-comments]]
203-
[float]
204-
=== Comments
197+
==== Comments
205198

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

0 commit comments

Comments
 (0)