bucket-script-aggregation.asciidoc 4.9 KB

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