aggregations.asciidoc 7.9 KB

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