esql-rest.asciidoc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. [[esql-rest]]
  2. === {esql} REST API
  3. ++++
  4. <titleabbrev>REST API</titleabbrev>
  5. ++++
  6. [discrete]
  7. [[esql-rest-overview]]
  8. === Overview
  9. The <<esql-query-api,{esql} query API>> accepts an {esql} query string in the
  10. `query` parameter, runs it, and returns the results. For example:
  11. [source,console]
  12. ----
  13. POST /_query?format=txt
  14. {
  15. "query": "FROM library | KEEP author, name, page_count, release_date | SORT page_count DESC | LIMIT 5"
  16. }
  17. ----
  18. // TEST[setup:library]
  19. Which returns:
  20. [source,text]
  21. ----
  22. author | name | page_count | release_date
  23. -----------------+--------------------+---------------+------------------------
  24. Peter F. Hamilton|Pandora's Star |768 |2004-03-02T00:00:00.000Z
  25. Vernor Vinge |A Fire Upon the Deep|613 |1992-06-01T00:00:00.000Z
  26. Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
  27. Alastair Reynolds|Revelation Space |585 |2000-03-15T00:00:00.000Z
  28. James S.A. Corey |Leviathan Wakes |561 |2011-06-02T00:00:00.000Z
  29. ----
  30. // TESTRESPONSE[s/\|/\\|/ s/\+/\\+/]
  31. // TESTRESPONSE[non_json]
  32. [discrete]
  33. [[esql-kibana-console]]
  34. ==== Kibana Console
  35. If you are using {kibana-ref}/console-kibana.html[Kibana Console] (which is
  36. highly recommended), take advantage of the triple quotes `"""` when creating the
  37. query. This not only automatically escapes double quotes (`"`) inside the query
  38. string but also supports multi-line requests:
  39. // tag::esql-query-api[]
  40. [source,console]
  41. ----
  42. POST /_query?format=txt
  43. {
  44. "query": """
  45. FROM library
  46. | KEEP author, name, page_count, release_date
  47. | SORT page_count DESC
  48. | LIMIT 5
  49. """
  50. }
  51. ----
  52. // TEST[setup:library]
  53. [discrete]
  54. [[esql-rest-format]]
  55. ==== Response formats
  56. {esql} can return the data in the following human readable and binary formats.
  57. You can set the format by specifying the `format` parameter in the URL or by
  58. setting the `Accept` or `Content-Type` HTTP header.
  59. NOTE: The URL parameter takes precedence over the HTTP headers. If neither is
  60. specified then the response is returned in the same format as the request.
  61. [cols="m,4m,8"]
  62. |===
  63. s|`format`
  64. s|HTTP header
  65. s|Description
  66. 3+h| Human readable
  67. |csv
  68. |text/csv
  69. |{wikipedia}/Comma-separated_values[Comma-separated values]
  70. |json
  71. |application/json
  72. |https://www.json.org/[JSON] (JavaScript Object Notation) human-readable format
  73. |tsv
  74. |text/tab-separated-values
  75. |{wikipedia}/Tab-separated_values[Tab-separated values]
  76. |txt
  77. |text/plain
  78. |CLI-like representation
  79. |yaml
  80. |application/yaml
  81. |{wikipedia}/YAML[YAML] (YAML Ain't Markup Language) human-readable format
  82. 3+h| Binary
  83. |cbor
  84. |application/cbor
  85. |https://cbor.io/[Concise Binary Object Representation]
  86. |smile
  87. |application/smile
  88. |{wikipedia}/Smile_(data_interchange_format)[Smile] binary data format similar
  89. to CBOR
  90. |===
  91. The `csv` format accepts a formatting URL query attribute, `delimiter`, which
  92. indicates which character should be used to separate the CSV values. It defaults
  93. to comma (`,`) and cannot take any of the following values: double quote (`"`),
  94. carriage-return (`\r`) and new-line (`\n`). The tab (`\t`) can also not be used.
  95. Use the `tsv` format instead.
  96. [discrete]
  97. [[esql-rest-filtering]]
  98. ==== Filtering using {es} Query DSL
  99. Specify a Query DSL query in the `filter` parameter to filter the set of
  100. documents that an {esql} query runs on.
  101. [source,console]
  102. ----
  103. POST /_query?format=txt
  104. {
  105. "query": """
  106. FROM library
  107. | KEEP author, name, page_count, release_date
  108. | SORT page_count DESC
  109. | LIMIT 5
  110. """,
  111. "filter": {
  112. "range": {
  113. "page_count": {
  114. "gte": 100,
  115. "lte": 200
  116. }
  117. }
  118. }
  119. }
  120. ----
  121. // TEST[setup:library]
  122. Which returns:
  123. [source,text]
  124. --------------------------------------------------
  125. author | name | page_count | release_date
  126. ---------------+------------------------------------+---------------+------------------------
  127. Douglas Adams |The Hitchhiker's Guide to the Galaxy|180 |1979-10-12T00:00:00.000Z
  128. --------------------------------------------------
  129. // TESTRESPONSE[s/\|/\\|/ s/\+/\\+/]
  130. // TESTRESPONSE[non_json]
  131. [discrete]
  132. [[esql-rest-columnar]]
  133. ==== Columnar results
  134. By default, {esql} returns results as rows. For example, `FROM` returns each
  135. individual document as one row. For the `json`, `yaml`, `cbor` and `smile`
  136. <<esql-rest-format,formats>>, {esql} can return the results in a columnar
  137. fashion where one row represents all the values of a certain column in the
  138. results.
  139. [source,console]
  140. ----
  141. POST /_query?format=json
  142. {
  143. "query": """
  144. FROM library
  145. | KEEP author, name, page_count, release_date
  146. | SORT page_count DESC
  147. | LIMIT 5
  148. """,
  149. "columnar": true
  150. }
  151. ----
  152. // TEST[setup:library]
  153. Which returns:
  154. [source,console-result]
  155. ----
  156. {
  157. "columns": [
  158. {"name": "author", "type": "text"},
  159. {"name": "name", "type": "text"},
  160. {"name": "page_count", "type": "integer"},
  161. {"name": "release_date", "type": "date"}
  162. ],
  163. "values": [
  164. ["Peter F. Hamilton", "Vernor Vinge", "Frank Herbert", "Alastair Reynolds", "James S.A. Corey"],
  165. ["Pandora's Star", "A Fire Upon the Deep", "Dune", "Revelation Space", "Leviathan Wakes"],
  166. [768, 613, 604, 585, 561],
  167. ["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"]
  168. ]
  169. }
  170. ----
  171. [discrete]
  172. [[esql-rest-params]]
  173. ==== Passing parameters to a query
  174. Values, for example for a condition, can be passed to a query "inline", by
  175. integrating the value in the query string itself:
  176. [source,console]
  177. ----
  178. POST /_query
  179. {
  180. "query": """
  181. FROM library
  182. | EVAL year = DATE_EXTRACT("year", release_date)
  183. | WHERE page_count > 300 AND author == "Frank Herbert"
  184. | STATS count = COUNT(*) by year
  185. | WHERE count > 0
  186. | LIMIT 5
  187. """
  188. }
  189. ----
  190. // TEST[setup:library]
  191. To avoid any attempts of hacking or code injection, extract the values in a
  192. separate list of parameters. Use question mark placeholders (`?`) in the query
  193. string for each of the parameters:
  194. [source,console]
  195. ----
  196. POST /_query
  197. {
  198. "query": """
  199. FROM library
  200. | EVAL year = DATE_EXTRACT("year", release_date)
  201. | WHERE page_count > ? AND author == ?
  202. | STATS count = COUNT(*) by year
  203. | WHERE count > ?
  204. | LIMIT 5
  205. """,
  206. "params": [300, "Frank Herbert", 0]
  207. }
  208. ----
  209. // TEST[setup:library]
  210. [discrete]
  211. [[esql-rest-async-query]]
  212. ==== Running an async {esql} query
  213. The <<esql-async-query-api,{esql} async query API>> lets you asynchronously
  214. execute a query request, monitor its progress, and retrieve results when
  215. they become available.
  216. Executing an {esql} query is commonly quite fast, however queries across
  217. large data sets or frozen data can take some time. To avoid long waits,
  218. run an async {esql} query.
  219. Queries initiated by the async query API may return results or not. The
  220. `wait_for_completion_timeout` property determines how long to wait for
  221. the results. If the results are not available by this time, a
  222. <<esql-async-query-api-response-body-query-id,query id>> is return which
  223. can be later used to retrieve the results. For example:
  224. [source,console]
  225. ----
  226. POST /_query/async
  227. {
  228. "query": """
  229. FROM library
  230. | EVAL year = DATE_TRUNC(1 YEARS, release_date)
  231. | STATS MAX(page_count) BY year
  232. | SORT year
  233. | LIMIT 5
  234. """,
  235. "wait_for_completion_timeout": "2s"
  236. }
  237. ----
  238. // TEST[setup:library]
  239. If the results are not available within the given timeout period, 2
  240. seconds in this case, no results are returned but rather a response that
  241. includes:
  242. * A query ID
  243. * An `is_running` value of _true_, indicating the query is ongoing
  244. The query continues to run in the background without blocking other
  245. requests.
  246. [source,console-result]
  247. ----
  248. {
  249. "id": "FmNJRUZ1YWZCU3dHY1BIOUhaenVSRkEaaXFlZ3h4c1RTWFNocDdnY2FSaERnUTozNDE=",
  250. "is_running": true
  251. }
  252. ----
  253. // TEST[skip: no access to query ID - may return response values]
  254. To check the progress of an async query, use the <<esql-async-query-get-api,
  255. {esql} async query get API>> with the query ID. Specify how long you'd like
  256. for complete results in the `wait_for_completion_timeout` parameter.
  257. [source,console]
  258. ----
  259. GET /_query/async/FmNJRUZ1YWZCU3dHY1BIOUhaenVSRkEaaXFlZ3h4c1RTWFNocDdnY2FSaERnUTozNDE=?wait_for_completion_timeout=30s
  260. ----
  261. // TEST[skip: no access to query ID - may return response values]
  262. If the response's `is_running` value is `false`, the query has finished,
  263. and the results are returned.
  264. [source,console-result]
  265. ----
  266. {
  267. "is_running": false,
  268. "columns": ...
  269. }
  270. ----
  271. // TEST[skip: no access to query ID - may return response values]
  272. Use the <<esql-async-query-delete-api, {esql} async query delete API>> to
  273. delete an async query before the `keep_alive` period ends. If the query
  274. is still running, {es} cancels it.
  275. [source,console]
  276. ----
  277. DELETE /_query/async/FmdMX2pIang3UWhLRU5QS0lqdlppYncaMUpYQ05oSkpTc3kwZ21EdC1tbFJXQToxOTI=
  278. ----
  279. // TEST[skip: no access to query ID]