esql-syntax.asciidoc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. [[esql-syntax]]
  2. === {esql} syntax reference
  3. ++++
  4. <titleabbrev>Syntax reference</titleabbrev>
  5. ++++
  6. [discrete]
  7. [[esql-basic-syntax]]
  8. === Basic syntax
  9. An {esql} query is composed of a <<esql-commands,source command>> followed
  10. by an optional series of <<esql-commands,processing commands>>,
  11. separated by a pipe character: `|`. For example:
  12. [source,esql]
  13. ----
  14. source-command
  15. | processing-command1
  16. | processing-command2
  17. ----
  18. The result of a query is the table produced by the final processing command.
  19. For an overview of all supported commands, functions, and operators, refer to <<esql-commands>> and <<esql-functions-operators>>.
  20. [NOTE]
  21. ====
  22. For readability, this documentation puts each processing command on a new
  23. line. However, you can write an {esql} query as a single line. The following
  24. query is identical to the previous one:
  25. [source,esql]
  26. ----
  27. source-command | processing-command1 | processing-command2
  28. ----
  29. ====
  30. [discrete]
  31. [[esql-identifiers]]
  32. ==== Identifiers
  33. Identifiers need to be quoted with backticks (+{backtick}+) if:
  34. * they don't start with a letter, `_` or `@`
  35. * any of the other characters is not a letter, number, or `_`
  36. For example:
  37. [source,esql]
  38. ----
  39. FROM index
  40. | KEEP `1.field`
  41. ----
  42. When referencing a function alias that itself uses a quoted identifier, the
  43. backticks of the quoted identifier need to be escaped with another backtick. For
  44. example:
  45. [source,esql]
  46. ----
  47. FROM index
  48. | STATS COUNT(`1.field`)
  49. | EVAL my_count = `COUNT(``1.field``)`
  50. ----
  51. [discrete]
  52. [[esql-literals]]
  53. ==== Literals
  54. {esql} currently supports numeric and string literals.
  55. [discrete]
  56. [[esql-string-literals]]
  57. ===== String literals
  58. A string literal is a sequence of unicode characters delimited by double
  59. quotes (`"`).
  60. [source,esql]
  61. ----
  62. // Filter by a string value
  63. FROM index
  64. | WHERE first_name == "Georgi"
  65. ----
  66. If the literal string itself contains quotes, these need to be escaped (`\\"`).
  67. {esql} also supports the triple-quotes (`"""`) delimiter, for convenience:
  68. [source,esql]
  69. ----
  70. ROW name = """Indiana "Indy" Jones"""
  71. ----
  72. The special characters CR, LF and TAB can be provided with the usual escaping:
  73. `\r`, `\n`, `\t`, respectively.
  74. [discrete]
  75. [[esql-numeric-literals]]
  76. ===== Numerical literals
  77. The numeric literals are accepted in decimal and in the scientific notation
  78. with the exponent marker (`e` or `E`), starting either with a digit, decimal
  79. point `.` or the negative sign `-`:
  80. [source, sql]
  81. ----
  82. 1969 -- integer notation
  83. 3.14 -- decimal notation
  84. .1234 -- decimal notation starting with decimal point
  85. 4E5 -- scientific notation (with exponent marker)
  86. 1.2e-3 -- scientific notation with decimal point
  87. -.1e2 -- scientific notation starting with the negative sign
  88. ----
  89. The integer numeric literals are implicitly converted to the `integer`, `long`
  90. or the `double` type, whichever can first accommodate the literal's value.
  91. The floating point literals are implicitly converted the `double` type.
  92. To obtain constant values of different types, use one of the numeric
  93. <<esql-type-conversion-functions, conversion functions>>.
  94. [discrete]
  95. [[esql-comments]]
  96. ==== Comments
  97. {esql} uses C++ style comments:
  98. * double slash `//` for single line comments
  99. * `/*` and `*/` for block comments
  100. [source,esql]
  101. ----
  102. // Query the employees index
  103. FROM employees
  104. | WHERE height > 2
  105. ----
  106. [source,esql]
  107. ----
  108. FROM /* Query the employees index */ employees
  109. | WHERE height > 2
  110. ----
  111. [source,esql]
  112. ----
  113. FROM employees
  114. /* Query the
  115. * employees
  116. * index */
  117. | WHERE height > 2
  118. ----
  119. [discrete]
  120. [[esql-timespan-literals]]
  121. ==== Timespan literals
  122. Datetime intervals and timespans can be expressed using timespan literals.
  123. Timespan literals are a combination of a number and a temporal unit. The
  124. supported temporal units are listed in <<esql-time-spans-table, time span unit>>.
  125. More examples of the usages of time spans can be found in
  126. <<esql-time-spans, Use time spans in ES|QL>>.
  127. Timespan literals are not whitespace sensitive. These expressions are all valid:
  128. * `1day`
  129. * `1 day`
  130. * `1 day`
  131. [discrete]
  132. [[esql-function-named-params]]
  133. ==== Function named parameters
  134. Some functions like <<esql-match,match>> use named parameters to provide additional options.
  135. Named parameters allow specifying name value pairs, using the following syntax:
  136. `{"option_name": option_value, "another_option_name": another_value}`
  137. Valid value types are strings, numbers and booleans.
  138. An example using <<esql-match,match>>:
  139. [source,console]
  140. ----
  141. POST /_query
  142. {
  143. "query": """
  144. FROM library
  145. | WHERE match(author, "Frank Herbert", {"minimum_should_match": 2, "operator": "AND"})
  146. | LIMIT 5
  147. """
  148. }
  149. ----
  150. // TEST[setup:library]
  151. You can also use <<esql-rest-params,query parameters>> in function named parameters:
  152. [source,console]
  153. ----
  154. POST /_query
  155. {
  156. "query": """
  157. FROM library
  158. | EVAL year = DATE_EXTRACT("year", release_date)
  159. | WHERE page_count > ? AND match(author, ?, {"minimum_should_match": ?})
  160. | LIMIT 5
  161. """,
  162. "params": [300, "Frank Herbert", 2]
  163. }
  164. ----
  165. // TEST[setup:library]