eql-search-api.asciidoc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. [role="xpack"]
  2. [[eql-search-api]]
  3. === EQL search API
  4. ++++
  5. <titleabbrev>EQL search</titleabbrev>
  6. ++++
  7. Returns search results for an <<eql,Event Query Language (EQL)>> query.
  8. EQL assumes each document in a data stream or index corresponds to an
  9. event.
  10. ////
  11. [source,console]
  12. ----
  13. DELETE /_data_stream/*
  14. DELETE /_index_template/*
  15. ----
  16. // TEARDOWN
  17. ////
  18. [source,console]
  19. ----
  20. GET /my-data-stream/_eql/search
  21. {
  22. "query": """
  23. process where process.name == "regsvr32.exe"
  24. """
  25. }
  26. ----
  27. // TEST[setup:sec_logs]
  28. [[eql-search-api-request]]
  29. ==== {api-request-title}
  30. `GET /<target>/_eql/search`
  31. `POST /<target>/_eql/search`
  32. [[eql-search-api-prereqs]]
  33. ==== {api-prereq-title}
  34. * If the {es} {security-features} are enabled, you must have the `read`
  35. <<privileges-list-indices,index privilege>> for the target data stream, index,
  36. or alias.
  37. * See <<eql-required-fields>>.
  38. * experimental:[] For cross-cluster search, the local and remote clusters must
  39. use the same {es} version if they have versions prior to 7.17.7 (included) or prior to 8.5.1 (included). For security, see <<remote-clusters-security>>.
  40. [[eql-search-api-limitations]]
  41. ===== Limitations
  42. See <<eql-syntax-limitations,EQL limitations>>.
  43. [[eql-search-api-path-params]]
  44. ==== {api-path-parms-title}
  45. `<target>`::
  46. (Required, string) Comma-separated list of data streams, indices, or aliases
  47. used to limit the request. Supports wildcards (`*`). To search all data streams
  48. and indices, use `*` or `_all`.
  49. +
  50. experimental:[] To search a remote cluster, use the `<cluster>:<target>` syntax.
  51. See <<run-eql-search-across-clusters>>.
  52. [[eql-search-api-query-params]]
  53. ==== {api-query-parms-title}
  54. `allow_no_indices`::
  55. (Optional, Boolean)
  56. +
  57. NOTE: This parameter's behavior differs from the `allow_no_indices` parameter
  58. used in other <<multi-index,multi-target APIs>>.
  59. +
  60. If `false`, the request returns an error if any wildcard pattern, alias, or
  61. `_all` value targets only missing or closed indices. This behavior applies even
  62. if the request targets other open indices. For example, a request targeting
  63. `foo*,bar*` returns an error if an index starts with `foo` but no index starts
  64. with `bar`.
  65. +
  66. If `true`, only requests that exclusively target missing or closed indices
  67. return an error. For example, a request targeting `foo*,bar*` does not return an
  68. error if an index starts with `foo` but no index starts with `bar`. However, a
  69. request that targets only `bar*` still returns an error.
  70. +
  71. Defaults to `true`.
  72. `ccs_minimize_roundtrips`::
  73. (Optional, Boolean) If `true`, network round-trips between the local and the
  74. remote cluster are minimized when running cross-cluster search (CCS) requests.
  75. +
  76. This option is effective for requests that target data fully contained in one
  77. remote cluster; when data is spread across multiple clusters, the setting is
  78. ignored.
  79. +
  80. Defaults to `true`.
  81. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
  82. +
  83. Defaults to `open`.
  84. `filter_path`::
  85. (Optional, string)
  86. Comma-separated list of filters for the API response. See
  87. <<common-options-response-filtering>>.
  88. `ignore_unavailable`::
  89. (Optional, Boolean) If `false`, the request returns an error if it targets a
  90. missing or closed index. Defaults to `true`.
  91. `keep_alive`::
  92. +
  93. --
  94. (Optional, <<time-units,time value>>)
  95. Period for which the search and its results are stored on the cluster. Defaults
  96. to `5d` (five days).
  97. When this period expires, the search and its results are deleted, even if the
  98. search is still ongoing.
  99. If the <<eql-search-api-keep-on-completion,`keep_on_completion`>> parameter is
  100. `false`, {es} only stores <<eql-search-async,async searches>> that do not
  101. complete within the period set by the
  102. <<eql-search-api-wait-for-completion-timeout,`wait_for_completion_timeout`>>
  103. parameter, regardless of this value.
  104. [IMPORTANT]
  105. ====
  106. You can also specify this value using the `keep_alive` request body parameter.
  107. If both parameters are specified, only the query parameter is used.
  108. ====
  109. --
  110. `keep_on_completion`::
  111. +
  112. --
  113. (Optional, Boolean)
  114. If `true`, the search and its results are stored on the cluster.
  115. If `false`, the search and its results are stored on the cluster only if the
  116. request does not complete during the period set by the
  117. <<eql-search-api-wait-for-completion-timeout,`wait_for_completion_timeout`>>
  118. parameter. Defaults to `false`.
  119. [IMPORTANT]
  120. ====
  121. You can also specify this value using the `keep_on_completion` request body
  122. parameter. If both parameters are specified, only the query parameter is used.
  123. ====
  124. --
  125. `wait_for_completion_timeout`::
  126. +
  127. --
  128. (Optional, <<time-units,time value>>)
  129. Timeout duration to wait for the request to finish. Defaults to no
  130. timeout, meaning the request waits for complete search results.
  131. If this parameter is specified and the request completes during this period,
  132. complete search results are returned.
  133. If the request does not complete during this period, the search becomes an
  134. <<eql-search-async,async search>>.
  135. [IMPORTANT]
  136. ====
  137. You can also specify this value using the `wait_for_completion_timeout` request
  138. body parameter. If both parameters are specified, only the query parameter is
  139. used.
  140. ====
  141. --
  142. [role="child_attributes"]
  143. [[eql-search-api-request-body]]
  144. ==== {api-request-body-title}
  145. `event_category_field`::
  146. (Required*, string)
  147. Field containing the event classification, such as `process`, `file`, or
  148. `network`.
  149. +
  150. Defaults to `event.category`, as defined in the {ecs-ref}/ecs-event.html[Elastic
  151. Common Schema (ECS)]. If a data stream or index does not contain the
  152. `event.category` field, this value is required.
  153. +
  154. The event category field must be mapped as a field type in the
  155. <<keyword,`keyword`>> family.
  156. `fetch_size`::
  157. (Optional, integer)
  158. Maximum number of events to search at a time for sequence queries. Defaults to
  159. `1000`.
  160. +
  161. This value must be greater than `2` but cannot exceed the value of the
  162. <<index-max-result-window,`index.max_result_window`>> setting, which defaults to
  163. `10000`.
  164. +
  165. Internally, a sequence query fetches and paginates sets of events to search for
  166. matches. This parameter controls the size of those sets. This parameter does not
  167. limit the total number of events searched or the number of matching events
  168. returned.
  169. +
  170. A greater `fetch_size` value often increases search speed but uses more memory.
  171. include::{es-repo-dir}/search/search.asciidoc[tag=fields-param-def]
  172. `filter`::
  173. (Optional, <<query-dsl,Query DSL object>>)
  174. Query, written in Query DSL, used to filter the events on which the EQL query
  175. runs.
  176. `keep_alive`::
  177. +
  178. --
  179. (Optional, <<time-units,time value>>)
  180. Period for which the search and its results are stored on the cluster. Defaults
  181. to `5d` (five days).
  182. When this period expires, the search and its results are deleted, even if the
  183. search is still ongoing.
  184. If the <<eql-search-api-keep-on-completion,`keep_on_completion`>> parameter is
  185. `false`, {es} only stores <<eql-search-async,async searches>> that do not
  186. complete within the period set by the
  187. <<eql-search-api-wait-for-completion-timeout,`wait_for_completion_timeout`>>
  188. parameter, regardless of this value.
  189. [IMPORTANT]
  190. ====
  191. You can also specify this value using the `keep_alive` query parameter.
  192. If both parameters are specified, only the query parameter is used.
  193. ====
  194. --
  195. [[eql-search-api-keep-on-completion]]
  196. `keep_on_completion`::
  197. +
  198. --
  199. (Optional, Boolean)
  200. If `true`, the search and its results are stored on the cluster.
  201. If `false`, the search and its results are stored on the cluster only if the
  202. request does not complete during the period set by the
  203. <<eql-search-api-wait-for-completion-timeout,`wait_for_completion_timeout`>>
  204. parameter. Defaults to `false`.
  205. [IMPORTANT]
  206. ====
  207. You can also specify this value using the `keep_on_completion` query parameter.
  208. If both parameters are specified, only the query parameter is used.
  209. ====
  210. --
  211. [[eql-search-api-request-query-param]]
  212. `query`::
  213. (Required, string)
  214. <<eql-syntax,EQL>> query you wish to run.
  215. `result_position`::
  216. (Optional, enum)
  217. Set of matching events or sequences to return.
  218. +
  219. .Valid values for `result_position`
  220. [%collapsible%open]
  221. ====
  222. `tail`::
  223. (Default)
  224. Return the most recent matches, similar to the {wikipedia}/Tail_(Unix)[Unix tail
  225. command].
  226. `head`::
  227. Return the earliest matches, similar to the {wikipedia}/Head_(Unix)[Unix head
  228. command].
  229. ====
  230. +
  231. NOTE: This parameter may change the set of returned hits. However, it does not
  232. change the sort order of hits in the response.
  233. include::{es-repo-dir}/search/search.asciidoc[tag=runtime-mappings-def]
  234. [[eql-search-api-params-size]]
  235. `size`::
  236. (Optional, integer or float)
  237. For <<eql-basic-syntax,basic queries>>, the maximum number of matching events to
  238. return.
  239. +
  240. For <<eql-sequences,sequence queries>>, the maximum number of matching sequences
  241. to return.
  242. +
  243. Defaults to `10`. This value must be greater than `0`.
  244. +
  245. NOTE: You cannot use <<eql-pipe-ref,pipes>>, such as `head` or `tail`, to exceed
  246. this value.
  247. [[eql-search-api-tiebreaker-field]]
  248. `tiebreaker_field`::
  249. (Optional, string)
  250. Field used to sort hits with the same
  251. <<eql-search-api-timestamp-field,timestamp>> in ascending order. See
  252. <<eql-search-specify-a-sort-tiebreaker>>.
  253. [[eql-search-api-timestamp-field]]
  254. `timestamp_field`::
  255. +
  256. --
  257. (Required*, string)
  258. Field containing event timestamp.
  259. Defaults to `@timestamp`, as defined in the
  260. {ecs-ref}/ecs-event.html[Elastic Common Schema (ECS)]. If a data stream or index
  261. does not contain the `@timestamp` field, this value is required.
  262. Events in the API response are sorted by this field's value, converted to
  263. milliseconds since the {wikipedia}/Unix_time[Unix epoch], in
  264. ascending order.
  265. The timestamp field should be mapped as a <<date,`date`>>. The
  266. <<date_nanos,`date_nanos`>> field type is not supported.
  267. --
  268. [[eql-search-api-wait-for-completion-timeout]]
  269. `wait_for_completion_timeout`::
  270. +
  271. --
  272. (Optional, <<time-units,time value>>)
  273. Timeout duration to wait for the request to finish. Defaults to no
  274. timeout, meaning the request waits for complete search results.
  275. If this parameter is specified and the request completes during this period,
  276. complete search results are returned.
  277. If the request does not complete during this period, the search becomes an
  278. <<eql-search-async,async search>>.
  279. [IMPORTANT]
  280. ====
  281. You can also specify this value using the `wait_for_completion_timeout` query
  282. parameter. If both parameters are specified, only the query parameter is used.
  283. ====
  284. --
  285. [role="child_attributes"]
  286. [[eql-search-api-response-body]]
  287. ==== {api-response-body-title}
  288. [[eql-search-api-response-body-search-id]]
  289. `id`::
  290. +
  291. --
  292. (string)
  293. Identifier for the search.
  294. This search ID is only provided if one of the following conditions is met:
  295. * A search request does not return complete results during the
  296. <<eql-search-api-wait-for-completion-timeout,`wait_for_completion_timeout`>>
  297. parameter's timeout period, becoming an <<eql-search-async,async search>>.
  298. * The search request's <<eql-search-api-keep-on-completion,`keep_on_completion`>>
  299. parameter is `true`.
  300. You can use this ID with the <<get-async-eql-search-api,get async EQL search
  301. API>> to get the current status and available results for the search or
  302. <<get-async-eql-status-api,get async EQL status API>> to get only
  303. the current status.
  304. --
  305. `is_partial`::
  306. (Boolean)
  307. If `true`, the response does not contain complete search results.
  308. `is_running`::
  309. +
  310. --
  311. (Boolean)
  312. If `true`, the search request is still executing.
  313. [IMPORTANT]
  314. ====
  315. If this parameter and the `is_partial` parameter are `true`, the search is an
  316. <<eql-search-async,ongoing async search>>. If the `keep_alive` period does not
  317. pass, the complete search results will be available when the search completes.
  318. If `is_partial` is `true` but `is_running` is `false`, the search returned
  319. partial results due to a failure. Only some shards returned results or the node
  320. coordinating the search failed.
  321. ====
  322. --
  323. `took`::
  324. +
  325. --
  326. (integer)
  327. Milliseconds it took {es} to execute the request.
  328. This value is calculated by measuring the time elapsed
  329. between receipt of a request on the coordinating node
  330. and the time at which the coordinating node is ready to send the response.
  331. Took time includes:
  332. * Communication time between the coordinating node and data nodes
  333. * Time the request spends in the `search` <<modules-threadpool,thread pool>>,
  334. queued for execution
  335. * Actual execution time
  336. Took time does *not* include:
  337. * Time needed to send the request to {es}
  338. * Time needed to serialize the JSON response
  339. * Time needed to send the response to a client
  340. --
  341. `timed_out`::
  342. (Boolean)
  343. If `true`, the request timed out before completion.
  344. `hits`::
  345. (object)
  346. Contains matching events and sequences. Also contains related metadata.
  347. +
  348. .Properties of `hits`
  349. [%collapsible%open]
  350. ====
  351. `total`::
  352. (object)
  353. Metadata about the number of matching events or sequences.
  354. +
  355. .Properties of `total`
  356. [%collapsible%open]
  357. =====
  358. `value`::
  359. (integer)
  360. For <<eql-basic-syntax,basic queries>>, the total number of matching events.
  361. +
  362. For <<eql-sequences,sequence queries>>, the total number of matching sequences.
  363. `relation`::
  364. +
  365. --
  366. (string)
  367. Indicates whether the number of events or sequences returned is accurate or a
  368. lower bound.
  369. Returned values are:
  370. `eq`::: Accurate
  371. `gte`::: Lower bound, including returned events or sequences
  372. --
  373. =====
  374. `sequences`::
  375. (array of objects)
  376. Contains event sequences matching the query. Each object represents a
  377. matching sequence. This parameter is only returned for EQL queries containing
  378. a <<eql-sequences,sequence>>.
  379. +
  380. .Properties of `sequences` objects
  381. [%collapsible%open]
  382. =====
  383. `join_keys`::
  384. (array of values)
  385. Shared field values used to constrain matches in the sequence. These are defined
  386. using the <<eql-sequences,`by` keyword>> in the EQL query syntax.
  387. `events`::
  388. (array of objects)
  389. Contains events matching the query. Each object represents a
  390. matching event.
  391. +
  392. .Properties of `events` objects
  393. [%collapsible%open]
  394. ======
  395. `_index`::
  396. (string)
  397. Name of the index containing the event.
  398. `_id`::
  399. (string)
  400. Unique identifier for the event.
  401. This ID is only unique within the index.
  402. `_source`::
  403. (object)
  404. Original JSON body passed for the event at index time.
  405. ======
  406. =====
  407. [[eql-search-api-response-events]]
  408. `events`::
  409. (array of objects)
  410. Contains events matching the query. Each object represents a
  411. matching event.
  412. +
  413. .Properties of `events` objects
  414. [%collapsible%open]
  415. =====
  416. `_index`::
  417. (string)
  418. Name of the index containing the event.
  419. `_id`::
  420. (string)
  421. (string)
  422. Unique identifier for the event.
  423. This ID is only unique within the index.
  424. `_source`::
  425. (object)
  426. Original JSON body passed for the event at index time.
  427. =====
  428. ====
  429. [[eql-search-api-example]]
  430. ==== {api-examples-title}
  431. [[eql-search-api-basic-query-ex]]
  432. ===== Basic query example
  433. The following EQL search request searches for events with an `event.category` of
  434. `process` that meet the following conditions:
  435. * A `process.name` of `cmd.exe`
  436. * An `process.pid` other than `2013`
  437. [source,console]
  438. ----
  439. GET /my-data-stream/_eql/search
  440. {
  441. "query": """
  442. process where (process.name == "cmd.exe" and process.pid != 2013)
  443. """
  444. }
  445. ----
  446. // TEST[setup:sec_logs]
  447. The API returns the following response. Matching events in the `hits.events`
  448. property are sorted by <<eql-search-api-timestamp-field,timestamp>>, converted
  449. to milliseconds since the {wikipedia}/Unix_time[Unix epoch],
  450. in ascending order.
  451. If two or more events share the same timestamp, the
  452. <<eql-search-api-tiebreaker-field,`tiebreaker_field`>> field is used to sort
  453. the events in ascending order.
  454. [source,console-result]
  455. ----
  456. {
  457. "is_partial": false,
  458. "is_running": false,
  459. "took": 6,
  460. "timed_out": false,
  461. "hits": {
  462. "total": {
  463. "value": 2,
  464. "relation": "eq"
  465. },
  466. "events": [
  467. {
  468. "_index": ".ds-my-data-stream-2099.12.07-000001",
  469. "_id": "babI3XMBI9IjHuIqU0S_",
  470. "_source": {
  471. "@timestamp": "2099-12-06T11:04:05.000Z",
  472. "event": {
  473. "category": "process",
  474. "id": "edwCRnyD",
  475. "sequence": 1
  476. },
  477. "process": {
  478. "pid": 2012,
  479. "name": "cmd.exe",
  480. "executable": "C:\\Windows\\System32\\cmd.exe"
  481. }
  482. }
  483. },
  484. {
  485. "_index": ".ds-my-data-stream-2099.12.07-000001",
  486. "_id": "b6bI3XMBI9IjHuIqU0S_",
  487. "_source": {
  488. "@timestamp": "2099-12-07T11:06:07.000Z",
  489. "event": {
  490. "category": "process",
  491. "id": "cMyt5SZ2",
  492. "sequence": 3
  493. },
  494. "process": {
  495. "pid": 2012,
  496. "name": "cmd.exe",
  497. "executable": "C:\\Windows\\System32\\cmd.exe"
  498. }
  499. }
  500. }
  501. ]
  502. }
  503. }
  504. ----
  505. // TESTRESPONSE[s/"took": 6/"took": $body.took/]
  506. // TESTRESPONSE[s/"_index": ".ds-my-data-stream-2099.12.07-000001"/"_index": $body.hits.events.0._index/]
  507. // TESTRESPONSE[s/"_id": "babI3XMBI9IjHuIqU0S_"/"_id": $body.hits.events.0._id/]
  508. // TESTRESPONSE[s/"_id": "b6bI3XMBI9IjHuIqU0S_"/"_id": $body.hits.events.1._id/]
  509. [[eql-search-api-sequence-ex]]
  510. ===== Sequence query example
  511. The following EQL search request matches a <<eql-sequences,sequence>> of events
  512. that:
  513. . Start with an event with:
  514. +
  515. --
  516. * An `event.category` of `file`
  517. * A `file.name` of `cmd.exe`
  518. * An `process.pid` other than `2013`
  519. --
  520. . Followed by an event with:
  521. +
  522. --
  523. * An `event.category` of `process`
  524. * A `process.executable` that contains the substring `regsvr32`
  525. --
  526. These events must also share the same `process.pid` value.
  527. [source,console]
  528. ----
  529. GET /my-data-stream/_eql/search
  530. {
  531. "query": """
  532. sequence by process.pid
  533. [ file where file.name == "cmd.exe" and process.pid != 2013 ]
  534. [ process where stringContains(process.executable, "regsvr32") ]
  535. """
  536. }
  537. ----
  538. // TEST[setup:sec_logs]
  539. The API returns the following response. Matching sequences are included in the
  540. `hits.sequences` property. The `hits.sequences.join_keys` property contains the
  541. shared `process.pid` value for each matching event.
  542. [source,console-result]
  543. ----
  544. {
  545. "is_partial": false,
  546. "is_running": false,
  547. "took": 6,
  548. "timed_out": false,
  549. "hits": {
  550. "total": {
  551. "value": 1,
  552. "relation": "eq"
  553. },
  554. "sequences": [
  555. {
  556. "join_keys": [
  557. 2012
  558. ],
  559. "events": [
  560. {
  561. "_index": ".ds-my-data-stream-2099.12.07-000001",
  562. "_id": "AtOJ4UjUBAAx3XR5kcCM",
  563. "_source": {
  564. "@timestamp": "2099-12-06T11:04:07.000Z",
  565. "event": {
  566. "category": "file",
  567. "id": "dGCHwoeS",
  568. "sequence": 2
  569. },
  570. "file": {
  571. "accessed": "2099-12-07T11:07:08.000Z",
  572. "name": "cmd.exe",
  573. "path": "C:\\Windows\\System32\\cmd.exe",
  574. "type": "file",
  575. "size": 16384
  576. },
  577. "process": {
  578. "pid": 2012,
  579. "name": "cmd.exe",
  580. "executable": "C:\\Windows\\System32\\cmd.exe"
  581. }
  582. }
  583. },
  584. {
  585. "_index": ".ds-my-data-stream-2099.12.07-000001",
  586. "_id": "OQmfCaduce8zoHT93o4H",
  587. "_source": {
  588. "@timestamp": "2099-12-07T11:07:09.000Z",
  589. "event": {
  590. "category": "process",
  591. "id": "aR3NWVOs",
  592. "sequence": 4
  593. },
  594. "process": {
  595. "pid": 2012,
  596. "name": "regsvr32.exe",
  597. "command_line": "regsvr32.exe /s /u /i:https://...RegSvr32.sct scrobj.dll",
  598. "executable": "C:\\Windows\\System32\\regsvr32.exe"
  599. }
  600. }
  601. }
  602. ]
  603. }
  604. ]
  605. }
  606. }
  607. ----
  608. // TESTRESPONSE[s/"took": 6/"took": $body.took/]
  609. // TESTRESPONSE[s/"_index": ".ds-my-data-stream-2099.12.07-000001"/"_index": $body.hits.sequences.0.events.0._index/]
  610. // TESTRESPONSE[s/"_id": "AtOJ4UjUBAAx3XR5kcCM"/"_id": $body.hits.sequences.0.events.0._id/]
  611. // TESTRESPONSE[s/"_id": "OQmfCaduce8zoHT93o4H"/"_id": $body.hits.sequences.0.events.1._id/]