bucket-selector-aggregation.asciidoc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. [[search-aggregations-pipeline-bucket-selector-aggregation]]
  2. === Bucket Selector Aggregation
  3. experimental[]
  4. A parent pipeline aggregation which executes a script which determines whether the current bucket will be retained
  5. in the parent multi-bucket aggregation. The specified metric must be numeric and the script must return a boolean value.
  6. If the script language is `expression` then a numeric return value is permitted. In this case 0.0 will be evaluated as `false`
  7. and all other values will evaluate to true.
  8. Note: The bucket_selector aggregation, like all pipeline aggregations, executions after all other sibling aggregations. This means that
  9. using the bucket_selector aggregation to filter the returned buckets in the response does not save on execution time running the aggregations.
  10. ==== Syntax
  11. A `bucket_selector` aggregation looks like this in isolation:
  12. [source,js]
  13. --------------------------------------------------
  14. {
  15. "bucket_selector": {
  16. "buckets_path": {
  17. "my_var1": "the_sum", <1>
  18. "my_var2": "the_value_count"
  19. },
  20. "script": "params.my_var1 > params.my_var2"
  21. }
  22. }
  23. --------------------------------------------------
  24. // NOTCONSOLE
  25. <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
  26. the metrics to use for that variable.
  27. .`bucket_selector` Parameters
  28. |===
  29. |Parameter Name |Description |Required |Default Value
  30. |`script` |The script to run for this aggregation. The script can be inline, file or indexed. (see <<modules-scripting>>
  31. for more details) |Required |
  32. |`buckets_path` |A map of script variables and their associated path to the buckets we wish to use for the variable
  33. (see <<buckets-path-syntax>> for more details) |Required |
  34. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  35. details)|Optional |`skip`
  36. |===
  37. The following snippet only retains buckets where the total sales for the month is more than 400:
  38. [source,js]
  39. --------------------------------------------------
  40. POST /sales/_search
  41. {
  42. "size": 0,
  43. "aggs" : {
  44. "sales_per_month" : {
  45. "date_histogram" : {
  46. "field" : "date",
  47. "interval" : "month"
  48. },
  49. "aggs": {
  50. "total_sales": {
  51. "sum": {
  52. "field": "price"
  53. }
  54. },
  55. "sales_bucket_filter": {
  56. "bucket_selector": {
  57. "buckets_path": {
  58. "totalSales": "total_sales"
  59. },
  60. "script": "params.totalSales > 200"
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. --------------------------------------------------
  68. // CONSOLE
  69. // TEST[setup:sales]
  70. And the following may be the response:
  71. [source,js]
  72. --------------------------------------------------
  73. {
  74. "took": 11,
  75. "timed_out": false,
  76. "_shards": ...,
  77. "hits": ...,
  78. "aggregations": {
  79. "sales_per_month": {
  80. "buckets": [
  81. {
  82. "key_as_string": "2015/01/01 00:00:00",
  83. "key": 1420070400000,
  84. "doc_count": 3,
  85. "total_sales": {
  86. "value": 550.0
  87. }
  88. },<1>
  89. {
  90. "key_as_string": "2015/03/01 00:00:00",
  91. "key": 1425168000000,
  92. "doc_count": 2,
  93. "total_sales": {
  94. "value": 375.0
  95. },
  96. }
  97. ]
  98. }
  99. }
  100. }
  101. --------------------------------------------------
  102. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  103. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  104. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  105. <1> Bucket for `2015/02/01 00:00:00` has been removed as its total sales was less than 200