esql-rest.asciidoc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. [source,console]
  40. ----
  41. POST /_query?format=txt
  42. {
  43. "query": """
  44. FROM library
  45. | KEEP author, name, page_count, release_date
  46. | SORT page_count DESC
  47. | LIMIT 5
  48. """
  49. }
  50. ----
  51. // TEST[setup:library]
  52. [discrete]
  53. [[esql-rest-format]]
  54. === Response formats
  55. {esql} can return the data in the following human readable and binary formats.
  56. You can set the format by specifying the `format` parameter in the URL or by
  57. setting the `Accept` or `Content-Type` HTTP header.
  58. NOTE: The URL parameter takes precedence over the HTTP headers. If neither is
  59. specified then the response is returned in the same format as the request.
  60. [cols="m,4m,8"]
  61. |===
  62. s|`format`
  63. s|HTTP header
  64. s|Description
  65. 3+h| Human readable
  66. |csv
  67. |text/csv
  68. |{wikipedia}/Comma-separated_values[Comma-separated values]
  69. |json
  70. |application/json
  71. |https://www.json.org/[JSON] (JavaScript Object Notation) human-readable format
  72. |tsv
  73. |text/tab-separated-values
  74. |{wikipedia}/Tab-separated_values[Tab-separated values]
  75. |txt
  76. |text/plain
  77. |CLI-like representation
  78. |yaml
  79. |application/yaml
  80. |{wikipedia}/YAML[YAML] (YAML Ain't Markup Language) human-readable format
  81. 3+h| Binary
  82. |cbor
  83. |application/cbor
  84. |https://cbor.io/[Concise Binary Object Representation]
  85. |smile
  86. |application/smile
  87. |{wikipedia}/Smile_(data_interchange_format)[Smile] binary data format similar
  88. to CBOR
  89. |===
  90. The `csv` format accepts a formatting URL query attribute, `delimiter`, which
  91. indicates which character should be used to separate the CSV values. It defaults
  92. to comma (`,`) and cannot take any of the following values: double quote (`"`),
  93. carriage-return (`\r`) and new-line (`\n`). The tab (`\t`) can also not be used.
  94. Use the `tsv` format instead.
  95. [discrete]
  96. [[esql-rest-filtering]]
  97. === Filtering using {es} Query DSL
  98. Specify a Query DSL query in the `filter` parameter to filter the set of
  99. documents that an {esql} query runs on.
  100. [source,console]
  101. ----
  102. POST /_query?format=txt
  103. {
  104. "query": """
  105. FROM library
  106. | KEEP author, name, page_count, release_date
  107. | SORT page_count DESC
  108. | LIMIT 5
  109. """,
  110. "filter": {
  111. "range": {
  112. "page_count": {
  113. "gte": 100,
  114. "lte": 200
  115. }
  116. }
  117. }
  118. }
  119. ----
  120. // TEST[setup:library]
  121. Which returns:
  122. [source,text]
  123. --------------------------------------------------
  124. author | name | page_count | release_date
  125. ---------------+------------------------------------+---------------+------------------------
  126. Douglas Adams |The Hitchhiker's Guide to the Galaxy|180 |1979-10-12T00:00:00.000Z
  127. --------------------------------------------------
  128. // TESTRESPONSE[s/\|/\\|/ s/\+/\\+/]
  129. // TESTRESPONSE[non_json]
  130. [discrete]
  131. [[esql-rest-columnar]]
  132. === Columnar results
  133. By default, {esql} returns results as rows. For example, `FROM` returns each
  134. individual document as one row. For the `json`, `yaml`, `cbor` and `smile`
  135. <<esql-rest-format,formats>>, {esql} can return the results in a columnar
  136. fashion where one row represents all the values of a certain column in the
  137. results.
  138. [source,console]
  139. ----
  140. POST /_query?format=json
  141. {
  142. "query": """
  143. FROM library
  144. | KEEP author, name, page_count, release_date
  145. | SORT page_count DESC
  146. | LIMIT 5
  147. """,
  148. "columnar": true
  149. }
  150. ----
  151. // TEST[setup:library]
  152. Which returns:
  153. [source,console-result]
  154. ----
  155. {
  156. "columns": [
  157. {"name": "author", "type": "text"},
  158. {"name": "name", "type": "text"},
  159. {"name": "page_count", "type": "integer"},
  160. {"name": "release_date", "type": "date"}
  161. ],
  162. "values": [
  163. ["Peter F. Hamilton", "Vernor Vinge", "Frank Herbert", "Alastair Reynolds", "James S.A. Corey"],
  164. ["Pandora's Star", "A Fire Upon the Deep", "Dune", "Revelation Space", "Leviathan Wakes"],
  165. [768, 613, 604, 585, 561],
  166. ["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"]
  167. ]
  168. }
  169. ----
  170. [discrete]
  171. [[esql-rest-params]]
  172. === Passing parameters to a query
  173. Values, for example for a condition, can be passed to a query "inline", by
  174. integrating the value in the query string itself:
  175. [source,console]
  176. ----
  177. POST /_query
  178. {
  179. "query": """
  180. FROM library
  181. | EVAL year = DATE_EXTRACT("year", release_date)
  182. | WHERE page_count > 300 AND author == "Frank Herbert"
  183. | STATS count = COUNT(*) by year
  184. | WHERE count > 0
  185. """
  186. }
  187. ----
  188. // TEST[setup:library]
  189. To avoid any attempts of hacking or code injection, extract the values in a
  190. separate list of parameters. Use question mark placeholders (`?`) in the query
  191. string for each of the parameters:
  192. [source,console]
  193. ----
  194. POST /_query
  195. {
  196. "query": """
  197. FROM library
  198. | EVAL year = DATE_EXTRACT("year", release_date)
  199. | WHERE page_count > ? AND author == ?
  200. | STATS count = COUNT(*) by year
  201. | WHERE count > ?
  202. """,
  203. "params": [300, "Frank Herbert", 0]
  204. }
  205. ----
  206. // TEST[setup:library]