aggregations.asciidoc 9.1 KB

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