esql-processing-commands.asciidoc 7.0 KB

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