esql-rest.asciidoc 9.8 KB

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