bucket-script-aggregation.asciidoc 4.5 KB

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