ml-configuring-aggregations.asciidoc 15 KB

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