ml-configuring-aggregations.asciidoc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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, the
  12. aggregation might not be effective and you might want to just use the default
  13. search and scroll behavior.
  14. [discrete]
  15. [[aggs-limits-dfeeds]]
  16. == Requirements and limitations
  17. There are some limitations to using aggregations in {dfeeds}. Your aggregation
  18. must include a `date_histogram` aggregation, which in turn must contain a `max`
  19. aggregation on the time field. This requirement ensures that the aggregated data
  20. is a time series and the timestamp of each bucket is the time of the last record
  21. in the bucket.
  22. IMPORTANT: The name of the aggregation and the name of the field that the agg
  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 also consider the interval of the date histogram aggregation carefully.
  27. The bucket span of your {anomaly-job} must be divisible by the value of the
  28. `calendar_interval` or `fixed_interval` in your aggregation (with no remainder).
  29. If you specify a `frequency` for your {dfeed}, it must also be divisible by this
  30. interval. {anomaly-jobs-cap} cannot use date histograms with an interval
  31. measured in months because the length of the month is not fixed. {dfeeds-cap}
  32. tolerate 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
  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. [discrete]
  40. [[aggs-include-jobs]]
  41. == Including aggregations in {anomaly-jobs}
  42. When you create or update an {anomaly-job}, you can include the names of
  43. aggregations, for example:
  44. [source,console]
  45. ----------------------------------
  46. PUT _ml/anomaly_detectors/farequote
  47. {
  48. "analysis_config": {
  49. "bucket_span": "60m",
  50. "detectors": [{
  51. "function": "mean",
  52. "field_name": "responsetime", <1>
  53. "by_field_name": "airline" <1>
  54. }],
  55. "summary_count_field_name": "doc_count"
  56. },
  57. "data_description": {
  58. "time_field":"time" <1>
  59. }
  60. }
  61. ----------------------------------
  62. // TEST[skip:setup:farequote_data]
  63. <1> The `airline`, `responsetime`, and `time` fields are aggregations. Only the
  64. aggregated fields defined in the `analysis_config` object are analyzed by the
  65. {anomaly-job}.
  66. NOTE: When the `summary_count_field_name` property is set to a non-null value,
  67. the job expects to receive aggregated input. The property must be set to the
  68. name of the field that contains the count of raw data points that have been
  69. aggregated. It applies to all detectors in the job.
  70. The aggregations are defined in the {dfeed} as follows:
  71. [source,console]
  72. ----------------------------------
  73. PUT _ml/datafeeds/datafeed-farequote
  74. {
  75. "job_id":"farequote",
  76. "indices": ["farequote"],
  77. "aggregations": {
  78. "buckets": {
  79. "date_histogram": {
  80. "field": "time",
  81. "fixed_interval": "360s",
  82. "time_zone": "UTC"
  83. },
  84. "aggregations": {
  85. "time": { <1>
  86. "max": {"field": "time"}
  87. },
  88. "airline": { <2>
  89. "terms": {
  90. "field": "airline",
  91. "size": 100
  92. },
  93. "aggregations": {
  94. "responsetime": { <3>
  95. "avg": {
  96. "field": "responsetime"
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. ----------------------------------
  106. // TEST[skip:setup:farequote_job]
  107. <1> The aggregations have names that match the fields that they operate on. The
  108. `max` aggregation is named `time` and its field also needs to be `time`.
  109. <2> The `term` aggregation is named `airline` and its field is also named
  110. `airline`.
  111. <3> The `avg` aggregation is named `responsetime` and its field is also named
  112. `responsetime`.
  113. Your {dfeed} can contain multiple aggregations, but only the ones with names
  114. that match values in the job configuration are fed to the job.
  115. [discrete]
  116. [[aggs-dfeeds]]
  117. == Nested aggregations in {dfeeds}
  118. {dfeeds-cap} support complex nested aggregations. This example uses the
  119. `derivative` pipeline aggregation to find the first order derivative of the
  120. counter `system.network.out.bytes` for each value of the field `beat.name`.
  121. [source,js]
  122. ----------------------------------
  123. "aggregations": {
  124. "beat.name": {
  125. "terms": {
  126. "field": "beat.name"
  127. },
  128. "aggregations": {
  129. "buckets": {
  130. "date_histogram": {
  131. "field": "@timestamp",
  132. "fixed_interval": "5m"
  133. },
  134. "aggregations": {
  135. "@timestamp": {
  136. "max": {
  137. "field": "@timestamp"
  138. }
  139. },
  140. "bytes_out_average": {
  141. "avg": {
  142. "field": "system.network.out.bytes"
  143. }
  144. },
  145. "bytes_out_derivative": {
  146. "derivative": {
  147. "buckets_path": "bytes_out_average"
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. ----------------------------------
  156. // NOTCONSOLE
  157. [discrete]
  158. [[aggs-single-dfeeds]]
  159. == Single bucket aggregations in {dfeeds}
  160. {dfeeds-cap} not only supports multi-bucket aggregations, but also single bucket
  161. aggregations. The following shows two `filter` aggregations, each gathering the
  162. number of unique entries for the `error` field.
  163. [source,js]
  164. ----------------------------------
  165. {
  166. "job_id":"servers-unique-errors",
  167. "indices": ["logs-*"],
  168. "aggregations": {
  169. "buckets": {
  170. "date_histogram": {
  171. "field": "time",
  172. "interval": "360s",
  173. "time_zone": "UTC"
  174. },
  175. "aggregations": {
  176. "time": {
  177. "max": {"field": "time"}
  178. }
  179. "server1": {
  180. "filter": {"term": {"source": "server-name-1"}},
  181. "aggregations": {
  182. "server1_error_count": {
  183. "value_count": {
  184. "field": "error"
  185. }
  186. }
  187. }
  188. },
  189. "server2": {
  190. "filter": {"term": {"source": "server-name-2"}},
  191. "aggregations": {
  192. "server2_error_count": {
  193. "value_count": {
  194. "field": "error"
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }
  203. ----------------------------------
  204. // NOTCONSOLE
  205. [discrete]
  206. [[aggs-define-dfeeds]]
  207. == Defining aggregations in {dfeeds}
  208. When you define an aggregation in a {dfeed}, it must have the following form:
  209. [source,js]
  210. ----------------------------------
  211. "aggregations": {
  212. ["bucketing_aggregation": {
  213. "bucket_agg": {
  214. ...
  215. },
  216. "aggregations": {]
  217. "data_histogram_aggregation": {
  218. "date_histogram": {
  219. "field": "time",
  220. },
  221. "aggregations": {
  222. "timestamp": {
  223. "max": {
  224. "field": "time"
  225. }
  226. },
  227. [,"<first_term>": {
  228. "terms":{...
  229. }
  230. [,"aggregations" : {
  231. [<sub_aggregation>]+
  232. } ]
  233. }]
  234. }
  235. }
  236. }
  237. }
  238. }
  239. ----------------------------------
  240. // NOTCONSOLE
  241. The top level aggregation must be either a
  242. {ref}/search-aggregations-bucket.html[bucket aggregation] containing as single
  243. sub-aggregation that is a `date_histogram` or the top level aggregation is the
  244. required `date_histogram`. There must be exactly one `date_histogram`
  245. aggregation. For more information, see
  246. {ref}/search-aggregations-bucket-datehistogram-aggregation.html[Date histogram aggregation].
  247. NOTE: The `time_zone` parameter in the date histogram aggregation must be set to
  248. `UTC`, which is the default value.
  249. Each histogram bucket has a key, which is the bucket start time. This key cannot
  250. be used for aggregations in {dfeeds}, however, because they need to know the
  251. time of the latest record within a bucket. Otherwise, when you restart a
  252. {dfeed}, it continues from the start time of the histogram bucket and possibly
  253. fetches the same data twice. The max aggregation for the time field is therefore
  254. necessary to provide the time of the latest record within a bucket.
  255. You can optionally specify a terms aggregation, which creates buckets for
  256. different values of a field.
  257. IMPORTANT: If you use a terms aggregation, by default it returns buckets for
  258. the top ten terms. Thus if the cardinality of the term is greater than 10, not
  259. all terms are analyzed.
  260. You can change this behavior by setting the `size` parameter. To
  261. determine the cardinality of your data, you can run searches such as:
  262. [source,js]
  263. --------------------------------------------------
  264. GET .../_search {
  265. "aggs": {
  266. "service_cardinality": {
  267. "cardinality": {
  268. "field": "service"
  269. }
  270. }
  271. }
  272. }
  273. --------------------------------------------------
  274. // NOTCONSOLE
  275. By default, {es} limits the maximum number of terms returned to 10000. For high
  276. cardinality fields, the query might not run. It might return errors related to
  277. circuit breaking exceptions that indicate that the data is too large. In such
  278. cases, do not use aggregations in your {dfeed}. For more information, see
  279. {ref}/search-aggregations-bucket-terms-aggregation.html[Terms aggregation].
  280. You can also optionally specify multiple sub-aggregations. The sub-aggregations
  281. are aggregated for the buckets that were created by their parent aggregation.
  282. For more information, see {ref}/search-aggregations.html[Aggregations].