ml-configuring-aggregations.asciidoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. [role="xpack"]
  2. [[ml-configuring-aggregation]]
  3. = Aggregating data for faster performance
  4. When you aggregate data, {es} automatically distributes the calculations across
  5. your cluster. Then you can feed this aggregated data into the {ml-features}
  6. instead of raw results. It reduces the volume of data that must be analyzed.
  7. [discrete]
  8. [[aggs-requs-dfeeds]]
  9. == Requirements
  10. There are a number of requirements for using aggregations in {dfeeds}.
  11. [discrete]
  12. [[aggs-aggs]]
  13. === Aggregations
  14. * Your aggregation must include a `date_histogram` aggregation or a top level
  15. `composite` aggregation, which in turn must contain a `max` aggregation on the
  16. time field. It ensures that the aggregated data is a time series and the
  17. timestamp of each bucket is the time of the last record in the bucket.
  18. * The `time_zone` parameter in the date histogram aggregation must be set to
  19. `UTC`, which is the default value.
  20. * The name of the aggregation and the name of the field that it operates on need
  21. to match. For example, if you use a `max` aggregation on a time field called
  22. `responsetime`, the name of the aggregation must also be `responsetime`.
  23. * For `composite` aggregation support, there must be exactly one
  24. `date_histogram` value source. That value source must not be sorted in
  25. descending order. Additional `composite` aggregation value sources are allowed,
  26. such as `terms`.
  27. * If you set the `summary_count_field_name` property to a non-null value, the
  28. {anomaly-job} expects to receive aggregated input. The property must be set to
  29. the name of the field that contains the count of raw data points that have been
  30. aggregated. It applies to all detectors in the job.
  31. * The influencers or the partition fields must be included in the aggregation of
  32. your {dfeed}, otherwise they are not included in the job analysis. For more
  33. information on influencers, refer to <<ml-ad-influencers>>.
  34. [discrete]
  35. [[aggs-interval]]
  36. === Intervals
  37. * The bucket span of your {anomaly-job} must be divisible by the value of the
  38. `calendar_interval` or `fixed_interval` in your aggregation (with no remainder).
  39. * If you specify a `frequency` for your {dfeed}, it must be divisible by the
  40. `calendar_interval` or the `fixed_interval`.
  41. * {anomaly-jobs-cap} cannot use `date_histogram` or `composite` aggregations
  42. with an interval measured in months because the length of the month is not
  43. fixed; they can use weeks or smaller units.
  44. [discrete]
  45. [[aggs-limits-dfeeds]]
  46. == Limitations
  47. * If your <<aggs-dfeeds,{dfeed} uses aggregations with nested `terms` aggs>> and
  48. model plot is not enabled for the {anomaly-job}, neither the
  49. **Single Metric Viewer** nor the **Anomaly Explorer** can plot and display an
  50. anomaly chart. In these cases, an explanatory message is shown instead of the
  51. chart.
  52. * Your {dfeed} can contain multiple aggregations, but only the ones with names
  53. that match values in the job configuration are fed to the job.
  54. [discrete]
  55. [[aggs-recommendations-dfeeds]]
  56. == Recommendations
  57. * When your detectors use <<ml-metric-functions,metric>> or
  58. <<ml-sum-functions,sum>> analytical functions, it's recommended to set the
  59. `date_histogram` or `composite` aggregation interval to a tenth of the bucket
  60. span. This creates finer, more granular time buckets, which are ideal for this
  61. type of analysis.
  62. * When your detectors use <<ml-count-functions,count>> or
  63. <<ml-rare-functions,rare>> functions, set the interval to the same value as the
  64. bucket span.
  65. * If you have multiple influencers or partition fields or if your field
  66. cardinality is more than 1000, use
  67. {ref}/search-aggregations-bucket-composite-aggregation.html[composite aggregations].
  68. +
  69. --
  70. To determine the cardinality of your data, you can run searches such as:
  71. [source,js]
  72. --------------------------------------------------
  73. GET .../_search
  74. {
  75. "aggs": {
  76. "service_cardinality": {
  77. "cardinality": {
  78. "field": "service"
  79. }
  80. }
  81. }
  82. }
  83. --------------------------------------------------
  84. // NOTCONSOLE
  85. --
  86. [discrete]
  87. [[aggs-using-date-histogram]]
  88. == Including aggregations in {anomaly-jobs}
  89. When you create or update an {anomaly-job}, you can include aggregated fields in
  90. the analysis configuration. In the {dfeed} configuration object, you can define
  91. the aggregations.
  92. [source,console]
  93. ----------------------------------
  94. PUT _ml/anomaly_detectors/kibana-sample-data-flights
  95. {
  96. "analysis_config": {
  97. "bucket_span": "60m",
  98. "detectors": [{
  99. "function": "mean",
  100. "field_name": "responsetime", <1>
  101. "by_field_name": "airline" <1>
  102. }],
  103. "summary_count_field_name": "doc_count" <2>
  104. },
  105. "data_description": {
  106. "time_field":"time" <1>
  107. },
  108. "datafeed_config":{
  109. "indices": ["kibana-sample-data-flights"],
  110. "aggregations": {
  111. "buckets": {
  112. "date_histogram": {
  113. "field": "time",
  114. "fixed_interval": "360s",
  115. "time_zone": "UTC"
  116. },
  117. "aggregations": {
  118. "time": { <3>
  119. "max": {"field": "time"}
  120. },
  121. "airline": { <4>
  122. "terms": {
  123. "field": "airline",
  124. "size": 100
  125. },
  126. "aggregations": {
  127. "responsetime": { <5>
  128. "avg": {
  129. "field": "responsetime"
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. ----------------------------------
  140. // TEST[skip:setup:farequote_data]
  141. <1> The `airline`, `responsetime`, and `time` fields are aggregations. Only the
  142. aggregated fields defined in the `analysis_config` object are analyzed by the
  143. {anomaly-job}.
  144. <2> The `summary_count_field_name` property is set to the `doc_count` field that
  145. is an aggregated field and contains the count of the aggregated data points.
  146. <3> The aggregations have names that match the fields that they operate on. The
  147. `max` aggregation is named `time` and its field also needs to be `time`.
  148. <4> The `term` aggregation is named `airline` and its field is also named
  149. `airline`.
  150. <5> The `avg` aggregation is named `responsetime` and its field is also named
  151. `responsetime`.
  152. Use the following format to define a `date_histogram` aggregation to bucket by
  153. time in your {dfeed}:
  154. [source,js]
  155. ----------------------------------
  156. "aggregations": {
  157. ["bucketing_aggregation": {
  158. "bucket_agg": {
  159. ...
  160. },
  161. "aggregations": {
  162. "data_histogram_aggregation": {
  163. "date_histogram": {
  164. "field": "time",
  165. },
  166. "aggregations": {
  167. "timestamp": {
  168. "max": {
  169. "field": "time"
  170. }
  171. },
  172. [,"<first_term>": {
  173. "terms":{...
  174. }
  175. [,"aggregations" : {
  176. [<sub_aggregation>]+
  177. } ]
  178. }]
  179. }
  180. }
  181. }
  182. }
  183. }
  184. ----------------------------------
  185. // NOTCONSOLE
  186. [discrete]
  187. [[aggs-using-composite]]
  188. == Composite aggregations
  189. Composite aggregations are optimized for queries that are either `match_all` or
  190. `range` filters. Use composite aggregations in your {dfeeds} for these cases.
  191. Other types of queries may cause the `composite` aggregation to be inefficient.
  192. The following is an example of a job with a {dfeed} that uses a `composite`
  193. aggregation to bucket the metrics based on time and terms:
  194. [source,console]
  195. ----------------------------------
  196. PUT _ml/anomaly_detectors/kibana-sample-data-flights-composite
  197. {
  198. "analysis_config": {
  199. "bucket_span": "60m",
  200. "detectors": [{
  201. "function": "mean",
  202. "field_name": "responsetime",
  203. "by_field_name": "airline"
  204. }],
  205. "summary_count_field_name": "doc_count"
  206. },
  207. "data_description": {
  208. "time_field":"time"
  209. },
  210. "datafeed_config":{
  211. "indices": ["kibana-sample-data-flights"],
  212. "aggregations": {
  213. "buckets": {
  214. "composite": {
  215. "size": 1000, <1>
  216. "sources": [
  217. {
  218. "time_bucket": { <2>
  219. "date_histogram": {
  220. "field": "time",
  221. "fixed_interval": "360s",
  222. "time_zone": "UTC"
  223. }
  224. }
  225. },
  226. {
  227. "airline": { <3>
  228. "terms": {
  229. "field": "airline"
  230. }
  231. }
  232. }
  233. ]
  234. },
  235. "aggregations": {
  236. "time": { <4>
  237. "max": {
  238. "field": "time"
  239. }
  240. },
  241. "responsetime": { <5>
  242. "avg": {
  243. "field": "responsetime"
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. ----------------------------------
  252. <1> The number of resources to use when aggregating the data. A larger `size`
  253. means a faster {dfeed} but more cluster resources are used when searching.
  254. <2> The required `date_histogram` composite aggregation source. Make sure it
  255. is named differently than your desired time field.
  256. <3> Instead of using a regular `term` aggregation, adding a composite
  257. aggregation `term` source with the name `airline` works. Note its name
  258. is the same as the field.
  259. <4> The required `max` aggregation whose name is the time field in the
  260. job analysis config.
  261. <5> The `avg` aggregation is named `responsetime` and its field is also named
  262. `responsetime`.
  263. Use the following format to define a composite aggregation in your {dfeed}:
  264. [source,js]
  265. ----------------------------------
  266. "aggregations": {
  267. "composite_agg": {
  268. "sources": [
  269. {
  270. "date_histogram_agg": {
  271. "field": "time",
  272. ...settings...
  273. }
  274. },
  275. ...other valid sources...
  276. ],
  277. ...composite agg settings...,
  278. "aggregations": {
  279. "timestamp": {
  280. "max": {
  281. "field": "time"
  282. }
  283. },
  284. ...other aggregations...
  285. [
  286. [,"aggregations" : {
  287. [<sub_aggregation>]+
  288. } ]
  289. }]
  290. }
  291. }
  292. }
  293. ----------------------------------
  294. // NOTCONSOLE
  295. [discrete]
  296. [[aggs-dfeeds]]
  297. == Nested aggregations
  298. You can also use complex nested aggregations in {dfeeds}.
  299. The next example uses the
  300. {ref}/search-aggregations-pipeline-derivative-aggregation.html[`derivative` pipeline aggregation]
  301. to find the first order derivative of the counter `system.network.out.bytes` for
  302. each value of the field `beat.name`.
  303. NOTE: `derivative` or other pipeline aggregations may not work within
  304. `composite` aggregations. See
  305. {ref}/search-aggregations-bucket-composite-aggregation.html#search-aggregations-bucket-composite-aggregation-pipeline-aggregations[composite aggregations and pipeline aggregations].
  306. [source,js]
  307. ----------------------------------
  308. "aggregations": {
  309. "beat.name": {
  310. "terms": {
  311. "field": "beat.name"
  312. },
  313. "aggregations": {
  314. "buckets": {
  315. "date_histogram": {
  316. "field": "@timestamp",
  317. "fixed_interval": "5m"
  318. },
  319. "aggregations": {
  320. "@timestamp": {
  321. "max": {
  322. "field": "@timestamp"
  323. }
  324. },
  325. "bytes_out_average": {
  326. "avg": {
  327. "field": "system.network.out.bytes"
  328. }
  329. },
  330. "bytes_out_derivative": {
  331. "derivative": {
  332. "buckets_path": "bytes_out_average"
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }
  339. }
  340. ----------------------------------
  341. // NOTCONSOLE
  342. [discrete]
  343. [[aggs-single-dfeeds]]
  344. == Single bucket aggregations
  345. You can also use single bucket aggregations in {dfeeds}. The following example
  346. shows two `filter` aggregations, each gathering the number of unique entries for
  347. the `error` field.
  348. [source,js]
  349. ----------------------------------
  350. {
  351. "job_id":"servers-unique-errors",
  352. "indices": ["logs-*"],
  353. "aggregations": {
  354. "buckets": {
  355. "date_histogram": {
  356. "field": "time",
  357. "interval": "360s",
  358. "time_zone": "UTC"
  359. },
  360. "aggregations": {
  361. "time": {
  362. "max": {"field": "time"}
  363. }
  364. "server1": {
  365. "filter": {"term": {"source": "server-name-1"}},
  366. "aggregations": {
  367. "server1_error_count": {
  368. "value_count": {
  369. "field": "error"
  370. }
  371. }
  372. }
  373. },
  374. "server2": {
  375. "filter": {"term": {"source": "server-name-2"}},
  376. "aggregations": {
  377. "server2_error_count": {
  378. "value_count": {
  379. "field": "error"
  380. }
  381. }
  382. }
  383. }
  384. }
  385. }
  386. }
  387. }
  388. ----------------------------------
  389. // NOTCONSOLE
  390. [discrete]
  391. [[aggs-amd-dfeeds]]
  392. == Using `aggregate_metric_double` field type in {dfeeds}
  393. NOTE: It is not currently possible to use `aggregate_metric_double` type fields
  394. in {dfeeds} without aggregations.
  395. You can use fields with the
  396. {ref}/aggregate-metric-double.html[`aggregate_metric_double`] field type in a
  397. {dfeed} with aggregations. It is required to retrieve the `value_count` of the
  398. `aggregate_metric_double` filed in an aggregation and then use it as the
  399. `summary_count_field_name` to provide the correct count that represents the
  400. aggregation value.
  401. In the following example, `presum` is an `aggregate_metric_double` type field
  402. that has all the possible metrics: `[ min, max, sum, value_count ]`. To use an
  403. `avg` aggregation on this field, you need to perform a `value_count` aggregation
  404. on `presum` and then set the field that contains the aggregated values
  405. `my_count` as the `summary_count_field_name`:
  406. [source,js]
  407. ----------------------------------
  408. {
  409. "analysis_config": {
  410. "bucket_span": "1h",
  411. "detectors": [
  412. {
  413. "function": "avg",
  414. "field_name": "my_avg"
  415. }
  416. ],
  417. "summary_count_field_name": "my_count" <1>
  418. },
  419. "data_description": {
  420. "time_field": "timestamp"
  421. },
  422. "datafeed_config": {
  423. "indices": [
  424. "my_index"
  425. ],
  426. "datafeed_id": "datafeed-id",
  427. "aggregations": {
  428. "buckets": {
  429. "date_histogram": {
  430. "field": "time",
  431. "fixed_interval": "360s",
  432. "time_zone": "UTC"
  433. },
  434. "aggregations": {
  435. "timestamp": {
  436. "max": {"field": "timestamp"}
  437. },
  438. "my_avg": { <2>
  439. "avg": {
  440. "field": "presum"
  441. }
  442. },
  443. "my_count": { <3>
  444. "value_count": {
  445. "field": "presum"
  446. }
  447. }
  448. }
  449. }
  450. }
  451. }
  452. }
  453. ----------------------------------
  454. // NOTCONSOLE
  455. <1> The field `my_count` is set as the `summary_count_field_name`. This field
  456. contains aggregated values from the `presum` `aggregate_metric_double` type
  457. field (refer to footnote 3).
  458. <2> The `avg` aggregation to use on the `presum` `aggregate_metric_double` type
  459. field.
  460. <3> The `value_count` aggregation on the `presum` `aggregate_metric_double` type
  461. field. This aggregated field must be set as the `summary_count_field_name`
  462. (refer to footnote 1) to make it possible to use the `aggregate_metric_double`
  463. type field in another aggregation.