aggregations.asciidoc 9.0 KB

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