ml-configuring-aggregations.asciidoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. [role="xpack"]
  2. [[ml-configuring-aggregation]]
  3. = Aggregating data for faster performance
  4. By default, {dfeeds} fetch data from {es} using search and scroll requests.
  5. It can be significantly more efficient, however, to aggregate data in {es}
  6. and to configure your {anomaly-jobs} to analyze aggregated data.
  7. One of the benefits of aggregating data this way is that {es} automatically
  8. distributes these calculations across your cluster. You can then feed this
  9. aggregated data into the {ml-features} instead of raw results, which
  10. reduces the volume of data that must be considered while detecting anomalies.
  11. TIP: If you use a terms aggregation and the cardinality of a term is high but
  12. still significantly less than your total number of documents, use
  13. {ref}/search-aggregations-bucket-composite-aggregation.html[composite aggregations]
  14. experimental:[Support for composite aggregations inside datafeeds is currently experimental].
  15. [discrete]
  16. [[aggs-limits-dfeeds]]
  17. == Requirements and limitations
  18. There are some limitations to using aggregations in {dfeeds}.
  19. Your aggregation must include a `date_histogram` aggregation or a top level `composite` aggregation,
  20. which in turn must contain a `max` aggregation on the time field.
  21. This requirement ensures that the aggregated data is a time series and the timestamp
  22. of each bucket is the time of the last record in the bucket.
  23. IMPORTANT: The name of the aggregation and the name of the field that it
  24. operates on need to match, otherwise the aggregation doesn't work. For example,
  25. if you use a `max` aggregation on a time field called `responsetime`, the name
  26. of the aggregation must be also `responsetime`.
  27. You must consider the interval of the `date_histogram` or `composite`
  28. aggregation carefully. The bucket span of your {anomaly-job} must be divisible
  29. by the value of the `calendar_interval` or `fixed_interval` in your aggregation
  30. (with no remainder). If you specify a `frequency` for your {dfeed},
  31. it must also be divisible by this interval. {anomaly-jobs-cap} cannot use
  32. `date_histogram` or `composite` aggregations with an interval measured in months
  33. because the length of the month is not fixed; they can use weeks or smaller units.
  34. TIP: As a rule of thumb, if your detectors use <<ml-metric-functions,metric>> or
  35. <<ml-sum-functions,sum>> analytical functions, set the `date_histogram` or `composite`
  36. aggregation interval to a tenth of the bucket span. This suggestion creates
  37. finer, more granular time buckets, which are ideal for this type of analysis. If
  38. your detectors use <<ml-count-functions,count>> or <<ml-rare-functions,rare>>
  39. functions, set the interval to the same value as the bucket span.
  40. If your <<aggs-dfeeds,{dfeed} uses aggregations with nested `terms` aggs>> and
  41. model plot is not enabled for the {anomaly-job}, neither the **Single Metric
  42. Viewer** nor the **Anomaly Explorer** can plot and display an anomaly
  43. chart for the job. In these cases, the charts are not visible and an explanatory
  44. message is shown.
  45. Your {dfeed} can contain multiple aggregations, but only the ones with names
  46. that match values in the job configuration are fed to the job.
  47. [discrete]
  48. [[aggs-using-date-histogram]]
  49. === Including aggregations in {anomaly-jobs}
  50. When you create or update an {anomaly-job}, you can include the names of
  51. aggregations, for example:
  52. [source,console]
  53. ----------------------------------
  54. PUT _ml/anomaly_detectors/farequote
  55. {
  56. "analysis_config": {
  57. "bucket_span": "60m",
  58. "detectors": [{
  59. "function": "mean",
  60. "field_name": "responsetime", <1>
  61. "by_field_name": "airline" <1>
  62. }],
  63. "summary_count_field_name": "doc_count"
  64. },
  65. "data_description": {
  66. "time_field":"time" <1>
  67. },
  68. "datafeed_config":{
  69. "indices": ["farequote"],
  70. "aggregations": {
  71. "buckets": {
  72. "date_histogram": {
  73. "field": "time",
  74. "fixed_interval": "360s",
  75. "time_zone": "UTC"
  76. },
  77. "aggregations": {
  78. "time": { <2>
  79. "max": {"field": "time"}
  80. },
  81. "airline": { <3>
  82. "terms": {
  83. "field": "airline",
  84. "size": 100
  85. },
  86. "aggregations": {
  87. "responsetime": { <4>
  88. "avg": {
  89. "field": "responsetime"
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. ----------------------------------
  100. // TEST[skip:setup:farequote_data]
  101. <1> The `airline`, `responsetime`, and `time` fields are aggregations. Only the
  102. aggregated fields defined in the `analysis_config` object are analyzed by the
  103. {anomaly-job}.
  104. <2> The aggregations have names that match the fields that they operate on. The
  105. `max` aggregation is named `time` and its field also needs to be `time`.
  106. <3> The `term` aggregation is named `airline` and its field is also named
  107. `airline`.
  108. <4> The `avg` aggregation is named `responsetime` and its field is also named
  109. `responsetime`.
  110. When the `summary_count_field_name` property is set to a non-null value, the job
  111. expects to receive aggregated input. The property must be set to the name of the
  112. field that contains the count of raw data points that have been aggregated. It
  113. applies to all detectors in the job.
  114. TIP: If you are using a `term` aggregation to gather influencer or partition
  115. field information, consider using a `composite` aggregation. It performs
  116. better than a `date_histogram` with a nested `term` aggregation and also
  117. includes all the values of the field instead of the top values per bucket.
  118. [discrete]
  119. [[aggs-using-composite]]
  120. === Using composite aggregations in {anomaly-jobs}
  121. experimental::[]
  122. For `composite` aggregation support, there must be exactly one `date_histogram` value
  123. source. That value source must not be sorted in descending order. Additional
  124. `composite` aggregation value sources are allowed, such as `terms`.
  125. NOTE: A {dfeed} that uses composite aggregations may not be as performant as
  126. {dfeeds} that use scrolling or date histogram aggregations. Composite
  127. aggregations are optimized for queries that are either `match_all` or `range`
  128. filters. Other types of
  129. queries may cause the `composite` aggregation to be ineffecient.
  130. Here is an example that uses a `composite` aggregation instead of a
  131. `date_histogram`.
  132. This is an example of a job with a {dfeed} that uses a `composite` aggregation
  133. to bucket the metrics based on time and terms:
  134. [source,console]
  135. ----------------------------------
  136. PUT _ml/anomaly_detectors/farequote-composite
  137. {
  138. "analysis_config": {
  139. "bucket_span": "60m",
  140. "detectors": [{
  141. "function": "mean",
  142. "field_name": "responsetime",
  143. "by_field_name": "airline"
  144. }],
  145. "summary_count_field_name": "doc_count"
  146. },
  147. "data_description": {
  148. "time_field":"time"
  149. },
  150. "datafeed_config":{
  151. "indices": ["farequote"],
  152. "aggregations": {
  153. "buckets": {
  154. "composite": {
  155. "size": 1000, <1>
  156. "sources": [
  157. {
  158. "time_bucket": { <2>
  159. "date_histogram": {
  160. "field": "time",
  161. "fixed_interval": "360s",
  162. "time_zone": "UTC"
  163. }
  164. }
  165. },
  166. {
  167. "airline": { <3>
  168. "terms": {
  169. "field": "airline"
  170. }
  171. }
  172. }
  173. ]
  174. },
  175. "aggregations": {
  176. "time": { <4>
  177. "max": {
  178. "field": "time"
  179. }
  180. },
  181. "responsetime": { <5>
  182. "avg": {
  183. "field": "responsetime"
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }
  191. ----------------------------------
  192. <1> Provide the `size` to the composite agg to control how many resources
  193. are used when aggregating the data. A larger `size` means a faster {dfeed} but
  194. more cluster resources are used when searching.
  195. <2> The required `date_histogram` composite aggregation source. Make sure it
  196. is named differently than your desired time field.
  197. <3> Instead of using a regular `term` aggregation, adding a composite
  198. aggregation `term` source with the name `airline` works. Note its name
  199. is the same as the field.
  200. <4> The required `max` aggregation whose name is the time field in the
  201. job analysis config.
  202. <5> The `avg` aggregation is named `responsetime` and its field is also named
  203. `responsetime`.
  204. [discrete]
  205. [[aggs-dfeeds]]
  206. == Nested aggregations in {dfeeds}
  207. {dfeeds-cap} support complex nested aggregations. This example uses the
  208. `derivative` pipeline aggregation to find the first order derivative of the
  209. counter `system.network.out.bytes` for each value of the field `beat.name`.
  210. NOTE: `derivative` or other pipeline aggregations may not work within `composite`
  211. aggregations. See
  212. {ref}/search-aggregations-bucket-composite-aggregation.html#search-aggregations-bucket-composite-aggregation-pipeline-aggregations[composite aggregations and pipeline aggregations].
  213. [source,js]
  214. ----------------------------------
  215. "aggregations": {
  216. "beat.name": {
  217. "terms": {
  218. "field": "beat.name"
  219. },
  220. "aggregations": {
  221. "buckets": {
  222. "date_histogram": {
  223. "field": "@timestamp",
  224. "fixed_interval": "5m"
  225. },
  226. "aggregations": {
  227. "@timestamp": {
  228. "max": {
  229. "field": "@timestamp"
  230. }
  231. },
  232. "bytes_out_average": {
  233. "avg": {
  234. "field": "system.network.out.bytes"
  235. }
  236. },
  237. "bytes_out_derivative": {
  238. "derivative": {
  239. "buckets_path": "bytes_out_average"
  240. }
  241. }
  242. }
  243. }
  244. }
  245. }
  246. }
  247. ----------------------------------
  248. // NOTCONSOLE
  249. [discrete]
  250. [[aggs-single-dfeeds]]
  251. == Single bucket aggregations in {dfeeds}
  252. {dfeeds-cap} not only supports multi-bucket aggregations, but also single bucket
  253. aggregations. The following shows two `filter` aggregations, each gathering the
  254. number of unique entries for the `error` field.
  255. [source,js]
  256. ----------------------------------
  257. {
  258. "job_id":"servers-unique-errors",
  259. "indices": ["logs-*"],
  260. "aggregations": {
  261. "buckets": {
  262. "date_histogram": {
  263. "field": "time",
  264. "interval": "360s",
  265. "time_zone": "UTC"
  266. },
  267. "aggregations": {
  268. "time": {
  269. "max": {"field": "time"}
  270. }
  271. "server1": {
  272. "filter": {"term": {"source": "server-name-1"}},
  273. "aggregations": {
  274. "server1_error_count": {
  275. "value_count": {
  276. "field": "error"
  277. }
  278. }
  279. }
  280. },
  281. "server2": {
  282. "filter": {"term": {"source": "server-name-2"}},
  283. "aggregations": {
  284. "server2_error_count": {
  285. "value_count": {
  286. "field": "error"
  287. }
  288. }
  289. }
  290. }
  291. }
  292. }
  293. }
  294. }
  295. ----------------------------------
  296. // NOTCONSOLE
  297. [discrete]
  298. [[aggs-define-dfeeds]]
  299. == Defining aggregations in {dfeeds}
  300. When you define an aggregation in a {dfeed}, it must have one of the following forms:
  301. When using a `date_histogram` aggregation to bucket by time:
  302. [source,js]
  303. ----------------------------------
  304. "aggregations": {
  305. ["bucketing_aggregation": {
  306. "bucket_agg": {
  307. ...
  308. },
  309. "aggregations": {
  310. "data_histogram_aggregation": {
  311. "date_histogram": {
  312. "field": "time",
  313. },
  314. "aggregations": {
  315. "timestamp": {
  316. "max": {
  317. "field": "time"
  318. }
  319. },
  320. [,"<first_term>": {
  321. "terms":{...
  322. }
  323. [,"aggregations" : {
  324. [<sub_aggregation>]+
  325. } ]
  326. }]
  327. }
  328. }
  329. }
  330. }
  331. }
  332. ----------------------------------
  333. // NOTCONSOLE
  334. When using a `composite` aggregation:
  335. [source,js]
  336. ----------------------------------
  337. "aggregations": {
  338. "composite_agg": {
  339. "sources": [
  340. {
  341. "date_histogram_agg": {
  342. "field": "time",
  343. ...settings...
  344. }
  345. },
  346. ...other valid sources...
  347. ],
  348. ...composite agg settings...,
  349. "aggregations": {
  350. "timestamp": {
  351. "max": {
  352. "field": "time"
  353. }
  354. },
  355. ...other aggregations...
  356. [
  357. [,"aggregations" : {
  358. [<sub_aggregation>]+
  359. } ]
  360. }]
  361. }
  362. }
  363. }
  364. ----------------------------------
  365. // NOTCONSOLE
  366. The top level aggregation must be exclusively one of the following:
  367. * A {ref}/search-aggregations-bucket.html[bucket aggregation] containing a single
  368. sub-aggregation that is a `date_histogram`
  369. * A top level aggregation that is a `date_histogram`
  370. * A top level aggregation is a `composite` aggregation
  371. There must be exactly one `date_histogram`, `composite` aggregation. For more information, see
  372. {ref}/search-aggregations-bucket-datehistogram-aggregation.html[Date histogram aggregation] and
  373. {ref}/search-aggregations-bucket-composite-aggregation.html[Composite aggregation].
  374. NOTE: The `time_zone` parameter in the date histogram aggregation must be set to
  375. `UTC`, which is the default value.
  376. Each histogram or composite bucket has a key, which is the bucket start time.
  377. This key cannot be used for aggregations in {dfeeds}, however, because
  378. they need to know the time of the latest record within a bucket.
  379. Otherwise, when you restart a {dfeed}, it continues from the start time of the
  380. histogram or composite bucket and possibly fetches the same data twice.
  381. The max aggregation for the time field is therefore necessary to provide
  382. the time of the latest record within a bucket.
  383. You can optionally specify a terms aggregation, which creates buckets for
  384. different values of a field.
  385. IMPORTANT: If you use a terms aggregation, by default it returns buckets for
  386. the top ten terms. Thus if the cardinality of the term is greater than 10, not
  387. all terms are analyzed. In this case, consider using `composite` aggregations
  388. experimental:[Support for composite aggregations inside datafeeds is currently experimental].
  389. You can change this behavior by setting the `size` parameter. To
  390. determine the cardinality of your data, you can run searches such as:
  391. [source,js]
  392. --------------------------------------------------
  393. GET .../_search
  394. {
  395. "aggs": {
  396. "service_cardinality": {
  397. "cardinality": {
  398. "field": "service"
  399. }
  400. }
  401. }
  402. }
  403. --------------------------------------------------
  404. // NOTCONSOLE
  405. By default, {es} limits the maximum number of terms returned to 10000. For high
  406. cardinality fields, the query might not run. It might return errors related to
  407. circuit breaking exceptions that indicate that the data is too large. In such
  408. cases, use `composite` aggregations in your {dfeed}. For more information, see
  409. {ref}/search-aggregations-bucket-terms-aggregation.html[Terms aggregation].
  410. You can also optionally specify multiple sub-aggregations. The sub-aggregations
  411. are aggregated for the buckets that were created by their parent aggregation.
  412. For more information, see {ref}/search-aggregations.html[Aggregations].