bucket-selector-aggregation.asciidoc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. [[search-aggregations-pipeline-bucket-selector-aggregation]]
  2. === Bucket Selector Aggregation
  3. A parent pipeline aggregation which executes a script which determines whether the current bucket will be retained
  4. in the parent multi-bucket aggregation. The specified metric must be numeric and the script must return a boolean value.
  5. If the script language is `expression` then a numeric return value is permitted. In this case 0.0 will be evaluated as `false`
  6. and all other values will evaluate to true.
  7. NOTE: The bucket_selector aggregation, like all pipeline aggregations, executes after all other sibling aggregations. This means that
  8. using the bucket_selector aggregation to filter the returned buckets in the response does not save on execution time running the aggregations.
  9. ==== Syntax
  10. A `bucket_selector` aggregation looks like this in isolation:
  11. [source,js]
  12. --------------------------------------------------
  13. {
  14. "bucket_selector": {
  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-selector-params]]
  27. .`bucket_selector` 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. |===
  38. The following snippet only retains buckets where the total sales for the month is more than 200:
  39. [source,console]
  40. --------------------------------------------------
  41. POST /sales/_search
  42. {
  43. "size": 0,
  44. "aggs": {
  45. "sales_per_month": {
  46. "date_histogram": {
  47. "field": "date",
  48. "calendar_interval": "month"
  49. },
  50. "aggs": {
  51. "total_sales": {
  52. "sum": {
  53. "field": "price"
  54. }
  55. },
  56. "sales_bucket_filter": {
  57. "bucket_selector": {
  58. "buckets_path": {
  59. "totalSales": "total_sales"
  60. },
  61. "script": "params.totalSales > 200"
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. --------------------------------------------------
  69. // TEST[setup:sales]
  70. And the following may be the response:
  71. [source,console-result]
  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