syntax.asciidoc 18 KB

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