eql-search-api.asciidoc 17 KB

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