eql-search-api.asciidoc 13 KB

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