eql-search-api.asciidoc 17 KB

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