syntax.asciidoc 18 KB

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