aggregations.asciidoc 9.0 KB

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