syntax.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[eql-syntax]]
  4. == EQL syntax reference
  5. ++++
  6. <titleabbrev>Syntax reference</titleabbrev>
  7. ++++
  8. dev::[]
  9. [IMPORTANT]
  10. ====
  11. {es} supports a subset of EQL syntax. See <<eql-limitations>>.
  12. ====
  13. [discrete]
  14. [[eql-basic-syntax]]
  15. === Basic syntax
  16. EQL queries require an event category and a matching condition. The `where`
  17. keyword connects them.
  18. [source,eql]
  19. ----
  20. event_category where condition
  21. ----
  22. For example, the following EQL query matches `process` events with a
  23. `process.name` field value of `svchost.exe`:
  24. [source,eql]
  25. ----
  26. process where process.name == "svchost.exe"
  27. ----
  28. [discrete]
  29. [[eql-syntax-event-categories]]
  30. ==== Event categories
  31. In {es}, an event category is a valid, indexed value of the
  32. <<eql-required-fields,event category field>>. You can set the event category
  33. field using the `event_category_field` parameter of the EQL search API.
  34. [discrete]
  35. [[eql-syntax-match-any-event-category]]
  36. ===== Match any event category
  37. To match events of any category, use the `any` keyword. You can also use the
  38. `any` keyword to search for documents without a event category field.
  39. For example, the following EQL query matches any documents with a
  40. `network.protocol` field value of `http`:
  41. [source,eql]
  42. ----
  43. any where network.protocol == "http"
  44. ----
  45. [discrete]
  46. [[eql-syntax-conditions]]
  47. ==== Conditions
  48. A condition consists of one or more criteria an event must match.
  49. You can specify and combine these criteria using the following operators:
  50. [discrete]
  51. [[eql-syntax-comparison-operators]]
  52. ===== Comparison operators
  53. [source,eql]
  54. ----
  55. < <= == != >= >
  56. ----
  57. [IMPORTANT]
  58. ====
  59. Avoid using the equal operator (`==`) to perform exact matching on `text` field
  60. values.
  61. By default, {es} changes the values of <<text,`text`>> fields as part of
  62. <<analysis, analysis>>. This can make finding exact matches for `text` field
  63. values difficult.
  64. To search `text` fields, consider using a <<eql-search-filter-query-dsl,query
  65. DSL filter>> that contains a <<query-dsl-match-query,`match`>> query.
  66. ====
  67. .*Definitions*
  68. [%collapsible]
  69. ====
  70. `<` (less than)::
  71. Returns `true` if the value to the left of the operator is less than the value
  72. to the right. Otherwise returns `false`.
  73. `<=` (less than or equal) ::
  74. Returns `true` if the value to the left of the operator is less than or equal to
  75. the value to the right. Otherwise returns `false`.
  76. `==` (equal)::
  77. Returns `true` if the values to the left and right of the operator are equal.
  78. Otherwise returns `false`.
  79. `!=` (not equal)::
  80. Returns `true` if the values to the left and right of the operator are not
  81. equal. Otherwise returns `false`.
  82. `>=` (greater than or equal) ::
  83. Returns `true` if the value to the left of the operator is greater than or equal
  84. to the value to the right. Otherwise returns `false`.
  85. `>` (greater than)::
  86. Returns `true` if the value to the left of the operator is greater than the
  87. value to the right. Otherwise returns `false`.
  88. ====
  89. [discrete]
  90. [[eql-syntax-logical-operators]]
  91. ===== Logical operators
  92. [source,eql]
  93. ----
  94. and or not
  95. ----
  96. .*Definitions*
  97. [%collapsible]
  98. ====
  99. `and`::
  100. Returns `true` only if the condition to the left and right _both_ return `true`.
  101. Otherwise returns `false.
  102. `or`::
  103. Returns `true` if one of the conditions to the left or right `true`.
  104. Otherwise returns `false.
  105. `not`::
  106. Returns `true` if the condition to the right is `false`.
  107. ====
  108. [discrete]
  109. [[eql-syntax-lookup-operators]]
  110. ===== Lookup operators
  111. [source,eql]
  112. ----
  113. user.name in ("Administrator", "SYSTEM", "NETWORK SERVICE")
  114. user.name not in ("Administrator", "SYSTEM", "NETWORK SERVICE")
  115. ----
  116. .*Definitions*
  117. [%collapsible]
  118. ====
  119. `in`::
  120. Returns `true` if the value is contained in the provided list.
  121. `not in`::
  122. Returns `true` if the value is not contained in the provided list.
  123. ====
  124. [discrete]
  125. [[eql-syntax-math-operators]]
  126. ===== Math operators
  127. [source,eql]
  128. ----
  129. + - * / %
  130. ----
  131. .*Definitions*
  132. [%collapsible]
  133. ====
  134. `+` (add)::
  135. Adds the values to the left and right of the operator.
  136. `-` (Subtract)::
  137. Subtracts the value to the right of the operator from the value to the left.
  138. `*` (Subtract)::
  139. Multiplies the values to the left and right of the operator.
  140. `/` (Divide)::
  141. Divides the value to the left of the operator by the value to the right.
  142. `%` (modulo)::
  143. Divides the value to the left of the operator by the value to the right. Returns only the remainder.
  144. ====
  145. [[eql-divide-operator-float-rounding]]
  146. [WARNING]
  147. ====
  148. If both the dividend and divisor are integers, the divide (`\`) operation
  149. _rounds down_ any returned floating point numbers to the nearest integer.
  150. EQL queries in {es} should account for this rounding. To avoid rounding, convert
  151. either the dividend or divisor to a float.
  152. [%collapsible]
  153. .**Example**
  154. =====
  155. The `process.args_count` field is a <<number,`long`>> integer field containing a
  156. count of process arguments.
  157. A user might expect the following EQL query to only match events with a
  158. `process.args_count` value of `4`.
  159. [source,eql]
  160. ----
  161. process where ( 4 / process.args_count ) == 1
  162. ----
  163. However, the EQL query matches events with a `process.args_count` value of `3`
  164. or `4`.
  165. For events with a `process.args_count` value of `3`, the divide operation
  166. returns a float of `1.333...`, which is rounded down to `1`.
  167. To match only events with a `process.args_count` value of `4`, convert
  168. either the dividend or divisor to a float.
  169. The following EQL query changes the integer `4` to the equivalent float `4.0`.
  170. [source,eql]
  171. ----
  172. process where ( 4.0 / process.args_count ) == 1
  173. ----
  174. =====
  175. ====
  176. [discrete]
  177. [[eql-syntax-strings]]
  178. ==== Strings
  179. Strings are enclosed with double quotes (`"`) or single quotes (`'`).
  180. [source,eql]
  181. ----
  182. "hello world"
  183. "hello world with 'substring'"
  184. ----
  185. [discrete]
  186. [[eql-syntax-wildcards]]
  187. ===== Wildcards
  188. You can use the wildcard operator (`*`) within a string to match specific
  189. patterns. You can use wildcards with the `==` (equal) or `!=` (not equal)
  190. operators:
  191. [source,eql]
  192. ----
  193. field == "example*wildcard"
  194. field != "example*wildcard"
  195. ----
  196. [discrete]
  197. [[eql-syntax-match-any-condition]]
  198. ===== Match any condition
  199. To match events solely on event category, use the `where true` condition.
  200. For example, the following EQL query matches any `file` events:
  201. [source,eql]
  202. ----
  203. file where true
  204. ----
  205. To match any event, you can combine the `any` keyword with the `where true`
  206. condition:
  207. [source,eql]
  208. ----
  209. any where true
  210. ----
  211. [discrete]
  212. [[eql-syntax-escaped-characters]]
  213. ===== Escaped characters
  214. When used within a string, special characters, such as a carriage return or
  215. double quote (`"`), must be escaped with a preceding backslash (`\`).
  216. [source,eql]
  217. ----
  218. "example \t of \n escaped \r characters"
  219. ----
  220. .*Escape sequences*
  221. [%collapsible]
  222. ====
  223. [options="header"]
  224. |====
  225. | Escape sequence | Literal character
  226. |`\n` | A newline (linefeed) character
  227. |`\r` | A carriage return character
  228. |`\t` | A tab character
  229. |`\\` | A backslash (`\`) character
  230. |`\"` | A double quote (`"`) character
  231. |`\'` | A single quote (`'`) character
  232. |====
  233. ====
  234. [discrete]
  235. [[eql-syntax-raw-strings]]
  236. ===== Raw strings
  237. Raw strings are preceded by a question mark (`?`) and treat backslashes (`\`) as
  238. literal characters.
  239. [source,eql]
  240. ----
  241. ?"String with a literal 'blackslash' \ character included"
  242. ----
  243. You can escape single quotes (`'`) and double quotes (`"`) with a backslash, but
  244. the backslash remains in the resulting string.
  245. [source,eql]
  246. ----
  247. ?"\""
  248. ----
  249. [NOTE]
  250. ====
  251. Raw strings cannot contain only a single backslash. Additionally, raw strings
  252. cannot end in an odd number of backslashes.
  253. ====
  254. [discrete]
  255. [[eql-syntax-non-alpha-field-names]]
  256. ==== Non-alphanumeric field names
  257. Field names containing non-alphanumeric characters, such as underscores (`_`),
  258. dots (`.`), hyphens (`-`), or spaces, must be escaped using backticks (+++`+++).
  259. [source,eql]
  260. ----
  261. `my_field`
  262. `my.field`
  263. `my-field`
  264. `my field`
  265. ----
  266. [discrete]
  267. [[eql-sequences]]
  268. === Sequences
  269. You can use EQL sequences to describe and match an ordered series of events.
  270. Each item in a sequence is an event category and event condition,
  271. surrounded by square brackets. Events are listed in ascending chronological
  272. order, with the most recent event listed last.
  273. [source,eql]
  274. ----
  275. sequence
  276. [ event_category_1 where condition_1 ]
  277. [ event_category_2 where condition_2 ]
  278. ...
  279. ----
  280. .*Example*
  281. [%collapsible]
  282. ====
  283. The following EQL query matches this series of ordered events:
  284. . Start with an event with:
  285. +
  286. --
  287. * An event category of `file`
  288. * A `file.extension` of `exe`
  289. --
  290. . Followed by an event with an event category of `process`
  291. [source,eql]
  292. ----
  293. sequence
  294. [ file where file.extension == "exe" ]
  295. [ process where true ]
  296. ----
  297. ====
  298. You can use the `by` keyword with sequences to only match events that share the
  299. same field values. If a field value should be shared across all events, you
  300. can use `sequence by`.
  301. [source,eql]
  302. ----
  303. sequence by field_foo
  304. [ event_category_1 where condition_1 ] by field_baz
  305. [ event_category_2 where condition_2 ] by field_bar
  306. ...
  307. ----
  308. .*Example*
  309. [%collapsible]
  310. ====
  311. The following sequence uses the `by` keyword to constrain matching events to:
  312. * Events with the same `user.name` value
  313. * `file` events with a `file.path` value equal to the following `process`
  314. event's `process.path` value.
  315. [source,eql]
  316. ----
  317. sequence
  318. [ file where file.extension == "exe" ] by user.name, file.path
  319. [ process where true ] by user.name, process.path
  320. ----
  321. Because the `user.name` field is shared across all events in the sequence, it
  322. can be included using `sequence by`. The following sequence is equivalent to the
  323. prior one.
  324. [source,eql]
  325. ----
  326. sequence by user.name
  327. [ file where file.extension == "exe" ] by file.path
  328. [ process where true ] by process.path
  329. ----
  330. ====
  331. [discrete]
  332. [[eql-functions]]
  333. === Functions
  334. {es} supports several of EQL's built-in functions. You can use these functions
  335. to convert data types, perform math, manipulate strings, and more.
  336. For a list of supported functions, see <<eql-function-ref>>.
  337. [TIP]
  338. ====
  339. Using functions in EQL queries can result in slower search speeds. If you
  340. often use functions to transform indexed data, you can speed up search by making
  341. these changes during indexing instead. However, that often means slower index
  342. speeds.
  343. .*Example*
  344. [%collapsible]
  345. =====
  346. An index contains the `file.path` field. `file.path` contains the full path to a
  347. file, including the file extension.
  348. When running EQL searches, users often use the `endsWith` function with the
  349. `file.path` field to match file extensions:
  350. [source,eql]
  351. ----
  352. file where endsWith(file.path,".exe") or endsWith(file.path,".dll")
  353. ----
  354. While this works, it can be repetitive to write and can slow search speeds. To
  355. speed up search, you can do the following instead:
  356. . <<indices-put-mapping,Add a new field>>, `file.extension`, to the index. The
  357. `file.extension` field will contain only the file extension from the
  358. `file.path` field.
  359. . Use an <<ingest,ingest pipeline>> containing the <<grok-processor,`grok`>>
  360. processor or another preprocessor tool to extract the file extension from the
  361. `file.path` field before indexing.
  362. . Index the extracted file extension to the `file.extension` field.
  363. These changes may slow indexing but allow for faster searches. Users
  364. can use the `file.extension` field instead of multiple `endsWith` function
  365. calls:
  366. [source,eql]
  367. ----
  368. file where file.extension in ("exe", "dll")
  369. ----
  370. =====
  371. We recommend testing and benchmarking any indexing changes before deploying them
  372. in production. See <<tune-for-indexing-speed>> and <<tune-for-search-speed>>.
  373. ====