series-arithmetic-aggregation.asciidoc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. [[search-aggregations-pipeline-series-arithmetic-aggregation]]
  2. === Series Arithmetic Aggregation
  3. A parent pipeline aggregation which executes a script which can perform per bucket computations on specified metrics
  4. in the parent multi-bucket aggregation. The specified metric must be numeric and the script must return a numeric value.
  5. ==== Syntax
  6. A `series_arithmetic` aggregation looks like this in isolation:
  7. [source,js]
  8. --------------------------------------------------
  9. {
  10. "series_arithmetic": {
  11. "buckets_path": {
  12. "my_var1": "the_sum", <1>
  13. "my_var2": "the_value_count"
  14. },
  15. script: "my_var1 / my_var2"
  16. }
  17. }
  18. --------------------------------------------------
  19. <1> Here, `my_var1` is the name of the variable for this buckets path to use in the script, `the_sum` is the path to
  20. the metrics to use for that variable.
  21. .`series_arithmetic` Parameters
  22. |===
  23. |Parameter Name |Description |Required |Default Value
  24. |`script` |The script to run for this aggregation. The script can be inline, file or indexed. (see <<modules-scripting>>
  25. for more details) |Required |
  26. |`buckets_path` |A map of script variables and their associated path to the buckets we wish to use for the variable
  27. (see <<bucket-path-syntax>> for more details) |Required |
  28. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  29. details)|Optional, defaults to `skip` |
  30. |`format` |format to apply to the output value of this aggregation |Optional, defaults to `null` |
  31. |===
  32. The following snippet calculates the ratio percentage of t-shirt sales compared to total sales each month:
  33. [source,js]
  34. --------------------------------------------------
  35. {
  36. "aggs" : {
  37. "sales_per_month" : {
  38. "date_histogram" : {
  39. "field" : "date",
  40. "interval" : "month"
  41. },
  42. "aggs": {
  43. "total_sales": {
  44. "sum": {
  45. "field": "price"
  46. }
  47. },
  48. "t-shirts": {
  49. "filter": {
  50. "term": {
  51. "type": "t-shirt"
  52. }
  53. },
  54. "aggs": {
  55. "sales": {
  56. "sum": {
  57. "field": "price"
  58. }
  59. }
  60. }
  61. },
  62. "t-shirt-percentage": {
  63. "series_arithmetic": {
  64. "buckets_paths": {
  65. "tShirtSales": "t-shirts>sales",
  66. "totalSales": "total_sales"
  67. },
  68. "script": "tShirtSales / totalSales * 100"
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75. --------------------------------------------------
  76. And the following may be the response:
  77. [source,js]
  78. --------------------------------------------------
  79. {
  80. "aggregations": {
  81. "sales_per_month": {
  82. "buckets": [
  83. {
  84. "key_as_string": "2015/01/01 00:00:00",
  85. "key": 1420070400000,
  86. "doc_count": 3,
  87. "total_sales": {
  88. "value": 50
  89. },
  90. "t-shirts": {
  91. "doc_count": 2,
  92. "sales": {
  93. "value": 10
  94. }
  95. },
  96. "t-shirt-percentage": {
  97. "value": 20
  98. }
  99. },
  100. {
  101. "key_as_string": "2015/02/01 00:00:00",
  102. "key": 1422748800000,
  103. "doc_count": 2
  104. "total_sales": {
  105. "value": 60
  106. },
  107. "t-shirts": {
  108. "doc_count": 1,
  109. "sales": {
  110. "value": 15
  111. }
  112. },
  113. "t-shirt-percentage": {
  114. "value": 25
  115. }
  116. },
  117. {
  118. "key_as_string": "2015/03/01 00:00:00",
  119. "key": 1425168000000,
  120. "doc_count": 2,
  121. "total_sales": {
  122. "value": 40
  123. },
  124. "t-shirts": {
  125. "doc_count": 1,
  126. "sales": {
  127. "value": 20
  128. }
  129. },
  130. "t-shirt-percentage": {
  131. "value": 50
  132. }
  133. }
  134. ]
  135. }
  136. }
  137. }
  138. --------------------------------------------------