rest.asciidoc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[sql-rest]]
  4. == SQL REST API
  5. * <<sql-rest-overview>>
  6. * <<sql-rest-format>>
  7. * <<sql-pagination>>
  8. * <<sql-rest-filtering>>
  9. * <<sql-rest-columnar>>
  10. * <<sql-rest-params>>
  11. * <<sql-runtime-fields>>
  12. * <<sql-rest-fields>>
  13. [[sql-rest-overview]]
  14. === Overview
  15. The SQL REST API accepts SQL in a JSON document, executes it,
  16. and returns the results.
  17. For example:
  18. [source,console]
  19. --------------------------------------------------
  20. POST /_sql?format=txt
  21. {
  22. "query": "SELECT * FROM library ORDER BY page_count DESC LIMIT 5"
  23. }
  24. --------------------------------------------------
  25. // TEST[setup:library]
  26. Which returns:
  27. [source,text]
  28. --------------------------------------------------
  29. author | name | page_count | release_date
  30. -----------------+--------------------+---------------+------------------------
  31. Peter F. Hamilton|Pandora's Star |768 |2004-03-02T00:00:00.000Z
  32. Vernor Vinge |A Fire Upon the Deep|613 |1992-06-01T00:00:00.000Z
  33. Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
  34. Alastair Reynolds|Revelation Space |585 |2000-03-15T00:00:00.000Z
  35. James S.A. Corey |Leviathan Wakes |561 |2011-06-02T00:00:00.000Z
  36. --------------------------------------------------
  37. // TESTRESPONSE[s/\|/\\|/ s/\+/\\+/]
  38. // TESTRESPONSE[non_json]
  39. [[sql-kibana-console]]
  40. [TIP]
  41. .Using Kibana Console
  42. ====
  43. If you are using {kibana-ref}/console-kibana.html[Kibana Console]
  44. (which is highly recommended), take advantage of the
  45. triple quotes `"""` when creating the query. This not only automatically escapes double
  46. quotes (`"`) inside the query string but also support multi-line as shown below:
  47. image:images/sql/rest/console-triple-quotes.png[]
  48. ====
  49. [[sql-rest-format]]
  50. === Response Data Formats
  51. While the textual format is nice for humans, computers prefer something
  52. more structured.
  53. {es-sql} can return the data in the following formats which can be set
  54. either through the `format` property in the URL or by setting the `Accept` HTTP header:
  55. NOTE: The URL parameter takes precedence over the `Accept` HTTP header.
  56. If neither is specified then the response is returned in the same format as the request.
  57. [cols="^m,^4m,^8"]
  58. |===
  59. s|format
  60. s|`Accept` HTTP header
  61. s|Description
  62. 3+h| Human Readable
  63. |csv
  64. |text/csv
  65. |{wikipedia}/Comma-separated_values[Comma-separated values]
  66. |json
  67. |application/json
  68. |https://www.json.org/[JSON] (JavaScript Object Notation) human-readable format
  69. |tsv
  70. |text/tab-separated-values
  71. |{wikipedia}/Tab-separated_values[Tab-separated values]
  72. |txt
  73. |text/plain
  74. |CLI-like representation
  75. |yaml
  76. |application/yaml
  77. |{wikipedia}/YAML[YAML] (YAML Ain't Markup Language) human-readable format
  78. 3+h| Binary Formats
  79. |cbor
  80. |application/cbor
  81. |https://cbor.io/[Concise Binary Object Representation]
  82. |smile
  83. |application/smile
  84. |{wikipedia}/Smile_(data_interchange_format)[Smile] binary data format similar to CBOR
  85. |===
  86. The `CSV` format accepts a formatting URL query attribute, `delimiter`, which indicates which character should be used to separate the CSV
  87. values. It defaults to comma (`,`) and cannot take any of the following values: double quote (`"`), carriage-return (`\r`) and new-line (`\n`).
  88. The tab (`\t`) can also not be used, the `tsv` format needs to be used instead.
  89. Here are some examples for the human readable formats:
  90. ==== CSV
  91. [source,console]
  92. --------------------------------------------------
  93. POST /_sql?format=csv
  94. {
  95. "query": "SELECT * FROM library ORDER BY page_count DESC",
  96. "fetch_size": 5
  97. }
  98. --------------------------------------------------
  99. // TEST[setup:library]
  100. which returns:
  101. [source,text]
  102. --------------------------------------------------
  103. author,name,page_count,release_date
  104. Peter F. Hamilton,Pandora's Star,768,2004-03-02T00:00:00.000Z
  105. Vernor Vinge,A Fire Upon the Deep,613,1992-06-01T00:00:00.000Z
  106. Frank Herbert,Dune,604,1965-06-01T00:00:00.000Z
  107. Alastair Reynolds,Revelation Space,585,2000-03-15T00:00:00.000Z
  108. James S.A. Corey,Leviathan Wakes,561,2011-06-02T00:00:00.000Z
  109. --------------------------------------------------
  110. // TESTRESPONSE[non_json]
  111. or:
  112. [source,console]
  113. --------------------------------------------------
  114. POST /_sql?format=csv&delimiter=%3b
  115. {
  116. "query": "SELECT * FROM library ORDER BY page_count DESC",
  117. "fetch_size": 5
  118. }
  119. --------------------------------------------------
  120. // TEST[setup:library]
  121. which returns:
  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[non_json]
  132. ==== JSON
  133. [source,console]
  134. --------------------------------------------------
  135. POST /_sql?format=json
  136. {
  137. "query": "SELECT * FROM library ORDER BY page_count DESC",
  138. "fetch_size": 5
  139. }
  140. --------------------------------------------------
  141. // TEST[setup:library]
  142. Which returns:
  143. [source,console-result]
  144. --------------------------------------------------
  145. {
  146. "columns": [
  147. {"name": "author", "type": "text"},
  148. {"name": "name", "type": "text"},
  149. {"name": "page_count", "type": "short"},
  150. {"name": "release_date", "type": "datetime"}
  151. ],
  152. "rows": [
  153. ["Peter F. Hamilton", "Pandora's Star", 768, "2004-03-02T00:00:00.000Z"],
  154. ["Vernor Vinge", "A Fire Upon the Deep", 613, "1992-06-01T00:00:00.000Z"],
  155. ["Frank Herbert", "Dune", 604, "1965-06-01T00:00:00.000Z"],
  156. ["Alastair Reynolds", "Revelation Space", 585, "2000-03-15T00:00:00.000Z"],
  157. ["James S.A. Corey", "Leviathan Wakes", 561, "2011-06-02T00:00:00.000Z"]
  158. ],
  159. "cursor": "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl+v///w8="
  160. }
  161. --------------------------------------------------
  162. // TESTRESPONSE[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl\+v\/\/\/w8=/$body.cursor/]
  163. ==== TSV
  164. [source,console]
  165. --------------------------------------------------
  166. POST /_sql?format=tsv
  167. {
  168. "query": "SELECT * FROM library ORDER BY page_count DESC",
  169. "fetch_size": 5
  170. }
  171. --------------------------------------------------
  172. // TEST[setup:library]
  173. Which returns:
  174. [source,text]
  175. --------------------------------------------------
  176. author name page_count release_date
  177. Peter F. Hamilton Pandora's Star 768 2004-03-02T00:00:00.000Z
  178. Vernor Vinge A Fire Upon the Deep 613 1992-06-01T00:00:00.000Z
  179. Frank Herbert Dune 604 1965-06-01T00:00:00.000Z
  180. Alastair Reynolds Revelation Space 585 2000-03-15T00:00:00.000Z
  181. James S.A. Corey Leviathan Wakes 561 2011-06-02T00:00:00.000Z
  182. --------------------------------------------------
  183. // TESTRESPONSE[s/\t/ /]
  184. // TESTRESPONSE[non_json]
  185. ==== TXT
  186. [source,console]
  187. --------------------------------------------------
  188. POST /_sql?format=txt
  189. {
  190. "query": "SELECT * FROM library ORDER BY page_count DESC",
  191. "fetch_size": 5
  192. }
  193. --------------------------------------------------
  194. // TEST[setup:library]
  195. Which returns:
  196. [source,text]
  197. --------------------------------------------------
  198. author | name | page_count | release_date
  199. -----------------+--------------------+---------------+------------------------
  200. Peter F. Hamilton|Pandora's Star |768 |2004-03-02T00:00:00.000Z
  201. Vernor Vinge |A Fire Upon the Deep|613 |1992-06-01T00:00:00.000Z
  202. Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
  203. Alastair Reynolds|Revelation Space |585 |2000-03-15T00:00:00.000Z
  204. James S.A. Corey |Leviathan Wakes |561 |2011-06-02T00:00:00.000Z
  205. --------------------------------------------------
  206. // TESTRESPONSE[s/\|/\\|/ s/\+/\\+/]
  207. // TESTRESPONSE[non_json]
  208. ==== YAML
  209. [source,console]
  210. --------------------------------------------------
  211. POST /_sql?format=yaml
  212. {
  213. "query": "SELECT * FROM library ORDER BY page_count DESC",
  214. "fetch_size": 5
  215. }
  216. --------------------------------------------------
  217. // TEST[setup:library]
  218. Which returns:
  219. [source,yaml]
  220. --------------------------------------------------
  221. columns:
  222. - name: "author"
  223. type: "text"
  224. - name: "name"
  225. type: "text"
  226. - name: "page_count"
  227. type: "short"
  228. - name: "release_date"
  229. type: "datetime"
  230. rows:
  231. - - "Peter F. Hamilton"
  232. - "Pandora's Star"
  233. - 768
  234. - "2004-03-02T00:00:00.000Z"
  235. - - "Vernor Vinge"
  236. - "A Fire Upon the Deep"
  237. - 613
  238. - "1992-06-01T00:00:00.000Z"
  239. - - "Frank Herbert"
  240. - "Dune"
  241. - 604
  242. - "1965-06-01T00:00:00.000Z"
  243. - - "Alastair Reynolds"
  244. - "Revelation Space"
  245. - 585
  246. - "2000-03-15T00:00:00.000Z"
  247. - - "James S.A. Corey"
  248. - "Leviathan Wakes"
  249. - 561
  250. - "2011-06-02T00:00:00.000Z"
  251. cursor: "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl+v///w8="
  252. --------------------------------------------------
  253. // TESTRESPONSE[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl\+v\/\/\/w8=/$body.cursor/]
  254. [[sql-pagination]]
  255. === Paginating through a large response
  256. Using the example from the <<sql-rest-format,previous section>>, one can
  257. continue to the next page by sending back the cursor field. In the case of CSV, TSV and TXT
  258. formats, the cursor is returned in the `Cursor` HTTP header.
  259. [source,console]
  260. --------------------------------------------------
  261. POST /_sql?format=json
  262. {
  263. "cursor": "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWYUpOYklQMHhRUEtld3RsNnFtYU1hQQ==:BAFmBGRhdGUBZgVsaWtlcwFzB21lc3NhZ2UBZgR1c2Vy9f///w8="
  264. }
  265. --------------------------------------------------
  266. // TEST[continued]
  267. // TEST[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWYUpOYklQMHhRUEtld3RsNnFtYU1hQQ==:BAFmBGRhdGUBZgVsaWtlcwFzB21lc3NhZ2UBZgR1c2Vy9f\/\/\/w8=/$body.cursor/]
  268. Which looks like:
  269. [source,console-result]
  270. --------------------------------------------------
  271. {
  272. "rows" : [
  273. ["Dan Simmons", "Hyperion", 482, "1989-05-26T00:00:00.000Z"],
  274. ["Iain M. Banks", "Consider Phlebas", 471, "1987-04-23T00:00:00.000Z"],
  275. ["Neal Stephenson", "Snow Crash", 470, "1992-06-01T00:00:00.000Z"],
  276. ["Frank Herbert", "God Emperor of Dune", 454, "1981-05-28T00:00:00.000Z"],
  277. ["Frank Herbert", "Children of Dune", 408, "1976-04-21T00:00:00.000Z"]
  278. ],
  279. "cursor" : "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWODRMaXBUaVlRN21iTlRyWHZWYUdrdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl9f///w8="
  280. }
  281. --------------------------------------------------
  282. // TESTRESPONSE[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWODRMaXBUaVlRN21iTlRyWHZWYUdrdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl9f\/\/\/w8=/$body.cursor/]
  283. Note that the `columns` object is only part of the first page.
  284. You've reached the last page when there is no `cursor` returned
  285. in the results. Like Elasticsearch's <<scroll-search-results,scroll>>,
  286. SQL may keep state in Elasticsearch to support the cursor. Unlike
  287. scroll, receiving the last page is enough to guarantee that the
  288. Elasticsearch state is cleared.
  289. To clear the state earlier, you can use the clear cursor command:
  290. [source,console]
  291. --------------------------------------------------
  292. POST /_sql/close
  293. {
  294. "cursor": "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWYUpOYklQMHhRUEtld3RsNnFtYU1hQQ==:BAFmBGRhdGUBZgVsaWtlcwFzB21lc3NhZ2UBZgR1c2Vy9f///w8="
  295. }
  296. --------------------------------------------------
  297. // TEST[continued]
  298. // TEST[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWYUpOYklQMHhRUEtld3RsNnFtYU1hQQ==:BAFmBGRhdGUBZgVsaWtlcwFzB21lc3NhZ2UBZgR1c2Vy9f\/\/\/w8=/$body.cursor/]
  299. Which will like return the
  300. [source,console-result]
  301. --------------------------------------------------
  302. {
  303. "succeeded" : true
  304. }
  305. --------------------------------------------------
  306. [[sql-rest-filtering]]
  307. === Filtering using {es} Query DSL
  308. One can filter the results that SQL will run on using a standard
  309. {es} Query DSL by specifying the query in the filter
  310. parameter.
  311. [source,console]
  312. --------------------------------------------------
  313. POST /_sql?format=txt
  314. {
  315. "query": "SELECT * FROM library ORDER BY page_count DESC",
  316. "filter": {
  317. "range": {
  318. "page_count": {
  319. "gte" : 100,
  320. "lte" : 200
  321. }
  322. }
  323. },
  324. "fetch_size": 5
  325. }
  326. --------------------------------------------------
  327. // TEST[setup:library]
  328. Which returns:
  329. [source,text]
  330. --------------------------------------------------
  331. author | name | page_count | release_date
  332. ---------------+------------------------------------+---------------+------------------------
  333. Douglas Adams |The Hitchhiker's Guide to the Galaxy|180 |1979-10-12T00:00:00.000Z
  334. --------------------------------------------------
  335. // TESTRESPONSE[s/\|/\\|/ s/\+/\\+/]
  336. // TESTRESPONSE[non_json]
  337. [TIP]
  338. =================
  339. A useful and less obvious usage for standard Query DSL filtering is to search documents by a specific <<search-routing, routing key>>.
  340. Because {es-sql} does not support a `routing` parameter, one can specify a <<mapping-routing-field, `terms` filter for the `_routing` field>> instead:
  341. [source,console]
  342. --------------------------------------------------
  343. POST /_sql?format=txt
  344. {
  345. "query": "SELECT * FROM library",
  346. "filter": {
  347. "terms": {
  348. "_routing": ["abc"]
  349. }
  350. }
  351. }
  352. --------------------------------------------------
  353. // TEST[setup:library]
  354. =================
  355. [[sql-rest-columnar]]
  356. === Columnar results
  357. The most well known way of displaying the results of an SQL query result in general is the one where each
  358. individual record/document represents one line/row. For certain formats, {es-sql} can return the results
  359. in a columnar fashion: one row represents all the values of a certain column from the current page of results.
  360. The following formats can be returned in columnar orientation: `json`, `yaml`, `cbor` and `smile`.
  361. [source,console]
  362. --------------------------------------------------
  363. POST /_sql?format=json
  364. {
  365. "query": "SELECT * FROM library ORDER BY page_count DESC",
  366. "fetch_size": 5,
  367. "columnar": true
  368. }
  369. --------------------------------------------------
  370. // TEST[setup:library]
  371. Which returns:
  372. [source,console-result]
  373. --------------------------------------------------
  374. {
  375. "columns": [
  376. {"name": "author", "type": "text"},
  377. {"name": "name", "type": "text"},
  378. {"name": "page_count", "type": "short"},
  379. {"name": "release_date", "type": "datetime"}
  380. ],
  381. "values": [
  382. ["Peter F. Hamilton", "Vernor Vinge", "Frank Herbert", "Alastair Reynolds", "James S.A. Corey"],
  383. ["Pandora's Star", "A Fire Upon the Deep", "Dune", "Revelation Space", "Leviathan Wakes"],
  384. [768, 613, 604, 585, 561],
  385. ["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"]
  386. ],
  387. "cursor": "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl+v///w8="
  388. }
  389. --------------------------------------------------
  390. // TESTRESPONSE[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl\+v\/\/\/w8=/$body.cursor/]
  391. Any subsequent calls using a `cursor` still have to contain the `columnar` parameter to preserve the orientation,
  392. meaning the initial query will not _remember_ the columnar option.
  393. [source,console]
  394. --------------------------------------------------
  395. POST /_sql?format=json
  396. {
  397. "cursor": "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl+v///w8=",
  398. "columnar": true
  399. }
  400. --------------------------------------------------
  401. // TEST[continued]
  402. // TEST[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl\+v\/\/\/w8=/$body.cursor/]
  403. Which looks like:
  404. [source,console-result]
  405. --------------------------------------------------
  406. {
  407. "values": [
  408. ["Dan Simmons", "Iain M. Banks", "Neal Stephenson", "Frank Herbert", "Frank Herbert"],
  409. ["Hyperion", "Consider Phlebas", "Snow Crash", "God Emperor of Dune", "Children of Dune"],
  410. [482, 471, 470, 454, 408],
  411. ["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"]
  412. ],
  413. "cursor": "46ToAwFzQERYRjFaWEo1UVc1a1JtVjBZMmdCQUFBQUFBQUFBQUVXWjBaNlFXbzNOV0pVY21Wa1NUZDJhV2t3V2xwblp3PT3/////DwQBZgZhdXRob3IBBHRleHQAAAFmBG5hbWUBBHRleHQAAAFmCnBhZ2VfY291bnQBBGxvbmcBAAFmDHJlbGVhc2VfZGF0ZQEIZGF0ZXRpbWUBAAEP"
  414. }
  415. --------------------------------------------------
  416. // TESTRESPONSE[s/46ToAwFzQERYRjFaWEo1UVc1a1JtVjBZMmdCQUFBQUFBQUFBQUVXWjBaNlFXbzNOV0pVY21Wa1NUZDJhV2t3V2xwblp3PT3\/\/\/\/\/DwQBZgZhdXRob3IBBHRleHQAAAFmBG5hbWUBBHRleHQAAAFmCnBhZ2VfY291bnQBBGxvbmcBAAFmDHJlbGVhc2VfZGF0ZQEIZGF0ZXRpbWUBAAEP/$body.cursor/]
  417. [[sql-rest-params]]
  418. === Passing parameters to a query
  419. Using values in a query condition, for example, or in a `HAVING` statement can be done "inline",
  420. by integrating the value in the query string itself:
  421. [source,console]
  422. --------------------------------------------------
  423. POST /_sql?format=txt
  424. {
  425. "query": "SELECT YEAR(release_date) AS year FROM library WHERE page_count > 300 AND author = 'Frank Herbert' GROUP BY year HAVING COUNT(*) > 0"
  426. }
  427. --------------------------------------------------
  428. // TEST[setup:library]
  429. or it can be done by extracting the values in a separate list of parameters and using question mark placeholders (`?`) in the query string:
  430. [source,console]
  431. --------------------------------------------------
  432. POST /_sql?format=txt
  433. {
  434. "query": "SELECT YEAR(release_date) AS year FROM library WHERE page_count > ? AND author = ? GROUP BY year HAVING COUNT(*) > ?",
  435. "params": [300, "Frank Herbert", 0]
  436. }
  437. --------------------------------------------------
  438. // TEST[setup:library]
  439. [IMPORTANT]
  440. The recommended way of passing values to a query is with question mark placeholders, to avoid any attempts of hacking or SQL injection.
  441. [[sql-runtime-fields]]
  442. === Use runtime fields
  443. Use the `runtime_mappings` parameter to extract and create <<runtime,runtime
  444. fields>>, or columns, from existing ones during a search.
  445. The following search creates a `release_day_of_week` runtime field from
  446. `release_date` and returns it in the response.
  447. [source,console]
  448. ----
  449. POST _sql?format=txt
  450. {
  451. "runtime_mappings": {
  452. "release_day_of_week": {
  453. "type": "keyword",
  454. "script": """
  455. emit(doc['release_date'].value.dayOfWeekEnum.toString())
  456. """
  457. }
  458. },
  459. "query": """
  460. SELECT * FROM library WHERE page_count > 300 AND author = 'Frank Herbert'
  461. """
  462. }
  463. ----
  464. // TEST[setup:library]
  465. The API returns:
  466. [source,txt]
  467. ----
  468. author | name | page_count | release_date |release_day_of_week
  469. ---------------+---------------+---------------+------------------------+-------------------
  470. Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z|TUESDAY
  471. ----
  472. // TESTRESPONSE[non_json]
  473. [[sql-rest-fields]]
  474. === Supported REST parameters
  475. In addition to the `query` and `fetch_size`, a request a number of user-defined fields for specifying
  476. the request time-outs or localization information (such as timezone).
  477. The table below lists the supported parameters:
  478. [cols="<m,<m,<5"]
  479. |===
  480. s|name
  481. s|Default value
  482. s|Description
  483. |query
  484. |Mandatory
  485. |SQL query to execute
  486. |fetch_size
  487. |1000
  488. |The maximum number of rows (or entries) to return in one response
  489. |filter
  490. |none
  491. |Optional {es} Query DSL for additional <<sql-rest-filtering, filtering>>.
  492. |request_timeout
  493. |90s
  494. |The timeout before the request fails.
  495. |page_timeout
  496. |45s
  497. |The timeout before a pagination request fails.
  498. |[[sql-rest-fields-timezone]]time_zone
  499. |`Z` (or `UTC`)
  500. |Time-zone in ISO 8601 used for executing the query on the server.
  501. More information available https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html[here].
  502. |columnar
  503. |false
  504. |Return the results in a columnar fashion, rather than row-based fashion. Valid for `json`, `yaml`, `cbor` and `smile`.
  505. |field_multi_value_leniency
  506. |false
  507. |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).
  508. |index_include_frozen
  509. |false
  510. |Whether to include <<frozen-indices, frozen-indices>> in the query execution or not (default).
  511. |params
  512. |none
  513. |Optional list of parameters to replace question mark (`?`) placeholders inside the query.
  514. |runtime_mappings
  515. |none
  516. |Defines one or more <<runtime-search-request,runtime fields>> in the search
  517. request. These fields take precedence over mapped fields with the same name.
  518. |===
  519. 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.
  520. 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.