bucket-selector-aggregation.asciidoc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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": "my_var1 > my_var2"
  21. }
  22. }
  23. --------------------------------------------------
  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` Parameters
  27. |===
  28. |Parameter Name |Description |Required |Default Value
  29. |`script` |The script to run for this aggregation. The script can be inline, file or indexed. (see <<modules-scripting>>
  30. for more details) |Required |
  31. |`buckets_path` |A map of script variables and their associated path to the buckets we wish to use for the variable
  32. (see <<buckets-path-syntax>> for more details) |Required |
  33. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  34. details)|Optional, defaults to `skip` |
  35. |===
  36. The following snippet only retains buckets where the total sales for the month is less than or equal to 50:
  37. [source,js]
  38. --------------------------------------------------
  39. {
  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. "sales_bucket_filter": {
  53. "bucket_selector": {
  54. "buckets_path": {
  55. "totalSales": "total_sales"
  56. },
  57. "script": "totalSales <= 50"
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. --------------------------------------------------
  65. And the following may be the response:
  66. [source,js]
  67. --------------------------------------------------
  68. {
  69. "aggregations": {
  70. "sales_per_month": {
  71. "buckets": [
  72. {
  73. "key_as_string": "2015/01/01 00:00:00",
  74. "key": 1420070400000,
  75. "doc_count": 3,
  76. "total_sales": {
  77. "value": 50
  78. }
  79. },<1>
  80. {
  81. "key_as_string": "2015/03/01 00:00:00",
  82. "key": 1425168000000,
  83. "doc_count": 2,
  84. "total_sales": {
  85. "value": 40
  86. },
  87. }
  88. ]
  89. }
  90. }
  91. }
  92. --------------------------------------------------
  93. <1> Bucket for `2015/02/01 00:00:00` has been removed as its total sales exceeded 50