esql-processing-commands.asciidoc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. [[esql-processing-commands]]
  2. == ESQL processing commands
  3. ++++
  4. <titleabbrev>Processing commands</titleabbrev>
  5. ++++
  6. :keywords: {es}, ESQL, {es} query language, processing commands
  7. :description: ESQL processing commands change an input table by adding, removing, or changing rows and columns.
  8. ESQL processing commands change an input table by adding, removing, or changing
  9. rows and columns.
  10. image::images/esql/processing-command.svg[A processing command changing an input table,align="center"]
  11. ESQL supports these processing commands:
  12. * <<esql-dissect>>
  13. * <<esql-drop>>
  14. * <<esql-eval>>
  15. * <<esql-grok>>
  16. * <<esql-limit>>
  17. * <<esql-project>>
  18. * <<esql-rename>>
  19. * <<esql-sort>>
  20. * <<esql-stats-by>>
  21. * <<esql-where>>
  22. [[esql-dissect]]
  23. === `DISSECT`
  24. `DISSECT` enables you to extract structured data out of a string. `DISSECT`
  25. matches the string against a delimiter-based pattern, and extracts the specified
  26. keys as columns.
  27. Refer to the <<dissect-processor,dissect processor documentation>> for the
  28. syntax of dissect patterns.
  29. [source,esql]
  30. ----
  31. ROW a = "1953-01-23T12:15:00Z - some text - 127.0.0.1"
  32. | DISSECT a "%{Y}-%{M}-%{D}T%{h}:%{m}:%{s}Z - %{msg} - %{ip}"
  33. ----
  34. [[esql-drop]]
  35. === `DROP`
  36. Use `DROP` to remove columns from a table:
  37. [source,esql]
  38. ----
  39. FROM employees
  40. | DROP height
  41. ----
  42. Rather than specify each column by name, you can use wildcards to drop all
  43. columns with a name that matches a pattern:
  44. [source,esql]
  45. ----
  46. FROM employees
  47. | DROP height*
  48. ----
  49. [[esql-eval]]
  50. === `EVAL`
  51. `EVAL` enables you to add new columns to the end of a table:
  52. [source,esql]
  53. ----
  54. FROM employees
  55. | PROJECT first_name, last_name, height
  56. | EVAL height_feet = height * 3.281, height_cm = height * 100
  57. ----
  58. If the specified column already exists, the existing column will be dropped, and
  59. the new column will be appended to the table:
  60. [source,esql]
  61. ----
  62. FROM employees
  63. | PROJECT first_name, last_name, height
  64. | EVAL height = height * 3.281
  65. ----
  66. [discrete]
  67. ==== Functions
  68. `EVAL` supports various functions for calculating values. Refer to
  69. <<esql-functions,Functions>> for more information.
  70. [[esql-grok]]
  71. === `GROK`
  72. `GROK` enables you to extract structured data out of a string. `GROK` matches
  73. the string against patterns, based on regular expressions, and extracts the
  74. specified patterns as columns.
  75. Refer to the <<grok-processor,grok processor documentation>> for the syntax for
  76. of grok patterns.
  77. [source,esql]
  78. ----
  79. ROW a = "12 15.5 15.6 true"
  80. | GROK a "%{NUMBER:b:int} %{NUMBER:c:float} %{NUMBER:d:double} %{WORD:e:boolean}"
  81. ----
  82. [[esql-limit]]
  83. === `LIMIT`
  84. The `LIMIT` processing command enables you to limit the number of rows:
  85. [source,esql]
  86. ----
  87. FROM employees
  88. | LIMIT 5
  89. ----
  90. [[esql-project]]
  91. === `PROJECT`
  92. The `PROJECT` command enables you to specify what columns are returned and the
  93. order in which they are returned.
  94. To limit the columns that are returned, use a comma-separated list of column
  95. names. The columns are returned in the specified order:
  96. [source,esql]
  97. ----
  98. FROM employees
  99. | PROJECT first_name, last_name, height
  100. ----
  101. Rather than specify each column by name, you can use wildcards to return all
  102. columns with a name that matches a pattern:
  103. [source,esql]
  104. ----
  105. FROM employees
  106. | PROJECT h*
  107. ----
  108. The asterisk wildcard (`*`) by itself translates to all columns that do not
  109. match the other arguments. This query will first return all columns with a name
  110. that starts with an h, followed by all other columns:
  111. [source,esql]
  112. ----
  113. FROM employees
  114. | PROJECT h*, *
  115. ----
  116. [[esql-rename]]
  117. === `RENAME`
  118. Use `RENAME` to rename a column. If a column with the new name already exists,
  119. it will be replaced by the new column.
  120. [source,esql]
  121. ----
  122. FROM employees
  123. | PROJECT first_name, last_name, still_hired
  124. | RENAME employed = still_hired
  125. ----
  126. Multiple columns can be renamed with a single `RENAME` command:
  127. [source,esql]
  128. ----
  129. FROM employees
  130. | PROJECT first_name, last_name
  131. | RENAME fn = first_name, ln = last_name
  132. ----
  133. [[esql-sort]]
  134. === `SORT`
  135. Use the `SORT` command to sort rows on one or more fields:
  136. [source,esql]
  137. ----
  138. FROM employees
  139. | PROJECT first_name, last_name, height
  140. | SORT height
  141. ----
  142. The default sort order is ascending. Set an explicit sort order using `ASC` or
  143. `DESC`:
  144. [source,esql]
  145. ----
  146. FROM employees
  147. | PROJECT first_name, last_name, height
  148. | SORT height DESC
  149. ----
  150. If two rows have the same sort key, the original order will be preserved. You
  151. can provide additional sort expressions to act as tie breakers:
  152. [source,esql]
  153. ----
  154. FROM employees
  155. | PROJECT first_name, last_name, height
  156. | SORT height DESC, first_name ASC
  157. ----
  158. [discrete]
  159. ==== `null` values
  160. By default, `null` values are treated as being larger than any other value. With
  161. an ascending sort order, `null` values are sorted last, and with a descending
  162. sort order, `null` values are sorted first. You can change that by providing
  163. `NULLS FIRST` or `NULLS LAST`:
  164. [source,esql]
  165. ----
  166. FROM employees
  167. | PROJECT first_name, last_name, height
  168. | SORT first_name ASC NULLS FIRST
  169. ----
  170. [[esql-stats-by]]
  171. === `STATS ... BY`
  172. Use `STATS ... BY` to group rows according to a common value and calculate one
  173. or more aggregated values over the grouped rows.
  174. [source,esql]
  175. ----
  176. FROM employees
  177. | STATS count = COUNT(languages) BY languages
  178. ----
  179. If `BY` is omitted, the output table contains exactly one row with the
  180. aggregations applied over the entire dataset:
  181. [source,esql]
  182. ----
  183. FROM employees
  184. | STATS avg_lang = AVG(languages)
  185. ----
  186. It's possible to calculate multiple values:
  187. [source,esql]
  188. ----
  189. FROM employees
  190. | STATS avg_lang = AVG(languages), max_lang = MAX(languages)
  191. ----
  192. It's also possible to group by multiple values (only supported for long and
  193. keyword family fields):
  194. [source,esql]
  195. ----
  196. FROM employees
  197. | EVAL hired = DATE_FORMAT(hire_date, "YYYY")
  198. | STATS avg_salary = AVG(salary) BY hired, languages.long
  199. | EVAL avg_salary = ROUND(avg_salary)
  200. | SORT hired, languages.long
  201. ----
  202. The following aggregation functions are supported:
  203. * `AVG`
  204. * `COUNT`
  205. * `MAX`
  206. * `MEDIAN`
  207. * `MEDIAN_ABSOLUTE_DEVIATION`
  208. * `MIN`
  209. * `SUM`
  210. [[esql-where]]
  211. === `WHERE`
  212. Use `WHERE` to produce a table that contains all the rows from the input table
  213. for which the provided condition evaluates to `true`:
  214. [source,esql]
  215. ----
  216. FROM employees
  217. | PROJECT first_name, last_name, still_hired
  218. | WHERE still_hired == true
  219. ----
  220. Which, if `still_hired` is a boolean field, can be simplified to:
  221. [source,esql]
  222. ----
  223. FROM employees
  224. | PROJECT first_name, last_name, still_hired
  225. | WHERE still_hired
  226. ----
  227. [discrete]
  228. ==== Operators
  229. Refer to <<esql-operators>> for an overview of the supported operators.
  230. [discrete]
  231. ==== Functions
  232. `WHERE` supports various functions for calculating values. Refer to
  233. <<esql-functions,Functions>> for more information.
  234. [source,esql]
  235. ----
  236. FROM employees
  237. | PROJECT first_name, last_name, height
  238. | WHERE length(first_name) < 4
  239. ----