rest.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[sql-rest]]
  4. == SQL REST API
  5. The SQL REST API accepts SQL in a JSON document, executes it,
  6. and returns the results.
  7. For example:
  8. [source,js]
  9. --------------------------------------------------
  10. POST /_sql?format=txt
  11. {
  12. "query": "SELECT * FROM library ORDER BY page_count DESC LIMIT 5"
  13. }
  14. --------------------------------------------------
  15. // CONSOLE
  16. // TEST[setup:library]
  17. Which returns:
  18. [source,text]
  19. --------------------------------------------------
  20. author | name | page_count | release_date
  21. -----------------+--------------------+---------------+------------------------
  22. Peter F. Hamilton|Pandora's Star |768 |2004-03-02T00:00:00.000Z
  23. Vernor Vinge |A Fire Upon the Deep|613 |1992-06-01T00:00:00.000Z
  24. Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
  25. Alastair Reynolds|Revelation Space |585 |2000-03-15T00:00:00.000Z
  26. James S.A. Corey |Leviathan Wakes |561 |2011-06-02T00:00:00.000Z
  27. --------------------------------------------------
  28. // TESTRESPONSE[s/\|/\\|/ s/\+/\\+/]
  29. // TESTRESPONSE[non_json]
  30. [[sql-kibana-console]]
  31. .Using Kibana Console
  32. If you are using {kibana-ref}/console-kibana.html[Kibana Console].
  33. (which is highly recommended), take advantage of the
  34. triple quotes `"""` when creating the query. This not only automatically escapes double
  35. quotes (`"`) inside the query string but also support multi-line as shown below:
  36. image:images/sql/rest/console-triple-quotes.png[]
  37. [[sql-rest-format]]
  38. [float]
  39. === Response Data Formats
  40. While the textual format is nice for humans, computers prefer something
  41. more structured.
  42. {es-sql} can return the data in the following formats which can be set
  43. either through the `format` property in the URL or by setting the `Accept` HTTP header:
  44. NOTE: The URL parameter takes precedence over the `Accept` HTTP header.
  45. If neither is specified then the response is returned in the same format as the request.
  46. [cols="^m,^4m,^8"]
  47. |===
  48. s|format
  49. s|`Accept` HTTP header
  50. s|Description
  51. 3+h| Human Readable
  52. |csv
  53. |text/csv
  54. |https://en.wikipedia.org/wiki/Comma-separated_values[Comma-separated values]
  55. |json
  56. |application/json
  57. |https://www.json.org/[JSON] (JavaScript Object Notation) human-readable format
  58. |tsv
  59. |text/tab-separated-values
  60. |https://en.wikipedia.org/wiki/Tab-separated_values[Tab-separated values]
  61. |txt
  62. |text/plain
  63. |CLI-like representation
  64. |yaml
  65. |application/yaml
  66. |https://en.wikipedia.org/wiki/YAML[YAML] (YAML Ain't Markup Language) human-readable format
  67. 3+h| Binary Formats
  68. |cbor
  69. |application/cbor
  70. |http://cbor.io/[Concise Binary Object Representation]
  71. |smile
  72. |application/smile
  73. |https://en.wikipedia.org/wiki/Smile_(data_interchange_format)[Smile] binary data format similar to CBOR
  74. |===
  75. [source,js]
  76. --------------------------------------------------
  77. POST /_sql?format=json
  78. {
  79. "query": "SELECT * FROM library ORDER BY page_count DESC",
  80. "fetch_size": 5
  81. }
  82. --------------------------------------------------
  83. // CONSOLE
  84. // TEST[setup:library]
  85. Which returns:
  86. [source,js]
  87. --------------------------------------------------
  88. {
  89. "columns": [
  90. {"name": "author", "type": "text"},
  91. {"name": "name", "type": "text"},
  92. {"name": "page_count", "type": "short"},
  93. {"name": "release_date", "type": "datetime"}
  94. ],
  95. "rows": [
  96. ["Peter F. Hamilton", "Pandora's Star", 768, "2004-03-02T00:00:00.000Z"],
  97. ["Vernor Vinge", "A Fire Upon the Deep", 613, "1992-06-01T00:00:00.000Z"],
  98. ["Frank Herbert", "Dune", 604, "1965-06-01T00:00:00.000Z"],
  99. ["Alastair Reynolds", "Revelation Space", 585, "2000-03-15T00:00:00.000Z"],
  100. ["James S.A. Corey", "Leviathan Wakes", 561, "2011-06-02T00:00:00.000Z"]
  101. ],
  102. "cursor": "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl+v///w8="
  103. }
  104. --------------------------------------------------
  105. // TESTRESPONSE[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl\+v\/\/\/w8=/$body.cursor/]
  106. [[sql-pagination]]
  107. [float]
  108. === Paginating through a large response
  109. Using the example above, one can continue to the next page by sending back the `cursor` field. In
  110. case of text format the cursor is returned as `Cursor` http header.
  111. [source,js]
  112. --------------------------------------------------
  113. POST /_sql?format=json
  114. {
  115. "cursor": "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWYUpOYklQMHhRUEtld3RsNnFtYU1hQQ==:BAFmBGRhdGUBZgVsaWtlcwFzB21lc3NhZ2UBZgR1c2Vy9f///w8="
  116. }
  117. --------------------------------------------------
  118. // CONSOLE
  119. // TEST[continued]
  120. // TEST[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWYUpOYklQMHhRUEtld3RsNnFtYU1hQQ==:BAFmBGRhdGUBZgVsaWtlcwFzB21lc3NhZ2UBZgR1c2Vy9f\/\/\/w8=/$body.cursor/]
  121. Which looks like:
  122. [source,js]
  123. --------------------------------------------------
  124. {
  125. "rows" : [
  126. ["Dan Simmons", "Hyperion", 482, "1989-05-26T00:00:00.000Z"],
  127. ["Iain M. Banks", "Consider Phlebas", 471, "1987-04-23T00:00:00.000Z"],
  128. ["Neal Stephenson", "Snow Crash", 470, "1992-06-01T00:00:00.000Z"],
  129. ["Frank Herbert", "God Emperor of Dune", 454, "1981-05-28T00:00:00.000Z"],
  130. ["Frank Herbert", "Children of Dune", 408, "1976-04-21T00:00:00.000Z"]
  131. ],
  132. "cursor" : "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWODRMaXBUaVlRN21iTlRyWHZWYUdrdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl9f///w8="
  133. }
  134. --------------------------------------------------
  135. // TESTRESPONSE[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWODRMaXBUaVlRN21iTlRyWHZWYUdrdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl9f\/\/\/w8=/$body.cursor/]
  136. Note that the `columns` object is only part of the first page.
  137. You've reached the last page when there is no `cursor` returned
  138. in the results. Like Elasticsearch's <<search-request-scroll,scroll>>,
  139. SQL may keep state in Elasticsearch to support the cursor. Unlike
  140. scroll, receiving the last page is enough to guarantee that the
  141. Elasticsearch state is cleared.
  142. To clear the state earlier, you can use the clear cursor command:
  143. [source,js]
  144. --------------------------------------------------
  145. POST /_sql/close
  146. {
  147. "cursor": "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWYUpOYklQMHhRUEtld3RsNnFtYU1hQQ==:BAFmBGRhdGUBZgVsaWtlcwFzB21lc3NhZ2UBZgR1c2Vy9f///w8="
  148. }
  149. --------------------------------------------------
  150. // CONSOLE
  151. // TEST[continued]
  152. // TEST[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWYUpOYklQMHhRUEtld3RsNnFtYU1hQQ==:BAFmBGRhdGUBZgVsaWtlcwFzB21lc3NhZ2UBZgR1c2Vy9f\/\/\/w8=/$body.cursor/]
  153. Which will like return the
  154. [source,js]
  155. --------------------------------------------------
  156. {
  157. "succeeded" : true
  158. }
  159. --------------------------------------------------
  160. // TESTRESPONSE
  161. [[sql-rest-filtering]]
  162. [float]
  163. === Filtering using {es} query DSL
  164. You can filter the results that SQL will run on using a standard
  165. {es} query DSL by specifying the query in the filter
  166. parameter.
  167. [source,js]
  168. --------------------------------------------------
  169. POST /_sql?format=txt
  170. {
  171. "query": "SELECT * FROM library ORDER BY page_count DESC",
  172. "filter": {
  173. "range": {
  174. "page_count": {
  175. "gte" : 100,
  176. "lte" : 200
  177. }
  178. }
  179. },
  180. "fetch_size": 5
  181. }
  182. --------------------------------------------------
  183. // CONSOLE
  184. // TEST[setup:library]
  185. Which returns:
  186. [source,text]
  187. --------------------------------------------------
  188. author | name | page_count | release_date
  189. ---------------+------------------------------------+---------------+------------------------
  190. Douglas Adams |The Hitchhiker's Guide to the Galaxy|180 |1979-10-12T00:00:00.000Z
  191. --------------------------------------------------
  192. // TESTRESPONSE[s/\|/\\|/ s/\+/\\+/]
  193. // TESTRESPONSE[non_json]
  194. [[sql-rest-columnar]]
  195. [float]
  196. === Columnar results
  197. The most well known way of displaying the results of an SQL query result in general is the one where each
  198. individual record/document represents one line/row. For certain formats, {es-sql} can return the results
  199. in a columnar fashion: one row represents all the values of a certain column from the current page of results.
  200. The following formats can be returned in columnar orientation: `json`, `yaml`, `cbor` and `smile`.
  201. [source,js]
  202. --------------------------------------------------
  203. POST /_sql?format=json
  204. {
  205. "query": "SELECT * FROM library ORDER BY page_count DESC",
  206. "fetch_size": 5,
  207. "columnar": true
  208. }
  209. --------------------------------------------------
  210. // CONSOLE
  211. // TEST[setup:library]
  212. Which returns:
  213. [source,js]
  214. --------------------------------------------------
  215. {
  216. "columns": [
  217. {"name": "author", "type": "text"},
  218. {"name": "name", "type": "text"},
  219. {"name": "page_count", "type": "short"},
  220. {"name": "release_date", "type": "datetime"}
  221. ],
  222. "values": [
  223. ["Peter F. Hamilton", "Vernor Vinge", "Frank Herbert", "Alastair Reynolds", "James S.A. Corey"],
  224. ["Pandora's Star", "A Fire Upon the Deep", "Dune", "Revelation Space", "Leviathan Wakes"],
  225. [768, 613, 604, 585, 561],
  226. ["2004-03-02T00:00:00.000Z", "1992-06-01T00:00:00.000Z", "1965-06-01T00:00:00.000Z", "2000-03-15T00:00:00.000Z", "2011-06-02T00:00:00.000Z"]
  227. ],
  228. "cursor": "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl+v///w8="
  229. }
  230. --------------------------------------------------
  231. // TESTRESPONSE[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl\+v\/\/\/w8=/$body.cursor/]
  232. Any subsequent calls using a `cursor` still have to contain the `columnar` parameter to preserve the orientation,
  233. meaning the initial query will not _remember_ the columnar option.
  234. [source,js]
  235. --------------------------------------------------
  236. POST /_sql?format=json
  237. {
  238. "cursor": "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl+v///w8=",
  239. "columnar": true
  240. }
  241. --------------------------------------------------
  242. // CONSOLE
  243. // TEST[continued]
  244. // TEST[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl\+v\/\/\/w8=/$body.cursor/]
  245. Which looks like:
  246. [source,js]
  247. --------------------------------------------------
  248. {
  249. "values": [
  250. ["Dan Simmons", "Iain M. Banks", "Neal Stephenson", "Frank Herbert", "Frank Herbert"],
  251. ["Hyperion", "Consider Phlebas", "Snow Crash", "God Emperor of Dune", "Children of Dune"],
  252. [482, 471, 470, 454, 408],
  253. ["1989-05-26T00:00:00.000Z", "1987-04-23T00:00:00.000Z", "1992-06-01T00:00:00.000Z", "1981-05-28T00:00:00.000Z", "1976-04-21T00:00:00.000Z"]
  254. ],
  255. "cursor": "46ToAwFzQERYRjFaWEo1UVc1a1JtVjBZMmdCQUFBQUFBQUFBQUVXWjBaNlFXbzNOV0pVY21Wa1NUZDJhV2t3V2xwblp3PT3/////DwQBZgZhdXRob3IBBHRleHQAAAFmBG5hbWUBBHRleHQAAAFmCnBhZ2VfY291bnQBBGxvbmcBAAFmDHJlbGVhc2VfZGF0ZQEIZGF0ZXRpbWUBAAEP"
  256. }
  257. --------------------------------------------------
  258. // TESTRESPONSE[s/46ToAwFzQERYRjFaWEo1UVc1a1JtVjBZMmdCQUFBQUFBQUFBQUVXWjBaNlFXbzNOV0pVY21Wa1NUZDJhV2t3V2xwblp3PT3\/\/\/\/\/DwQBZgZhdXRob3IBBHRleHQAAAFmBG5hbWUBBHRleHQAAAFmCnBhZ2VfY291bnQBBGxvbmcBAAFmDHJlbGVhc2VfZGF0ZQEIZGF0ZXRpbWUBAAEP/$body.cursor/]
  259. [[sql-rest-fields]]
  260. [float]
  261. === Supported REST parameters
  262. In addition to the `query` and `fetch_size`, a request a number of user-defined fields for specifying
  263. the request time-outs or localization information (such as timezone).
  264. The table below lists the supported parameters:
  265. [cols="^m,^m,^5"]
  266. |===
  267. s|name
  268. s|Default value
  269. s|Description
  270. |query
  271. |Mandatory
  272. |SQL query to execute
  273. |fetch_size
  274. |1000
  275. |The maximum number of rows (or entries) to return in one response
  276. |filter
  277. |none
  278. |Optional {es} query DSL for additional <<sql-rest-filtering, filtering>>.
  279. |request_timeout
  280. |90s
  281. |The timeout before the request fails.
  282. |page_timeout
  283. |45s
  284. |The timeout before a pagination request fails.
  285. |time_zone
  286. |`Z` (or `UTC`)
  287. |Time-zone in ISO 8601 used for executing the query on the server.
  288. More information available https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html[here].
  289. |columnar
  290. |false
  291. |Return the results in a columnar fashion, rather than row-based fashion. Valid for `json`, `yaml`, `cbor` and `smile`.
  292. |field_multi_value_leniency
  293. |false
  294. |Throw an exception when encountering multiple values for a field (default) or be lenient and return the first value from the list (without any guarantees of what that will be - typically the first in natural ascending order).
  295. |index_include_frozen
  296. |false
  297. |Whether to include <<frozen-indices, frozen-indices>> in the query execution or not (default).
  298. |===
  299. Do note that most parameters (outside the timeout and `columnar` 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.
  300. 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.