syntax.asciidoc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[eql-syntax]]
  4. == EQL syntax reference
  5. ++++
  6. <titleabbrev>Syntax reference</titleabbrev>
  7. ++++
  8. experimental::[]
  9. IMPORTANT: {es} supports a subset of {eql-ref}/index.html[EQL syntax]. See
  10. <<eql-syntax-limitations>>.
  11. [discrete]
  12. [[eql-basic-syntax]]
  13. === Basic syntax
  14. EQL queries require an event category and a matching condition. The `where`
  15. keyword connects them.
  16. [source,eql]
  17. ----
  18. event_category where condition
  19. ----
  20. For example, the following EQL query matches `process` events with a
  21. `process.name` field value of `svchost.exe`:
  22. [source,eql]
  23. ----
  24. process where process.name == "svchost.exe"
  25. ----
  26. [discrete]
  27. [[eql-syntax-event-categories]]
  28. ==== Event categories
  29. In {es}, an event category is a valid, indexed value of the
  30. <<eql-required-fields,event category field>>. You can set the event category
  31. field using the `event_category_field` parameter of the EQL search API.
  32. [discrete]
  33. [[eql-syntax-match-any-event-category]]
  34. ===== Match any event category
  35. To match events of any category, use the `any` keyword. You can also use the
  36. `any` keyword to search for documents without a event category field.
  37. For example, the following EQL query matches any documents with a
  38. `network.protocol` field value of `http`:
  39. [source,eql]
  40. ----
  41. any where network.protocol == "http"
  42. ----
  43. [discrete]
  44. [[eql-syntax-escape-an-event-category]]
  45. ===== Escape an event category
  46. Use enclosing double quotes (`"`) or three enclosing double quotes (`"""`) to
  47. escape event categories that:
  48. * Contain a special character, such as a hyphen (`-`) or dot (`.`)
  49. * Contain a space
  50. * Start with a numeral
  51. [source,eql]
  52. ----
  53. ".my.event.category"
  54. "my-event-category"
  55. "my event category"
  56. "6eventcategory"
  57. """.my.event.category"""
  58. """my-event-category"""
  59. """my event category"""
  60. """6eventcategory"""
  61. ----
  62. [discrete]
  63. [[eql-syntax-escape-a-field-name]]
  64. ===== Escape a field name
  65. Use enclosing enclosing backticks (+++`+++) to escape field names that:
  66. * Contain a hyphen (`-`)
  67. * Contain a space
  68. * Start with a numeral
  69. [source,eql]
  70. ----
  71. `my-field`
  72. `my field`
  73. `6myfield`
  74. ----
  75. Use double backticks (+++``+++) to escape any backticks (+++`+++) in the field
  76. name.
  77. [source,eql]
  78. ----
  79. my`field -> `my``field`
  80. ----
  81. [discrete]
  82. [[eql-syntax-conditions]]
  83. ==== Conditions
  84. A condition consists of one or more criteria an event must match.
  85. You can specify and combine these criteria using the following operators. Most
  86. EQL operators are case-sensitive by default.
  87. [discrete]
  88. [[eql-syntax-comparison-operators]]
  89. ===== Comparison operators
  90. [source,eql]
  91. ----
  92. < <= == != >= >
  93. ----
  94. `<` (less than)::
  95. Returns `true` if the value to the left of the operator is less than the value
  96. to the right. Otherwise returns `false`.
  97. `<=` (less than or equal) ::
  98. Returns `true` if the value to the left of the operator is less than or equal to
  99. the value to the right. Otherwise returns `false`.
  100. `==` (equal)::
  101. Returns `true` if the values to the left and right of the operator are equal.
  102. Otherwise returns `false`.
  103. +
  104. [IMPORTANT]
  105. ====
  106. Avoid using the `==` operator to perform exact matching on <<text,`text`>> field
  107. values.
  108. By default, {es} changes the values of `text` fields as part of <<analysis,
  109. analysis>>. This can make finding exact matches for `text` field values
  110. difficult.
  111. To search `text` fields, consider using a <<eql-search-filter-query-dsl,query
  112. DSL filter>> that contains a <<query-dsl-match-query,`match`>> query.
  113. ====
  114. `!=` (not equal)::
  115. Returns `true` if the values to the left and right of the operator are not
  116. equal. Otherwise returns `false`.
  117. `>=` (greater than or equal) ::
  118. Returns `true` if the value to the left of the operator is greater than or equal
  119. to the value to the right. Otherwise returns `false`.
  120. `>` (greater than)::
  121. Returns `true` if the value to the left of the operator is greater than the
  122. value to the right. Otherwise returns `false`.
  123. NOTE: `=` is not supported as an equality operator. Use `==` instead.
  124. You cannot chain comparison operators. Instead, use a
  125. <<eql-syntax-logical-operators,logical operator>> between comparisons. For
  126. example, `foo < bar <= baz` is not supported. However, you can rewrite the
  127. expression as `foo < bar and bar <= baz`, which is supported.
  128. You also cannot use comparison operators to compare a field to another field.
  129. This applies even if the fields are changed using a <<eql-functions,function>>.
  130. *Example* +
  131. The following EQL query compares the `process.parent_name` field
  132. value to a static value, `foo`. This comparison is supported.
  133. However, the query also compares the `process.parent.name` field value to the
  134. `process.name` field. This comparison is not supported and will return an
  135. error for the entire query.
  136. [source,eql]
  137. ----
  138. process where process.parent.name == "foo" and process.parent.name == process.name
  139. ----
  140. Instead, you can rewrite the query to compare both the `process.parent.name`
  141. and `process.name` fields to static values.
  142. [source,eql]
  143. ----
  144. process where process.parent.name == "foo" and process.name == "foo"
  145. ----
  146. [discrete]
  147. [[eql-syntax-logical-operators]]
  148. ===== Logical operators
  149. [source,eql]
  150. ----
  151. and or not
  152. ----
  153. `and`::
  154. Returns `true` only if the condition to the left and right _both_ return `true`.
  155. Otherwise returns `false.
  156. `or`::
  157. Returns `true` if one of the conditions to the left or right `true`.
  158. Otherwise returns `false.
  159. `not`::
  160. Returns `true` if the condition to the right is `false`.
  161. [discrete]
  162. [[eql-syntax-lookup-operators]]
  163. ===== Lookup operators
  164. [source,eql]
  165. ----
  166. user.name in ("Administrator", "SYSTEM", "NETWORK SERVICE")
  167. user.name not in ("Administrator", "SYSTEM", "NETWORK SERVICE")
  168. ----
  169. `in` (case-sensitive)::
  170. Returns `true` if the value is contained in the provided list. For strings,
  171. matching is case-sensitive.
  172. `not in` (case-sensitive)::
  173. Returns `true` if the value is not contained in the provided list. For strings,
  174. matching is case-sensitive.
  175. [discrete]
  176. [[eql-syntax-math-operators]]
  177. ===== Math operators
  178. [source,eql]
  179. ----
  180. + - * / %
  181. ----
  182. `+` (add)::
  183. Adds the values to the left and right of the operator.
  184. `-` (Subtract)::
  185. Subtracts the value to the right of the operator from the value to the left.
  186. `*` (Subtract)::
  187. Multiplies the values to the left and right of the operator.
  188. `/` (Divide)::
  189. Divides the value to the left of the operator by the value to the right.
  190. +
  191. [[eql-divide-operator-float-rounding]]
  192. [WARNING]
  193. ====
  194. If both the dividend and divisor are integers, the divide (`\`) operation
  195. _rounds down_ any returned floating point numbers to the nearest integer.
  196. EQL queries in {es} should account for this rounding. To avoid rounding, convert
  197. either the dividend or divisor to a float.
  198. *Example* +
  199. The `process.args_count` field is a <<number,`long`>> integer field containing a
  200. count of process arguments.
  201. A user might expect the following EQL query to only match events with a
  202. `process.args_count` value of `4`.
  203. [source,eql]
  204. ----
  205. process where ( 4 / process.args_count ) == 1
  206. ----
  207. However, the EQL query matches events with a `process.args_count` value of `3`
  208. or `4`.
  209. For events with a `process.args_count` value of `3`, the divide operation
  210. returns a float of `1.333...`, which is rounded down to `1`.
  211. To match only events with a `process.args_count` value of `4`, convert
  212. either the dividend or divisor to a float.
  213. The following EQL query changes the integer `4` to the equivalent float `4.0`.
  214. [source,eql]
  215. ----
  216. process where ( 4.0 / process.args_count ) == 1
  217. ----
  218. ====
  219. `%` (modulo)::
  220. Divides the value to the left of the operator by the value to the right. Returns only the remainder.
  221. [discrete]
  222. [[eql-syntax-match-any-condition]]
  223. ==== Match any condition
  224. To match events solely on event category, use the `where true` condition.
  225. For example, the following EQL query matches any `file` events:
  226. [source,eql]
  227. ----
  228. file where true
  229. ----
  230. To match any event, you can combine the `any` keyword with the `where true`
  231. condition:
  232. [source,eql]
  233. ----
  234. any where true
  235. ----
  236. [discrete]
  237. [[eql-syntax-strings]]
  238. ==== Strings
  239. Strings are enclosed in double quotes (`"`).
  240. [source,eql]
  241. ----
  242. "hello world"
  243. ----
  244. Strings enclosed in single quotes (`'`) are not supported.
  245. [discrete]
  246. [[eql-syntax-escape-characters]]
  247. ===== Escape characters in a string
  248. When used within a string, special characters, such as a carriage return or
  249. double quote (`"`), must be escaped with a preceding backslash (`\`).
  250. [source,eql]
  251. ----
  252. "example \r of \" escaped \n characters"
  253. ----
  254. [options="header"]
  255. |====
  256. | Escape sequence | Literal character
  257. |`\n` | A newline (linefeed) character
  258. |`\r` | A carriage return character
  259. |`\t` | A tab character
  260. |`\\` | A backslash (`\`) character
  261. |`\"` | A double quote (`"`) character
  262. |====
  263. IMPORTANT: The single quote (`'`) character is reserved for future use. You
  264. cannot use an escaped single quote (`\'`) for literal strings. Use an escaped
  265. double quote (`\"`) instead.
  266. [discrete]
  267. [[eql-syntax-raw-strings]]
  268. ===== Raw strings
  269. Raw strings treat special characters, such as backslashes (`\`), as literal
  270. characters. Raw strings are enclosed in three double quotes (`"""`).
  271. [source,eql]
  272. ----
  273. """Raw string with a literal double quote " and blackslash \ included"""
  274. ----
  275. A raw string cannot contain three consecutive double quotes (`"""`). Instead,
  276. use a regular string with the `\"` escape sequence.
  277. [source,eql]
  278. ----
  279. "String containing \"\"\" three double quotes"
  280. ----
  281. [discrete]
  282. [[eql-syntax-wildcards]]
  283. ===== Wildcards
  284. When comparing strings using the `==` or `!=` operators, you can use the `*`
  285. operator within the string to match specific patterns:
  286. [source,eql]
  287. ----
  288. field == "example*wildcard"
  289. field != "example*wildcard"
  290. ----
  291. [discrete]
  292. [[eql-sequences]]
  293. === Sequences
  294. You can use EQL sequences to describe and match an ordered series of events.
  295. Each item in a sequence is an event category and event condition,
  296. surrounded by square brackets (`[ ]`). Events are listed in ascending
  297. chronological order, with the most recent event listed last.
  298. [source,eql]
  299. ----
  300. sequence
  301. [ event_category_1 where condition_1 ]
  302. [ event_category_2 where condition_2 ]
  303. ...
  304. ----
  305. *Example* +
  306. The following EQL sequence query matches this series of ordered events:
  307. . Start with an event with:
  308. +
  309. --
  310. * An event category of `file`
  311. * A `file.extension` of `exe`
  312. --
  313. . Followed by an event with an event category of `process`
  314. [source,eql]
  315. ----
  316. sequence
  317. [ file where file.extension == "exe" ]
  318. [ process where true ]
  319. ----
  320. [discrete]
  321. [[eql-with-maxspan-keywords]]
  322. ==== `with maxspan` keywords
  323. You can use the `with maxspan` keywords to constrain a sequence to a specified
  324. timespan. All events in a matching sequence must occur within this duration,
  325. starting at the first event's timestamp.
  326. The `maxspan` keyword accepts <<time-units,time value>> arguments.
  327. [source,eql]
  328. ----
  329. sequence with maxspan=30s
  330. [ event_category_1 where condition_1 ] by field_baz
  331. [ event_category_2 where condition_2 ] by field_bar
  332. ...
  333. ----
  334. *Example* +
  335. The following sequence query uses a `maxspan` value of `15m` (15 minutes).
  336. Events in a matching sequence must occur within 15 minutes of the first event's
  337. timestamp.
  338. [source,eql]
  339. ----
  340. sequence with maxspan=15m
  341. [ file where file.extension == "exe" ]
  342. [ process where true ]
  343. ----
  344. [discrete]
  345. [[eql-by-keyword]]
  346. ==== `by` keyword
  347. You can use the `by` keyword with sequences to only match events that share the
  348. same field values. If a field value should be shared across all events, you
  349. can use `sequence by`.
  350. [source,eql]
  351. ----
  352. sequence by field_foo
  353. [ event_category_1 where condition_1 ] by field_baz
  354. [ event_category_2 where condition_2 ] by field_bar
  355. ...
  356. ----
  357. *Example* +
  358. The following sequence query uses the `by` keyword to constrain matching events
  359. to:
  360. * Events with the same `user.name` value
  361. * `file` events with a `file.path` value equal to the following `process`
  362. event's `process.path` value.
  363. [source,eql]
  364. ----
  365. sequence
  366. [ file where file.extension == "exe" ] by user.name, file.path
  367. [ process where true ] by user.name, process.path
  368. ----
  369. Because the `user.name` field is shared across all events in the sequence, it
  370. can be included using `sequence by`. The following sequence is equivalent to the
  371. prior one.
  372. [source,eql]
  373. ----
  374. sequence by user.name
  375. [ file where file.extension == "exe" ] by file.path
  376. [ process where true ] by process.path
  377. ----
  378. You can combine the `sequence by` and `with maxspan` keywords to constrain a
  379. sequence by both field values and a timespan.
  380. [source,eql]
  381. ----
  382. sequence by field_foo with maxspan=30s
  383. [ event_category_1 where condition_1 ] by field_baz
  384. [ event_category_2 where condition_2 ] by field_bar
  385. ...
  386. ----
  387. *Example* +
  388. The following sequence query uses the `sequence by` keyword and `with maxspan`
  389. keywords to match only a sequence of events that:
  390. * Share the same `user.name` field values
  391. * Occur within `15m` (15 minutes) of the first matching event
  392. [source,eql]
  393. ----
  394. sequence by user.name with maxspan=15m
  395. [ file where file.extension == "exe" ] by file.path
  396. [ process where true ] by process.path
  397. ----
  398. [discrete]
  399. [[eql-until-keyword]]
  400. ==== `until` keyword
  401. You can use the `until` keyword to specify an expiration event for a sequence.
  402. If this expiration event occurs _between_ matching events in a sequence, the
  403. sequence expires and is not considered a match. If the expiration event occurs
  404. _after_ matching events in a sequence, the sequence is still considered a
  405. match. The expiration event is not included in the results.
  406. [source,eql]
  407. ----
  408. sequence
  409. [ event_category_1 where condition_1 ]
  410. [ event_category_2 where condition_2 ]
  411. ...
  412. until [ event_category_3 where condition_3 ]
  413. ----
  414. *Example* +
  415. A dataset contains the following event sequences, grouped by shared IDs:
  416. [source,txt]
  417. ----
  418. A, B
  419. A, B, C
  420. A, C, B
  421. ----
  422. The following EQL query searches the dataset for sequences containing
  423. event `A` followed by event `B`. Event `C` is used as an expiration event.
  424. [source,eql]
  425. ----
  426. sequence by ID
  427. A
  428. B
  429. until C
  430. ----
  431. The query matches sequences `A, B` and `A, B, C` but not `A, C, B`.
  432. [TIP]
  433. ====
  434. The `until` keyword can be useful when searching for process sequences in
  435. Windows event logs.
  436. In Windows, a process ID (PID) is unique only while a process is running. After
  437. a process terminates, its PID can be reused.
  438. You can search for a sequence of events with the same PID value using the `by`
  439. and `sequence by` keywords.
  440. *Example* +
  441. The following EQL query uses the `sequence by` keyword to match a
  442. sequence of events that share the same `process.pid` value.
  443. [source,eql]
  444. ----
  445. sequence by process.pid
  446. [ process where event.type == "start" and process.name == "cmd.exe" ]
  447. [ process where file.extension == "exe" ]
  448. ----
  449. However, due to PID reuse, this can result in a matching sequence that
  450. contains events across unrelated processes. To prevent false positives, you can
  451. use the `until` keyword to end matching sequences before a process termination
  452. event.
  453. The following EQL query uses the `until` keyword to end sequences before
  454. `process` events with an `event.type` of `stop`. These events indicate a process
  455. has been terminated.
  456. [source,eql]
  457. ----
  458. sequence by process.pid
  459. [ process where event.type == "start" and process.name == "cmd.exe" ]
  460. [ process where file.extension == "exe" ]
  461. until [ process where event.type == "stop" ]
  462. ----
  463. ====
  464. [discrete]
  465. [[eql-functions]]
  466. === Functions
  467. {es} supports several of EQL's built-in functions. You can use these functions
  468. to convert data types, perform math, manipulate strings, and more. Most
  469. functions are case-sensitive by default.
  470. For a list of supported functions, see <<eql-function-ref>>.
  471. [TIP]
  472. ====
  473. Using functions in EQL queries can result in slower search speeds. If you
  474. often use functions to transform indexed data, you can speed up search by making
  475. these changes during indexing instead. However, that often means slower index
  476. speeds.
  477. *Example* +
  478. An index contains the `file.path` field. `file.path` contains the full path to a
  479. file, including the file extension.
  480. When running EQL searches, users often use the `endsWith` function with the
  481. `file.path` field to match file extensions:
  482. [source,eql]
  483. ----
  484. file where endsWith(file.path,".exe") or endsWith(file.path,".dll")
  485. ----
  486. While this works, it can be repetitive to write and can slow search speeds. To
  487. speed up search, you can do the following instead:
  488. . <<indices-put-mapping,Add a new field>>, `file.extension`, to the index. The
  489. `file.extension` field will contain only the file extension from the
  490. `file.path` field.
  491. . Use an <<ingest,ingest pipeline>> containing the <<grok-processor,`grok`>>
  492. processor or another preprocessor tool to extract the file extension from the
  493. `file.path` field before indexing.
  494. . Index the extracted file extension to the `file.extension` field.
  495. These changes may slow indexing but allow for faster searches. Users
  496. can use the `file.extension` field instead of multiple `endsWith` function
  497. calls:
  498. [source,eql]
  499. ----
  500. file where file.extension in ("exe", "dll")
  501. ----
  502. We recommend testing and benchmarking any indexing changes before deploying them
  503. in production. See <<tune-for-indexing-speed>> and <<tune-for-search-speed>>.
  504. ====
  505. [discrete]
  506. [[eql-pipes]]
  507. === Pipes
  508. EQL pipes filter, aggregate, and post-process events returned by
  509. an EQL query. You can use pipes to narrow down EQL query results or make them
  510. more specific.
  511. Pipes are delimited using the pipe (`|`) character.
  512. [source,eql]
  513. ----
  514. event_category where condition | pipe
  515. ----
  516. *Example* +
  517. The following EQL query uses the `tail` pipe to return only the 10 most recent
  518. events matching the query.
  519. [source,eql]
  520. ----
  521. authentication where agent.id == 4624
  522. | tail 10
  523. ----
  524. You can pass the output of a pipe to another pipe. This lets you use multiple
  525. pipes with a single query.
  526. For a list of supported pipes, see <<eql-pipe-ref>>.
  527. [discrete]
  528. [[eql-syntax-limitations]]
  529. === Limitations
  530. {es} EQL does not support the following features and syntax.
  531. [discrete]
  532. [[eql-compare-fields]]
  533. ==== Comparing fields
  534. In {es} EQL, you cannot use comparison operators to compare a field to
  535. another field. This applies even if the fields are changed using a
  536. <<eql-functions,function>>.
  537. [discrete]
  538. [[eql-nested-fields]]
  539. ==== EQL search on nested fields
  540. You cannot use EQL to search the values of a <<nested,`nested`>> field or the
  541. sub-fields of a `nested` field. However, data streams and indices containing
  542. `nested` field mappings are otherwise supported.
  543. [discrete]
  544. [[single-quote-strings]]
  545. ==== Single quote strings
  546. In {es} EQL, the single quote (`'`) character is reserved for future use.
  547. Strings enclosed in single quotes are not supported. Enclose strings in
  548. double quotes (`"`) instead.
  549. You cannot use an escaped single quote (`\'`) for literal strings. Use an
  550. escaped double quote (`\"`) instead.
  551. [discrete]
  552. [[eql-unsupported-syntax]]
  553. ==== Unsupported syntax
  554. {es} supports a subset of {eql-ref}/index.html[EQL syntax]. {es} cannot run EQL
  555. queries that contain:
  556. * Array functions:
  557. ** {eql-ref}/functions.html#arrayContains[`arrayContains`]
  558. ** {eql-ref}/functions.html#arrayCount[`arrayCount`]
  559. ** {eql-ref}/functions.html#arraySearch[`arraySearch`]
  560. * {eql-ref}/joins.html[Joins]
  561. * {eql-ref}/basic-syntax.html#event-relationships[Lineage-related keywords]:
  562. ** `child of`
  563. ** `descendant of`
  564. ** `event of`
  565. * The following {eql-ref}/pipes.html[pipes]:
  566. ** {eql-ref}/pipes.html#count[`count`]
  567. ** {eql-ref}/pipes.html#filter[`filter`]
  568. ** {eql-ref}/pipes.html#sort[`sort`]
  569. ** {eql-ref}/pipes.html#unique[`unique`]
  570. ** {eql-ref}/pipes.html#unique-count[`unique_count`]