max-bucket-aggregation.asciidoc 3.0 KB

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