|
3 | 3 | [[sql-rest]] |
4 | 4 | == SQL REST API |
5 | 5 |
|
| 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 | + |
6 | 16 | The SQL REST API accepts SQL in a JSON document, executes it, |
7 | 17 | and returns the results. |
8 | 18 | For example: |
@@ -34,14 +44,13 @@ James S.A. Corey |Leviathan Wakes |561 |2011-06-02T00:00:00.000Z |
34 | 44 |
|
35 | 45 | [[sql-kibana-console]] |
36 | 46 | .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] |
38 | 48 | (which is highly recommended), take advantage of the |
39 | 49 | triple quotes `"""` when creating the query. This not only automatically escapes double |
40 | 50 | quotes (`"`) inside the query string but also support multi-line as shown below: |
41 | 51 | image:images/sql/rest/console-triple-quotes.png[] |
42 | 52 |
|
43 | 53 | [[sql-rest-format]] |
44 | | -[float] |
45 | 54 | === Response Data Formats |
46 | 55 |
|
47 | 56 | While the textual format is nice for humans, computers prefer something |
@@ -94,6 +103,35 @@ s|Description |
94 | 103 |
|
95 | 104 | |=== |
96 | 105 |
|
| 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 |
97 | 135 |
|
98 | 136 | [source,js] |
99 | 137 | -------------------------------------------------- |
@@ -129,8 +167,113 @@ Which returns: |
129 | 167 | -------------------------------------------------- |
130 | 168 | // TESTRESPONSE[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl\+v\/\/\/w8=/$body.cursor/] |
131 | 169 |
|
| 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 | + |
132 | 276 | [[sql-pagination]] |
133 | | -[float] |
134 | 277 | === Paginating through a large response |
135 | 278 |
|
136 | 279 | 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 |
198 | 341 |
|
199 | 342 |
|
200 | 343 | [[sql-rest-filtering]] |
201 | | -[float] |
202 | 344 | === Filtering using {es} query DSL |
203 | 345 |
|
204 | 346 | 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 |
236 | 378 | // TESTRESPONSE[non_json] |
237 | 379 |
|
238 | 380 | [[sql-rest-columnar]] |
239 | | -[float] |
240 | 381 | === Columnar results |
241 | 382 |
|
242 | 383 | 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: |
311 | 452 | // TESTRESPONSE[s/46ToAwFzQERYRjFaWEo1UVc1a1JtVjBZMmdCQUFBQUFBQUFBQUVXWjBaNlFXbzNOV0pVY21Wa1NUZDJhV2t3V2xwblp3PT3\/\/\/\/\/DwQBZgZhdXRob3IBBHRleHQAAAFmBG5hbWUBBHRleHQAAAFmCnBhZ2VfY291bnQBBGxvbmcBAAFmDHJlbGVhc2VfZGF0ZQEIZGF0ZXRpbWUBAAEP/$body.cursor/] |
312 | 453 |
|
313 | 454 | [[sql-rest-fields]] |
314 | | -[float] |
315 | 455 | === Supported REST parameters |
316 | 456 |
|
317 | 457 | In addition to the `query` and `fetch_size`, a request a number of user-defined fields for specifying |
|
0 commit comments