pipeline.asciidoc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. [float]
  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. PATH = <AGG_NAME> [ <AGG_SEPARATOR>, <AGG_NAME> ]* [ <METRIC_SEPARATOR>, <METRIC> ] ;
  33. --------------------------------------------------
  34. For example, the path `"my_bucket>my_stats.avg"` will path to the `avg` value in the `"my_stats"` metric, which is
  35. contained in the `"my_bucket"` bucket aggregation.
  36. Paths are relative from the position of the pipeline aggregation; they are not absolute paths, and the path cannot go back "up" the
  37. aggregation tree. For example, this moving average is embedded inside a date_histogram and refers to a "sibling"
  38. metric `"the_sum"`:
  39. [source,js]
  40. --------------------------------------------------
  41. POST /_search
  42. {
  43. "aggs": {
  44. "my_date_histo":{
  45. "date_histogram":{
  46. "field":"timestamp",
  47. "interval":"day"
  48. },
  49. "aggs":{
  50. "the_sum":{
  51. "sum":{ "field": "lemmings" } <1>
  52. },
  53. "the_movavg":{
  54. "moving_avg":{ "buckets_path": "the_sum" } <2>
  55. }
  56. }
  57. }
  58. }
  59. }
  60. --------------------------------------------------
  61. // CONSOLE
  62. // TEST[warning:The moving_avg aggregation has been deprecated in favor of the moving_fn aggregation.]
  63. <1> The metric is called `"the_sum"`
  64. <2> The `buckets_path` refers to the metric via a relative path `"the_sum"`
  65. `buckets_path` is also used for Sibling pipeline aggregations, where the aggregation is "next" to a series of buckets
  66. instead of embedded "inside" them. For example, the `max_bucket` aggregation uses the `buckets_path` to specify
  67. a metric embedded inside a sibling aggregation:
  68. [source,js]
  69. --------------------------------------------------
  70. POST /_search
  71. {
  72. "aggs" : {
  73. "sales_per_month" : {
  74. "date_histogram" : {
  75. "field" : "date",
  76. "interval" : "month"
  77. },
  78. "aggs": {
  79. "sales": {
  80. "sum": {
  81. "field": "price"
  82. }
  83. }
  84. }
  85. },
  86. "max_monthly_sales": {
  87. "max_bucket": {
  88. "buckets_path": "sales_per_month>sales" <1>
  89. }
  90. }
  91. }
  92. }
  93. --------------------------------------------------
  94. // CONSOLE
  95. // TEST[setup:sales]
  96. <1> `buckets_path` instructs this max_bucket aggregation that we want the maximum value of the `sales` aggregation in the
  97. `sales_per_month` date histogram.
  98. [float]
  99. === Special Paths
  100. Instead of pathing to a metric, `buckets_path` can use a special `"_count"` path. This instructs
  101. the pipeline aggregation to use the document count as its input. For example, a moving average can be calculated on the document count of each bucket, instead of a specific metric:
  102. [source,js]
  103. --------------------------------------------------
  104. POST /_search
  105. {
  106. "aggs": {
  107. "my_date_histo": {
  108. "date_histogram": {
  109. "field":"timestamp",
  110. "interval":"day"
  111. },
  112. "aggs": {
  113. "the_movavg": {
  114. "moving_avg": { "buckets_path": "_count" } <1>
  115. }
  116. }
  117. }
  118. }
  119. }
  120. --------------------------------------------------
  121. // CONSOLE
  122. // TEST[warning:The moving_avg aggregation has been deprecated in favor of the moving_fn aggregation.]
  123. <1> By using `_count` instead of a metric name, we can calculate the moving average of document counts in the histogram
  124. The `buckets_path` can also use `"_bucket_count"` and path to a multi-bucket aggregation to use the number of buckets
  125. returned by that aggregation in the pipeline aggregation instead of a metric. for example a `bucket_selector` can be
  126. used here to filter out buckets which contain no buckets for an inner terms aggregation:
  127. [source,js]
  128. --------------------------------------------------
  129. POST /sales/_search
  130. {
  131. "size": 0,
  132. "aggs": {
  133. "histo": {
  134. "date_histogram": {
  135. "field": "date",
  136. "interval": "day"
  137. },
  138. "aggs": {
  139. "categories": {
  140. "terms": {
  141. "field": "category"
  142. }
  143. },
  144. "min_bucket_selector": {
  145. "bucket_selector": {
  146. "buckets_path": {
  147. "count": "categories._bucket_count" <1>
  148. },
  149. "script": {
  150. "source": "params.count != 0"
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. --------------------------------------------------
  159. // CONSOLE
  160. // TEST[setup:sales]
  161. <1> By using `_bucket_count` instead of a metric name, we can filter out `histo` buckets where they contain no buckets
  162. for the `categories` aggregation
  163. [[dots-in-agg-names]]
  164. [float]
  165. === Dealing with dots in agg names
  166. An alternate syntax is supported to cope with aggregations or metrics which
  167. have dots in the name, such as the ++99.9++th
  168. <<search-aggregations-metrics-percentile-aggregation,percentile>>. This metric
  169. may be referred to as:
  170. [source,js]
  171. ---------------
  172. "buckets_path": "my_percentile[99.9]"
  173. ---------------
  174. // NOTCONSOLE
  175. [[gap-policy]]
  176. [float]
  177. === Dealing with gaps in the data
  178. Data in the real world is often noisy and sometimes contains *gaps* -- places where data simply doesn't exist. This can
  179. occur for a variety of reasons, the most common being:
  180. * Documents falling into a bucket do not contain a required field
  181. * There are no documents matching the query for one or more buckets
  182. * The metric being calculated is unable to generate a value, likely because another dependent bucket is missing a value.
  183. Some pipeline aggregations have specific requirements that must be met (e.g. a derivative cannot calculate a metric for the
  184. first value because there is no previous value, HoltWinters moving average need "warmup" data to begin calculating, etc)
  185. Gap policies are a mechanism to inform the pipeline aggregation about the desired behavior when "gappy" or missing
  186. data is encountered. All pipeline aggregations accept the `gap_policy` parameter. There are currently two gap policies
  187. to choose from:
  188. _skip_::
  189. This option treats missing data as if the bucket does not exist. It will skip the bucket and continue
  190. calculating using the next available value.
  191. _insert_zeros_::
  192. This option will replace missing values with a zero (`0`) and pipeline aggregation computation will
  193. proceed as normal.
  194. include::pipeline/avg-bucket-aggregation.asciidoc[]
  195. include::pipeline/derivative-aggregation.asciidoc[]
  196. include::pipeline/max-bucket-aggregation.asciidoc[]
  197. include::pipeline/min-bucket-aggregation.asciidoc[]
  198. include::pipeline/sum-bucket-aggregation.asciidoc[]
  199. include::pipeline/stats-bucket-aggregation.asciidoc[]
  200. include::pipeline/extended-stats-bucket-aggregation.asciidoc[]
  201. include::pipeline/percentiles-bucket-aggregation.asciidoc[]
  202. include::pipeline/movavg-aggregation.asciidoc[]
  203. include::pipeline/movfn-aggregation.asciidoc[]
  204. include::pipeline/cumulative-sum-aggregation.asciidoc[]
  205. include::pipeline/bucket-script-aggregation.asciidoc[]
  206. include::pipeline/bucket-selector-aggregation.asciidoc[]
  207. include::pipeline/bucket-sort-aggregation.asciidoc[]
  208. include::pipeline/serial-diff-aggregation.asciidoc[]