pipeline.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. [[search-aggregations-pipeline]]
  2. == Pipeline aggregations
  3. Pipeline aggregations work on the outputs produced from other aggregations rather than from document sets, adding
  4. information to the output tree. There are many different types of pipeline aggregation, each computing different information from
  5. other aggregations, but these types can be broken down into two families:
  6. _Parent_::
  7. A family of pipeline aggregations that is provided with the output of its parent aggregation and is able
  8. to compute new buckets or new aggregations to add to existing buckets.
  9. _Sibling_::
  10. Pipeline aggregations that are provided with the output of a sibling aggregation and are able to compute a
  11. new aggregation which will be at the same level as the sibling aggregation.
  12. Pipeline aggregations can reference the aggregations they need to perform their computation by using the `buckets_path`
  13. parameter to indicate the paths to the required metrics. The syntax for defining these paths can be found in the
  14. <<buckets-path-syntax, `buckets_path` Syntax>> section below.
  15. Pipeline aggregations cannot have sub-aggregations but depending on the type it can reference another pipeline in the `buckets_path`
  16. allowing pipeline aggregations to be chained. For example, you can chain together two derivatives to calculate the second derivative
  17. (i.e. a derivative of a derivative).
  18. NOTE: Because pipeline aggregations only add to the output, when chaining pipeline aggregations the output of each pipeline aggregation
  19. will be included in the final output.
  20. [[buckets-path-syntax]]
  21. [discrete]
  22. === `buckets_path` Syntax
  23. Most pipeline aggregations require another aggregation as their input. The input aggregation is defined via the `buckets_path`
  24. parameter, which follows a specific format:
  25. // https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form
  26. [source,ebnf]
  27. --------------------------------------------------
  28. AGG_SEPARATOR = `>` ;
  29. METRIC_SEPARATOR = `.` ;
  30. AGG_NAME = <the name of the aggregation> ;
  31. METRIC = <the name of the metric (in case of multi-value metrics aggregation)> ;
  32. MULTIBUCKET_KEY = `[<KEY_NAME>]`
  33. PATH = <AGG_NAME><MULTIBUCKET_KEY>? (<AGG_SEPARATOR>, <AGG_NAME> )* ( <METRIC_SEPARATOR>, <METRIC> ) ;
  34. --------------------------------------------------
  35. For example, the path `"my_bucket>my_stats.avg"` will path to the `avg` value in the `"my_stats"` metric, which is
  36. contained in the `"my_bucket"` bucket aggregation.
  37. Here are some more examples:
  38. --
  39. * `multi_bucket["foo"]>single_bucket>multi_metric.avg` will go to the `avg` metric in the `"multi_metric"` agg under the
  40. single bucket `"single_bucket"` within the `"foo"` bucket of the `"multi_bucket"` multi-bucket aggregation.
  41. * `agg1["foo"]._count` will get the `_count` metric for the `"foo"` bucket in the
  42. multi-bucket aggregation `"multi_bucket"`
  43. --
  44. Paths are relative from the position of the pipeline aggregation; they are not absolute paths, and the path cannot go back "up" the
  45. aggregation tree. For example, this derivative is embedded inside a date_histogram and refers to a "sibling"
  46. metric `"the_sum"`:
  47. [source,console,id=buckets-path-example]
  48. --------------------------------------------------
  49. POST /_search
  50. {
  51. "aggs": {
  52. "my_date_histo": {
  53. "date_histogram": {
  54. "field": "timestamp",
  55. "calendar_interval": "day"
  56. },
  57. "aggs": {
  58. "the_sum": {
  59. "sum": { "field": "lemmings" } <1>
  60. },
  61. "the_deriv": {
  62. "derivative": { "buckets_path": "the_sum" } <2>
  63. }
  64. }
  65. }
  66. }
  67. }
  68. --------------------------------------------------
  69. <1> The metric is called `"the_sum"`
  70. <2> The `buckets_path` refers to the metric via a relative path `"the_sum"`
  71. `buckets_path` is also used for Sibling pipeline aggregations, where the aggregation is "next" to a series of buckets
  72. instead of embedded "inside" them. For example, the `max_bucket` aggregation uses the `buckets_path` to specify
  73. a metric embedded inside a sibling aggregation:
  74. [source,console,id=buckets-path-sibling-example]
  75. --------------------------------------------------
  76. POST /_search
  77. {
  78. "aggs": {
  79. "sales_per_month": {
  80. "date_histogram": {
  81. "field": "date",
  82. "calendar_interval": "month"
  83. },
  84. "aggs": {
  85. "sales": {
  86. "sum": {
  87. "field": "price"
  88. }
  89. }
  90. }
  91. },
  92. "max_monthly_sales": {
  93. "max_bucket": {
  94. "buckets_path": "sales_per_month>sales" <1>
  95. }
  96. }
  97. }
  98. }
  99. --------------------------------------------------
  100. // TEST[setup:sales]
  101. <1> `buckets_path` instructs this max_bucket aggregation that we want the maximum value of the `sales` aggregation in the
  102. `sales_per_month` date histogram.
  103. If a Sibling pipeline agg references a multi-bucket aggregation, such as a `terms` agg, it also has the option to
  104. select specific keys from the multi-bucket. For example, a `bucket_script` could select two specific buckets (via
  105. their bucket keys) to perform the calculation:
  106. [source,console,id=buckets-path-specific-bucket-example]
  107. --------------------------------------------------
  108. POST /_search
  109. {
  110. "aggs": {
  111. "sales_per_month": {
  112. "date_histogram": {
  113. "field": "date",
  114. "calendar_interval": "month"
  115. },
  116. "aggs": {
  117. "sale_type": {
  118. "terms": {
  119. "field": "type"
  120. },
  121. "aggs": {
  122. "sales": {
  123. "sum": {
  124. "field": "price"
  125. }
  126. }
  127. }
  128. },
  129. "hat_vs_bag_ratio": {
  130. "bucket_script": {
  131. "buckets_path": {
  132. "hats": "sale_type['hat']>sales", <1>
  133. "bags": "sale_type['bag']>sales" <1>
  134. },
  135. "script": "params.hats / params.bags"
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. --------------------------------------------------
  143. // TEST[setup:sales]
  144. <1> `buckets_path` selects the hats and bags buckets (via `['hat']`/`['bag']``) to use in the script specifically,
  145. instead of fetching all the buckets from `sale_type` aggregation
  146. [discrete]
  147. === Special Paths
  148. Instead of pathing to a metric, `buckets_path` can use a special `"_count"` path. This instructs
  149. the pipeline aggregation to use the document count as its input. For example, a derivative can be calculated
  150. on the document count of each bucket, instead of a specific metric:
  151. [source,console,id=buckets-path-count-example]
  152. --------------------------------------------------
  153. POST /_search
  154. {
  155. "aggs": {
  156. "my_date_histo": {
  157. "date_histogram": {
  158. "field": "timestamp",
  159. "calendar_interval": "day"
  160. },
  161. "aggs": {
  162. "the_deriv": {
  163. "derivative": { "buckets_path": "_count" } <1>
  164. }
  165. }
  166. }
  167. }
  168. }
  169. --------------------------------------------------
  170. <1> By using `_count` instead of a metric name, we can calculate the derivative of document counts in the histogram
  171. The `buckets_path` can also use `"_bucket_count"` and path to a multi-bucket aggregation to use the number of buckets
  172. returned by that aggregation in the pipeline aggregation instead of a metric. For example, a `bucket_selector` can be
  173. used here to filter out buckets which contain no buckets for an inner terms aggregation:
  174. [source,console,id=buckets-path-bucket-count-example]
  175. --------------------------------------------------
  176. POST /sales/_search
  177. {
  178. "size": 0,
  179. "aggs": {
  180. "histo": {
  181. "date_histogram": {
  182. "field": "date",
  183. "calendar_interval": "day"
  184. },
  185. "aggs": {
  186. "categories": {
  187. "terms": {
  188. "field": "category"
  189. }
  190. },
  191. "min_bucket_selector": {
  192. "bucket_selector": {
  193. "buckets_path": {
  194. "count": "categories._bucket_count" <1>
  195. },
  196. "script": {
  197. "source": "params.count != 0"
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. --------------------------------------------------
  206. // TEST[setup:sales]
  207. <1> By using `_bucket_count` instead of a metric name, we can filter out `histo` buckets where they contain no buckets
  208. for the `categories` aggregation
  209. [[dots-in-agg-names]]
  210. [discrete]
  211. === Dealing with dots in agg names
  212. An alternate syntax is supported to cope with aggregations or metrics which
  213. have dots in the name, such as the ++99.9++th
  214. <<search-aggregations-metrics-percentile-aggregation,percentile>>. This metric
  215. may be referred to as:
  216. [source,js]
  217. ---------------
  218. "buckets_path": "my_percentile[99.9]"
  219. ---------------
  220. // NOTCONSOLE
  221. [[gap-policy]]
  222. [discrete]
  223. === Dealing with gaps in the data
  224. Data in the real world is often noisy and sometimes contains *gaps* -- places where data simply doesn't exist. This can
  225. occur for a variety of reasons, the most common being:
  226. * Documents falling into a bucket do not contain a required field
  227. * There are no documents matching the query for one or more buckets
  228. * The metric being calculated is unable to generate a value, likely because another dependent bucket is missing a value.
  229. Some pipeline aggregations have specific requirements that must be met (e.g. a derivative cannot calculate a metric for the
  230. first value because there is no previous value, HoltWinters moving average need "warmup" data to begin calculating, etc)
  231. Gap policies are a mechanism to inform the pipeline aggregation about the desired behavior when "gappy" or missing
  232. data is encountered. All pipeline aggregations accept the `gap_policy` parameter. There are currently two gap policies
  233. to choose from:
  234. _skip_::
  235. This option treats missing data as if the bucket does not exist. It will skip the bucket and continue
  236. calculating using the next available value.
  237. _insert_zeros_::
  238. This option will replace missing values with a zero (`0`) and pipeline aggregation computation will
  239. proceed as normal.
  240. _keep_values_::
  241. This option is similar to skip, except if the metric provides a non-null, non-NaN value this value is
  242. used, otherwise the empty bucket is skipped.
  243. include::pipeline/avg-bucket-aggregation.asciidoc[]
  244. include::pipeline/bucket-script-aggregation.asciidoc[]
  245. include::pipeline/bucket-count-ks-test-aggregation.asciidoc[]
  246. include::pipeline/bucket-correlation-aggregation.asciidoc[]
  247. include::pipeline/bucket-selector-aggregation.asciidoc[]
  248. include::pipeline/bucket-sort-aggregation.asciidoc[]
  249. include::pipeline/change-point-aggregation.asciidoc[]
  250. include::pipeline/cumulative-cardinality-aggregation.asciidoc[]
  251. include::pipeline/cumulative-sum-aggregation.asciidoc[]
  252. include::pipeline/derivative-aggregation.asciidoc[]
  253. include::pipeline/extended-stats-bucket-aggregation.asciidoc[]
  254. include::pipeline/inference-bucket-aggregation.asciidoc[]
  255. include::pipeline/max-bucket-aggregation.asciidoc[]
  256. include::pipeline/min-bucket-aggregation.asciidoc[]
  257. include::pipeline/movfn-aggregation.asciidoc[]
  258. include::pipeline/moving-percentiles-aggregation.asciidoc[]
  259. include::pipeline/normalize-aggregation.asciidoc[]
  260. include::pipeline/percentiles-bucket-aggregation.asciidoc[]
  261. include::pipeline/serial-diff-aggregation.asciidoc[]
  262. include::pipeline/stats-bucket-aggregation.asciidoc[]
  263. include::pipeline/sum-bucket-aggregation.asciidoc[]