eql-search-api.asciidoc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[eql-search-api]]
  4. === EQL search API
  5. ++++
  6. <titleabbrev>EQL search</titleabbrev>
  7. ++++
  8. experimental::[]
  9. Returns search results for an <<eql,Event Query Language (EQL)>> query.
  10. In {es}, 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. +
  173. IMPORTANT: This parameter supports a subset of EQL syntax. See
  174. <<eql-unsupported-syntax>>.
  175. `size`::
  176. (Optional, integer or float)
  177. For <<eql-basic-syntax,basic queries>>, the maximum number of matching events to
  178. return.
  179. +
  180. For <<eql-sequences,sequence queries>>, the maximum number of matching sequences
  181. to return.
  182. +
  183. Defaults to `10`. This value must be greater than `0`.
  184. +
  185. NOTE: You cannot use <<eql-pipe-ref,pipes>>, such as `head` or `tail`, to exceed
  186. this value.
  187. [[eql-search-api-tiebreaker-field]]
  188. `tiebreaker_field`::
  189. (Optional, string)
  190. Field used to sort events with the same
  191. <<eql-search-api-timestamp-field,timestamp field>> value. Defaults to
  192. `event.sequence`, as defined in the {ecs-ref}/ecs-event.html[Elastic Common
  193. Schema (ECS)].
  194. +
  195. By default, matching events in the search response are sorted by timestamp,
  196. converted to milliseconds since the {wikipedia}/Unix_time[Unix
  197. epoch], in ascending order. If two or more events share the same timestamp, this
  198. field is used to sort the events in ascending, lexicographic order.
  199. [[eql-search-api-timestamp-field]]
  200. `timestamp_field`::
  201. +
  202. --
  203. (Required*, string)
  204. Field containing event timestamp.
  205. Defaults to `@timestamp`, as defined in the
  206. {ecs-ref}/ecs-event.html[Elastic Common Schema (ECS)]. If a data stream or index
  207. does not contain the `@timestamp` field, this value is required.
  208. Events in the API response are sorted by this field's value, converted to
  209. milliseconds since the {wikipedia}/Unix_time[Unix epoch], in
  210. ascending order.
  211. The timestamp field should be mapped as a <<date,`date`>>. The
  212. <<date_nanos,`date_nanos`>> field type is not supported.
  213. --
  214. [[eql-search-api-wait-for-completion-timeout]]
  215. `wait_for_completion_timeout`::
  216. +
  217. --
  218. (Optional, <<time-units,time value>>)
  219. Timeout duration to wait for the request to finish. Defaults to no
  220. timeout, meaning the request waits for complete search results.
  221. If this parameter is specified and the request completes during this period,
  222. complete search results are returned.
  223. If the request does not complete during this period, the search becomes an
  224. <<eql-search-async,async search>>.
  225. [IMPORTANT]
  226. ====
  227. You can also specify this value using the `wait_for_completion_timeout` query
  228. parameter. If both parameters are specified, only the query parameter is used.
  229. ====
  230. --
  231. [role="child_attributes"]
  232. [[eql-search-api-response-body]]
  233. ==== {api-response-body-title}
  234. [[eql-search-api-response-body-search-id]]
  235. `id`::
  236. +
  237. --
  238. (string)
  239. Identifier for the search.
  240. This search ID is only provided if one of the following conditions is met:
  241. * A search request does not return complete results during the
  242. <<eql-search-api-wait-for-completion-timeout,`wait_for_completion_timeout`>>
  243. parameter's timeout period, becoming an <<eql-search-async,async search>>.
  244. * The search request's <<eql-search-api-keep-on-completion,`keep_on_completion`>>
  245. parameter is `true`.
  246. You can use this ID with the <<get-async-eql-search-api,get async EQL search
  247. API>> to get the current status and available results for the search.
  248. --
  249. `is_partial`::
  250. (boolean)
  251. If `true`, the response does not contain complete search results.
  252. `is_running`::
  253. +
  254. --
  255. (boolean)
  256. If `true`, the search request is still executing.
  257. [IMPORTANT]
  258. ====
  259. If this parameter and the `is_partial` parameter are `true`, the search is an
  260. <<eql-search-async,ongoing async search>>. If the `keep_alive` period does not
  261. pass, the complete search results will be available when the search completes.
  262. If `is_partial` is `true` but `is_running` is `false`, the search returned
  263. partial results due to a failure. Only some shards returned results or the node
  264. coordinating the search failed.
  265. ====
  266. --
  267. `took`::
  268. +
  269. --
  270. (integer)
  271. Milliseconds it took {es} to execute the request.
  272. This value is calculated by measuring the time elapsed
  273. between receipt of a request on the coordinating node
  274. and the time at which the coordinating node is ready to send the response.
  275. Took time includes:
  276. * Communication time between the coordinating node and data nodes
  277. * Time the request spends in the `search` <<modules-threadpool,thread pool>>,
  278. queued for execution
  279. * Actual execution time
  280. Took time does *not* include:
  281. * Time needed to send the request to {es}
  282. * Time needed to serialize the JSON response
  283. * Time needed to send the response to a client
  284. --
  285. `timed_out`::
  286. (boolean)
  287. If `true`, the request timed out before completion.
  288. `hits`::
  289. (object)
  290. Contains matching events and sequences. Also contains related metadata.
  291. +
  292. .Properties of `hits`
  293. [%collapsible%open]
  294. ====
  295. `total`::
  296. (object)
  297. Metadata about the number of matching events or sequences.
  298. +
  299. .Properties of `total`
  300. [%collapsible%open]
  301. =====
  302. `value`::
  303. (integer)
  304. For <<eql-basic-syntax,basic queries>>, the total number of matching events.
  305. +
  306. For <<eql-sequences,sequence queries>>, the total number of matching sequences.
  307. `relation`::
  308. +
  309. --
  310. (string)
  311. Indicates whether the number of events or sequences returned is accurate or a
  312. lower bound.
  313. Returned values are:
  314. `eq`::: Accurate
  315. `gte`::: Lower bound, including returned events or sequences
  316. --
  317. =====
  318. `sequences`::
  319. (array of objects)
  320. Contains event sequences matching the query. Each object represents a
  321. matching sequence. This parameter is only returned for EQL queries containing
  322. a <<eql-sequences,sequence>>.
  323. +
  324. .Properties of `sequences` objects
  325. [%collapsible%open]
  326. =====
  327. `join_keys`::
  328. (array of values)
  329. Shared field values used to constrain matches in the sequence. These are defined
  330. using the <<eql-sequences,`by` keyword>> in the EQL query syntax.
  331. `events`::
  332. (array of objects)
  333. Contains events matching the query. Each object represents a
  334. matching event.
  335. +
  336. .Properties of `events` objects
  337. [%collapsible%open]
  338. ======
  339. `_index`::
  340. (string)
  341. Name of the index containing the event.
  342. `_id`::
  343. (string)
  344. Unique identifier for the event.
  345. This ID is only unique within the index.
  346. `_source`::
  347. (object)
  348. Original JSON body passed for the event at index time.
  349. ======
  350. =====
  351. [[eql-search-api-response-events]]
  352. `events`::
  353. (array of objects)
  354. Contains events matching the query. Each object represents a
  355. matching event.
  356. +
  357. .Properties of `events` objects
  358. [%collapsible%open]
  359. =====
  360. `_index`::
  361. (string)
  362. Name of the index containing the event.
  363. `_id`::
  364. (string)
  365. (string)
  366. Unique identifier for the event.
  367. This ID is only unique within the index.
  368. `_source`::
  369. (object)
  370. Original JSON body passed for the event at index time.
  371. =====
  372. ====
  373. [[eql-search-api-example]]
  374. ==== {api-examples-title}
  375. [[eql-search-api-basic-query-ex]]
  376. ===== Basic query example
  377. The following EQL search request searches for events with an `event.category` of
  378. `process` that meet the following conditions:
  379. * A `process.name` of `cmd.exe`
  380. * An `process.pid` other than `2013`
  381. [source,console]
  382. ----
  383. GET /my-index-000001/_eql/search
  384. {
  385. "query": """
  386. process where (process.name == "cmd.exe" and process.pid != 2013)
  387. """
  388. }
  389. ----
  390. // TEST[setup:sec_logs]
  391. The API returns the following response. Matching events in the `hits.events`
  392. property are sorted by <<eql-search-api-timestamp-field,timestamp>>, converted
  393. to milliseconds since the {wikipedia}/Unix_time[Unix epoch],
  394. in ascending order.
  395. If two or more events share the same timestamp, the
  396. <<eql-search-api-tiebreaker-field,`tiebreaker_field`>> field is used to sort
  397. the events in ascending, lexicographic order.
  398. [source,console-result]
  399. ----
  400. {
  401. "is_partial": false,
  402. "is_running": false,
  403. "took": 6,
  404. "timed_out": false,
  405. "hits": {
  406. "total": {
  407. "value": 2,
  408. "relation": "eq"
  409. },
  410. "events": [
  411. {
  412. "_index": "my-index-000001",
  413. "_id": "babI3XMBI9IjHuIqU0S_",
  414. "_source": {
  415. "@timestamp": "2099-12-06T11:04:05.000Z",
  416. "event": {
  417. "category": "process",
  418. "id": "edwCRnyD",
  419. "sequence": 1
  420. },
  421. "process": {
  422. "pid": 2012,
  423. "name": "cmd.exe",
  424. "executable": "C:\\Windows\\System32\\cmd.exe"
  425. }
  426. }
  427. },
  428. {
  429. "_index": "my-index-000001",
  430. "_id": "b6bI3XMBI9IjHuIqU0S_",
  431. "_source": {
  432. "@timestamp": "2099-12-07T11:06:07.000Z",
  433. "event": {
  434. "category": "process",
  435. "id": "cMyt5SZ2",
  436. "sequence": 3
  437. },
  438. "process": {
  439. "pid": 2012,
  440. "name": "cmd.exe",
  441. "executable": "C:\\Windows\\System32\\cmd.exe"
  442. }
  443. }
  444. }
  445. ]
  446. }
  447. }
  448. ----
  449. // TESTRESPONSE[s/"took": 6/"took": $body.took/]
  450. // TESTRESPONSE[s/"_id": "babI3XMBI9IjHuIqU0S_"/"_id": $body.hits.events.0._id/]
  451. // TESTRESPONSE[s/"_id": "b6bI3XMBI9IjHuIqU0S_"/"_id": $body.hits.events.1._id/]
  452. [[eql-search-api-sequence-ex]]
  453. ===== Sequence query example
  454. The following EQL search request matches a <<eql-sequences,sequence>> of events
  455. that:
  456. . Start with an event with:
  457. +
  458. --
  459. * An `event.category` of `file`
  460. * A `file.name` of `cmd.exe`
  461. * An `process.pid` other than `2013`
  462. --
  463. . Followed by an event with:
  464. +
  465. --
  466. * An `event.category` of `process`
  467. * A `process.executable` that contains the substring `regsvr32`
  468. --
  469. These events must also share the same `process.pid` value.
  470. [source,console]
  471. ----
  472. GET /my-index-000001/_eql/search
  473. {
  474. "query": """
  475. sequence by process.pid
  476. [ file where file.name == "cmd.exe" and process.pid != 2013 ]
  477. [ process where stringContains(process.executable, "regsvr32") ]
  478. """
  479. }
  480. ----
  481. // TEST[setup:sec_logs]
  482. The API returns the following response. Matching sequences are included in the
  483. `hits.sequences` property. The `hits.sequences.join_keys` property contains the
  484. shared `process.pid` value for each matching event.
  485. [source,console-result]
  486. ----
  487. {
  488. "is_partial": false,
  489. "is_running": false,
  490. "took": 6,
  491. "timed_out": false,
  492. "hits": {
  493. "total": {
  494. "value": 1,
  495. "relation": "eq"
  496. },
  497. "sequences": [
  498. {
  499. "join_keys": [
  500. 2012
  501. ],
  502. "events": [
  503. {
  504. "_index": "my-index-000001",
  505. "_id": "AtOJ4UjUBAAx3XR5kcCM",
  506. "_source": {
  507. "@timestamp": "2099-12-06T11:04:07.000Z",
  508. "event": {
  509. "category": "file",
  510. "id": "dGCHwoeS",
  511. "sequence": 2
  512. },
  513. "file": {
  514. "accessed": "2099-12-07T11:07:08.000Z",
  515. "name": "cmd.exe",
  516. "path": "C:\\Windows\\System32\\cmd.exe",
  517. "type": "file",
  518. "size": 16384
  519. },
  520. "process": {
  521. "pid": 2012,
  522. "name": "cmd.exe",
  523. "executable": "C:\\Windows\\System32\\cmd.exe"
  524. }
  525. }
  526. },
  527. {
  528. "_index": "my-index-000001",
  529. "_id": "OQmfCaduce8zoHT93o4H",
  530. "_source": {
  531. "@timestamp": "2099-12-07T11:07:09.000Z",
  532. "event": {
  533. "category": "process",
  534. "id": "aR3NWVOs",
  535. "sequence": 4
  536. },
  537. "process": {
  538. "pid": 2012,
  539. "name": "regsvr32.exe",
  540. "command_line": "regsvr32.exe /s /u /i:https://...RegSvr32.sct scrobj.dll",
  541. "executable": "C:\\Windows\\System32\\regsvr32.exe"
  542. }
  543. }
  544. }
  545. ]
  546. }
  547. ]
  548. }
  549. }
  550. ----
  551. // TESTRESPONSE[s/"took": 6/"took": $body.took/]
  552. // TESTRESPONSE[s/"_id": "AtOJ4UjUBAAx3XR5kcCM"/"_id": $body.hits.sequences.0.events.0._id/]
  553. // TESTRESPONSE[s/"_id": "OQmfCaduce8zoHT93o4H"/"_id": $body.hits.sequences.0.events.1._id/]