aggregations.asciidoc 9.3 KB

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