max-bucket-aggregation.asciidoc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. [[search-aggregations-reducer-max-bucket-aggregation]]
  2. === Max Bucket Aggregation
  3. A sibling reducer aggregation which identifies the bucket(s) with the maximum value of a specified metric in a sibling aggregation
  4. and outputs both the value and the key(s) of the bucket(s). The specified metric must be numeric and the sibling aggregation must
  5. be a multi-bucket aggregation.
  6. ==== Syntax
  7. A `max_bucket` aggregation looks like this in isolation:
  8. [source,js]
  9. --------------------------------------------------
  10. {
  11. "max_bucket": {
  12. "buckets_path": "the_sum"
  13. }
  14. }
  15. --------------------------------------------------
  16. .`max_bucket` Parameters
  17. |===
  18. |Parameter Name |Description |Required |Default Value
  19. |`buckets_path` |The path to the buckets we wish to find the maximum for (see <<bucket-path-syntax>> for more
  20. details |Required |
  21. |===
  22. The following snippet calculates the maximum of the total monthly `sales`:
  23. [source,js]
  24. --------------------------------------------------
  25. {
  26. "aggs" : {
  27. "sales_per_month" : {
  28. "date_histogram" : {
  29. "field" : "date",
  30. "interval" : "month"
  31. },
  32. "aggs": {
  33. "sales": {
  34. "sum": {
  35. "field": "price"
  36. }
  37. }
  38. }
  39. },
  40. "max_monthly_sales": {
  41. "max_bucket": {
  42. "buckets_paths": "sales_per_month>sales" <1>
  43. }
  44. }
  45. }
  46. }
  47. --------------------------------------------------
  48. <1> `bucket_paths` instructs this max_bucket aggregation that we want the maximum value of the `sales` aggregation in the
  49. `sales_per_month` date histogram.
  50. And the following may be the response:
  51. [source,js]
  52. --------------------------------------------------
  53. {
  54. "aggregations": {
  55. "sales_per_month": {
  56. "buckets": [
  57. {
  58. "key_as_string": "2015/01/01 00:00:00",
  59. "key": 1420070400000,
  60. "doc_count": 3,
  61. "sales": {
  62. "value": 550
  63. }
  64. },
  65. {
  66. "key_as_string": "2015/02/01 00:00:00",
  67. "key": 1422748800000,
  68. "doc_count": 2,
  69. "sales": {
  70. "value": 60
  71. }
  72. },
  73. {
  74. "key_as_string": "2015/03/01 00:00:00",
  75. "key": 1425168000000,
  76. "doc_count": 2,
  77. "sales": {
  78. "value": 375
  79. }
  80. }
  81. ]
  82. },
  83. "max_monthly_sales": {
  84. "keys": ["2015/01/01 00:00:00"], <1>
  85. "value": 550
  86. }
  87. }
  88. }
  89. --------------------------------------------------
  90. <1> `keys` is an array of strings since the maximum value may be present in multiple buckets