syntax.asciidoc 18 KB

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