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