esql-processing-commands.asciidoc 6.7 KB

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