eql-search-api.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. [[eql-search-api-request-body]]
  65. ==== {api-request-body-title}
  66. `case_sensitive`::
  67. (Optional, boolean)
  68. If `true`, matching for the <<eql-search-api-request-query-param,EQL query>> is
  69. case sensitive. Defaults to `false`.
  70. `event_category_field`::
  71. (Required*, string)
  72. Field containing the event classification, such as `process`, `file`, or
  73. `network`.
  74. +
  75. Defaults to `event.category`, as defined in the {ecs-ref}/ecs-event.html[Elastic
  76. Common Schema (ECS)]. If an index does not contain the `event.category` field,
  77. this value is required.
  78. `filter`::
  79. (Optional, <<query-dsl,query DSL object>>)
  80. Query, written in query DSL, used to filter the events on which the EQL query
  81. runs.
  82. `implicit_join_key_field`::
  83. (Optional, string)
  84. Reserved for future use.
  85. [[eql-search-api-request-query-param]]
  86. `query`::
  87. (Required, string)
  88. <<eql-syntax,EQL>> query you wish to run.
  89. +
  90. IMPORTANT: This parameter supports a subset of EQL syntax. See
  91. <<eql-unsupported-syntax>>.
  92. `search_after`::
  93. (Optional, string)
  94. Reserved for future use.
  95. `size`::
  96. (Optional, integer or float)
  97. For <<eql-basic-syntax,basic queries>>, the maximum number of matching events to
  98. return.
  99. +
  100. For <<eql-sequences,sequence queries>>, the maximum number of matching sequences
  101. to return.
  102. +
  103. Defaults to `50`. Values must be greater than `0`.
  104. [[eql-search-api-timestamp-field]]
  105. `timestamp_field`::
  106. +
  107. --
  108. (Required*, string)
  109. Field containing event timestamp.
  110. Defaults to `@timestamp`, as defined in the
  111. {ecs-ref}/ecs-event.html[Elastic Common Schema (ECS)]. If an index does not
  112. contain the `@timestamp` field, this value is required.
  113. Events in the API response are sorted by this field's value, converted to
  114. milliseconds since the https://en.wikipedia.org/wiki/Unix_time[Unix epoch], in
  115. ascending order.
  116. --
  117. [role="child_attributes"]
  118. [[eql-search-api-response-body]]
  119. ==== {api-response-body-title}
  120. `took`::
  121. +
  122. --
  123. (integer)
  124. Milliseconds it took {es} to execute the request.
  125. This value is calculated by measuring the time elapsed
  126. between receipt of a request on the coordinating node
  127. and the time at which the coordinating node is ready to send the response.
  128. Took time includes:
  129. * Communication time between the coordinating node and data nodes
  130. * Time the request spends in the `search` <<modules-threadpool,thread pool>>,
  131. queued for execution
  132. * Actual execution time
  133. Took time does *not* include:
  134. * Time needed to send the request to {es}
  135. * Time needed to serialize the JSON response
  136. * Time needed to send the response to a client
  137. --
  138. `timed_out`::
  139. (boolean)
  140. If `true`, the request timed out before completion.
  141. `hits`::
  142. (object)
  143. Contains matching events and sequences. Also contains related metadata.
  144. +
  145. .Properties of `hits`
  146. [%collapsible%open]
  147. ====
  148. `total`::
  149. (object)
  150. Metadata about the number of matching events or sequences.
  151. +
  152. .Properties of `total`
  153. [%collapsible%open]
  154. =====
  155. `value`::
  156. (integer)
  157. For <<eql-basic-syntax,basic queries>>, the total number of matching events.
  158. +
  159. For <<eql-sequences,sequence queries>>, the total number of matching sequences.
  160. `relation`::
  161. +
  162. --
  163. (string)
  164. Indicates whether the number of events or sequences returned is accurate or a
  165. lower bound.
  166. Returned values are:
  167. `eq`::: Accurate
  168. `gte`::: Lower bound, including returned events or sequences
  169. --
  170. =====
  171. `sequences`::
  172. (array of objects)
  173. Contains event sequences matching the query. Each object represents a
  174. matching sequence. This parameter is only returned for EQL queries containing
  175. a <<eql-sequences,sequence>>.
  176. +
  177. .Properties of `sequences` objects
  178. [%collapsible%open]
  179. =====
  180. `join_keys`::
  181. (array of strings)
  182. Shared field values used to constrain matches in the sequence. These are defined
  183. using the <<eql-sequences,`by` keyword>> in the EQL query syntax.
  184. `events`::
  185. (array of objects)
  186. Contains events matching the query. Each object represents a
  187. matching event.
  188. +
  189. .Properties of `events` objects
  190. [%collapsible%open]
  191. ======
  192. `_index`::
  193. (string)
  194. Name of the index containing the event.
  195. `_id`::
  196. (string)
  197. (string)
  198. Unique identifier for the event.
  199. This ID is only unique within the index.
  200. `_score`::
  201. (float)
  202. Positive 32-bit floating point number used to determine the relevance of the
  203. event. See <<relevance-scores>>.
  204. `_source`::
  205. (object)
  206. Original JSON body passed for the event at index time.
  207. `sort`::
  208. (array)
  209. Integer used as the sort value for the event.
  210. +
  211. By default, this is the event's <<eql-search-api-timestamp-field,timestamp
  212. value>>, converted to milliseconds since the
  213. https://en.wikipedia.org/wiki/Unix_time[Unix epoch].
  214. ======
  215. =====
  216. [[eql-search-api-response-events]]
  217. `events`::
  218. (array of objects)
  219. Contains events matching the query. Each object represents a
  220. matching event.
  221. +
  222. .Properties of `events` objects
  223. [%collapsible%open]
  224. =====
  225. `_index`::
  226. (string)
  227. Name of the index containing the event.
  228. `_id`::
  229. (string)
  230. (string)
  231. Unique identifier for the event.
  232. This ID is only unique within the index.
  233. `_score`::
  234. (float)
  235. Positive 32-bit floating point number used to determine the relevance of the
  236. event. See <<relevance-scores>>.
  237. `_source`::
  238. (object)
  239. Original JSON body passed for the event at index time.
  240. `sort`::
  241. (array)
  242. Integer used as the sort value for the event.
  243. +
  244. By default, this is the event's <<eql-search-api-timestamp-field,timestamp
  245. value>>, converted to milliseconds since the
  246. https://en.wikipedia.org/wiki/Unix_time[Unix epoch].
  247. =====
  248. ====
  249. [[eql-search-api-example]]
  250. ==== {api-examples-title}
  251. [[eql-search-api-basic-query-ex]]
  252. ===== Basic query example
  253. The following EQL search request searches for events with an `event.category` of
  254. `file` that meet the following conditions:
  255. * A `file.name` of `cmd.exe`
  256. * An `agent.id` other than `my_user`
  257. [source,console]
  258. ----
  259. GET /my_index/_eql/search
  260. {
  261. "query": """
  262. file where (file.name == "cmd.exe" and agent.id != "my_user")
  263. """
  264. }
  265. ----
  266. The API returns the following response. Matching events in the `hits.events`
  267. property are sorted by <<eql-search-api-timestamp-field,timestamp>>, converted
  268. to milliseconds since the https://en.wikipedia.org/wiki/Unix_time[Unix epoch],
  269. in ascending order.
  270. [source,console-result]
  271. ----
  272. {
  273. "took": 6,
  274. "timed_out": false,
  275. "hits": {
  276. "total": {
  277. "value": 2,
  278. "relation": "eq"
  279. },
  280. "events": [
  281. {
  282. "_index": "my_index",
  283. "_id": "2",
  284. "_score": null,
  285. "_source": {
  286. "@timestamp": "2020-12-06T11:04:07.000Z",
  287. "agent": {
  288. "id": "8a4f500d"
  289. },
  290. "event": {
  291. "category": "file"
  292. },
  293. "file": {
  294. "accessed": "2020-12-07T11:07:08.000Z",
  295. "name": "cmd.exe",
  296. "path": "C:\\Windows\\System32\\cmd.exe",
  297. "type": "file",
  298. "size": 16384
  299. },
  300. "process": {
  301. "name": "cmd.exe",
  302. "path": "C:\\Windows\\System32\\cmd.exe"
  303. }
  304. },
  305. "sort": [
  306. 1607252647000
  307. ]
  308. },
  309. {
  310. "_index": "my_index",
  311. "_id": "4",
  312. "_score": null,
  313. "_source": {
  314. "@timestamp": "2020-12-07T11:07:08.000Z",
  315. "agent": {
  316. "id": "8a4f500d"
  317. },
  318. "event": {
  319. "category": "file"
  320. },
  321. "file": {
  322. "accessed": "2020-12-07T11:07:08.000Z",
  323. "name": "cmd.exe",
  324. "path": "C:\\Windows\\System32\\cmd.exe",
  325. "type": "file",
  326. "size": 16384
  327. },
  328. "process": {
  329. "name": "cmd.exe",
  330. "path": "C:\\Windows\\System32\\cmd.exe"
  331. }
  332. },
  333. "sort": [
  334. 1607339228000
  335. ]
  336. }
  337. ]
  338. }
  339. }
  340. ----
  341. // TESTRESPONSE[s/"took": 6/"took": $body.took/]
  342. [[eql-search-api-sequence-ex]]
  343. ===== Sequence query example
  344. The following EQL search request matches a <<eql-sequences,sequence>> of events
  345. that:
  346. . Start with an event with:
  347. +
  348. --
  349. * An `event.category` of `file`
  350. * A `file.name` of `cmd.exe`
  351. * An `agent.id` other than `my_user`
  352. --
  353. . Followed by an event with:
  354. +
  355. --
  356. * An `event.category` of `process`
  357. * A `process.path` that contains the substring `regsvr32`
  358. --
  359. These events must also share the same `agent.id` value.
  360. [source,console]
  361. ----
  362. GET /my_index/_eql/search
  363. {
  364. "query": """
  365. sequence by agent.id
  366. [ file where file.name == "cmd.exe" and agent.id != "my_user" ]
  367. [ process where stringContains(process.path, "regsvr32") ]
  368. """
  369. }
  370. ----
  371. The API returns the following response. The `hits.sequences.join_keys` property
  372. contains the shared `agent.id` value for each matching event. Matching events in
  373. the `hits.sequences.events` property are sorted by
  374. <<eql-search-api-timestamp-field,timestamp>>, converted to milliseconds since
  375. the https://en.wikipedia.org/wiki/Unix_time[Unix epoch], in ascending order.
  376. [source,console-result]
  377. ----
  378. {
  379. "took": 6,
  380. "timed_out": false,
  381. "hits": {
  382. "total": {
  383. "value": 1,
  384. "relation": "eq"
  385. },
  386. "sequences": [
  387. {
  388. "join_keys": [
  389. "8a4f500d"
  390. ],
  391. "events": [
  392. {
  393. "_index": "my_index",
  394. "_id": "4",
  395. "_score": null,
  396. "_source": {
  397. "@timestamp": "2020-12-07T11:07:08.000Z",
  398. "agent": {
  399. "id": "8a4f500d"
  400. },
  401. "event": {
  402. "category": "file"
  403. },
  404. "file": {
  405. "accessed": "2020-12-07T11:07:08.000Z",
  406. "name": "cmd.exe",
  407. "path": "C:\\Windows\\System32\\cmd.exe",
  408. "type": "file",
  409. "size": 16384
  410. },
  411. "process": {
  412. "name": "cmd.exe",
  413. "path": "C:\\Windows\\System32\\cmd.exe"
  414. }
  415. },
  416. "fields": {
  417. "@timestamp": [
  418. "1607339228000"
  419. ]
  420. },
  421. "sort": [
  422. 1607339228000
  423. ]
  424. },
  425. {
  426. "_index": "my_index",
  427. "_id": "5",
  428. "_score": null,
  429. "_source": {
  430. "@timestamp": "2020-12-07T11:07:09.000Z",
  431. "agent": {
  432. "id": "8a4f500d"
  433. },
  434. "event": {
  435. "category": "process"
  436. },
  437. "process": {
  438. "name": "regsvr32.exe",
  439. "path": "C:\\Windows\\System32\\regsvr32.exe"
  440. }
  441. },
  442. "fields": {
  443. "@timestamp": [
  444. "1607339229000"
  445. ]
  446. },
  447. "sort": [
  448. 1607339229000
  449. ]
  450. }
  451. ]
  452. }
  453. ]
  454. }
  455. }
  456. ----
  457. // TESTRESPONSE[s/"took": 6/"took": $body.took/]