eql-search-api.asciidoc 18 KB

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