eql-search-api.asciidoc 18 KB

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