bucket-script-aggregation.asciidoc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. [[search-aggregations-pipeline-bucket-script-aggregation]]
  2. === Bucket script aggregation
  3. ++++
  4. <titleabbrev>Bucket script</titleabbrev>
  5. ++++
  6. A parent pipeline aggregation which executes a script which can perform per bucket computations on specified metrics
  7. in the parent multi-bucket aggregation. The specified metric must be numeric and the script must return a numeric value.
  8. [[bucket-script-agg-syntax]]
  9. ==== Syntax
  10. A `bucket_script` aggregation looks like this in isolation:
  11. [source,js]
  12. --------------------------------------------------
  13. {
  14. "bucket_script": {
  15. "buckets_path": {
  16. "my_var1": "the_sum", <1>
  17. "my_var2": "the_value_count"
  18. },
  19. "script": "params.my_var1 / params.my_var2"
  20. }
  21. }
  22. --------------------------------------------------
  23. // NOTCONSOLE
  24. <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
  25. the metrics to use for that variable.
  26. [[bucket-script-params]]
  27. .`bucket_script` Parameters
  28. [options="header"]
  29. |===
  30. |Parameter Name |Description |Required |Default Value
  31. |`script` |The script to run for this aggregation. The script can be inline, file or indexed. (see <<modules-scripting>>
  32. for more details) |Required |
  33. |`buckets_path` |A map of script variables and their associated path to the buckets we wish to use for the variable
  34. (see <<buckets-path-syntax>> for more details) |Required |
  35. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  36. details)|Optional |`skip`
  37. |`format` |{javadoc}/java.base/java/text/DecimalFormat.html[DecimalFormat pattern] for the
  38. output value. If specified, the formatted value is returned in the aggregation's
  39. `value_as_string` property |Optional |`null`
  40. |===
  41. The following snippet calculates the ratio percentage of t-shirt sales compared to total sales each month:
  42. [source,console]
  43. --------------------------------------------------
  44. POST /sales/_search
  45. {
  46. "size": 0,
  47. "aggs": {
  48. "sales_per_month": {
  49. "date_histogram": {
  50. "field": "date",
  51. "calendar_interval": "month"
  52. },
  53. "aggs": {
  54. "total_sales": {
  55. "sum": {
  56. "field": "price"
  57. }
  58. },
  59. "t-shirts": {
  60. "filter": {
  61. "term": {
  62. "type": "t-shirt"
  63. }
  64. },
  65. "aggs": {
  66. "sales": {
  67. "sum": {
  68. "field": "price"
  69. }
  70. }
  71. }
  72. },
  73. "t-shirt-percentage": {
  74. "bucket_script": {
  75. "buckets_path": {
  76. "tShirtSales": "t-shirts>sales",
  77. "totalSales": "total_sales"
  78. },
  79. "script": "params.tShirtSales / params.totalSales * 100"
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. --------------------------------------------------
  87. // TEST[setup:sales]
  88. And the following may be the response:
  89. [source,console-result]
  90. --------------------------------------------------
  91. {
  92. "took": 11,
  93. "timed_out": false,
  94. "_shards": ...,
  95. "hits": ...,
  96. "aggregations": {
  97. "sales_per_month": {
  98. "buckets": [
  99. {
  100. "key_as_string": "2015/01/01 00:00:00",
  101. "key": 1420070400000,
  102. "doc_count": 3,
  103. "total_sales": {
  104. "value": 550.0
  105. },
  106. "t-shirts": {
  107. "doc_count": 1,
  108. "sales": {
  109. "value": 200.0
  110. }
  111. },
  112. "t-shirt-percentage": {
  113. "value": 36.36363636363637
  114. }
  115. },
  116. {
  117. "key_as_string": "2015/02/01 00:00:00",
  118. "key": 1422748800000,
  119. "doc_count": 2,
  120. "total_sales": {
  121. "value": 60.0
  122. },
  123. "t-shirts": {
  124. "doc_count": 1,
  125. "sales": {
  126. "value": 10.0
  127. }
  128. },
  129. "t-shirt-percentage": {
  130. "value": 16.666666666666664
  131. }
  132. },
  133. {
  134. "key_as_string": "2015/03/01 00:00:00",
  135. "key": 1425168000000,
  136. "doc_count": 2,
  137. "total_sales": {
  138. "value": 375.0
  139. },
  140. "t-shirts": {
  141. "doc_count": 1,
  142. "sales": {
  143. "value": 175.0
  144. }
  145. },
  146. "t-shirt-percentage": {
  147. "value": 46.666666666666664
  148. }
  149. }
  150. ]
  151. }
  152. }
  153. }
  154. --------------------------------------------------
  155. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  156. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  157. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]