eql-search-api.asciidoc 17 KB

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