max-bucket-aggregation.asciidoc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. [[search-aggregations-pipeline-max-bucket-aggregation]]
  2. === Max bucket aggregation
  3. ++++
  4. <titleabbrev>Max bucket</titleabbrev>
  5. ++++
  6. A sibling pipeline aggregation which identifies the bucket(s) with the maximum value of a specified metric in a sibling aggregation
  7. and outputs both the value and the key(s) of the bucket(s). The specified metric must be numeric and the sibling aggregation must
  8. be a multi-bucket aggregation.
  9. ==== Syntax
  10. A `max_bucket` aggregation looks like this in isolation:
  11. [source,js]
  12. --------------------------------------------------
  13. {
  14. "max_bucket": {
  15. "buckets_path": "the_sum"
  16. }
  17. }
  18. --------------------------------------------------
  19. // NOTCONSOLE
  20. [[max-bucket-params]]
  21. .`max_bucket` Parameters
  22. [options="header"]
  23. |===
  24. |Parameter Name |Description |Required |Default Value
  25. |`buckets_path` |The path to the buckets we wish to find the maximum for (see <<buckets-path-syntax>> for more
  26. details) |Required |
  27. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  28. details)|Optional | `skip`
  29. |`format` |{javadoc}/java.base/java/text/DecimalFormat.html[DecimalFormat pattern] for the
  30. output value. If specified, the formatted value is returned in the aggregation's
  31. `value_as_string` property |Optional |`null`
  32. |===
  33. The following snippet calculates the maximum of the total monthly `sales`:
  34. [source,console]
  35. --------------------------------------------------
  36. POST /sales/_search
  37. {
  38. "size": 0,
  39. "aggs": {
  40. "sales_per_month": {
  41. "date_histogram": {
  42. "field": "date",
  43. "calendar_interval": "month"
  44. },
  45. "aggs": {
  46. "sales": {
  47. "sum": {
  48. "field": "price"
  49. }
  50. }
  51. }
  52. },
  53. "max_monthly_sales": {
  54. "max_bucket": {
  55. "buckets_path": "sales_per_month>sales" <1>
  56. }
  57. }
  58. }
  59. }
  60. --------------------------------------------------
  61. // TEST[setup:sales]
  62. <1> `buckets_path` instructs this max_bucket aggregation that we want the maximum value of the `sales` aggregation in the
  63. `sales_per_month` date histogram.
  64. And the following may be the response:
  65. [source,console-result]
  66. --------------------------------------------------
  67. {
  68. "took": 11,
  69. "timed_out": false,
  70. "_shards": ...,
  71. "hits": ...,
  72. "aggregations": {
  73. "sales_per_month": {
  74. "buckets": [
  75. {
  76. "key_as_string": "2015/01/01 00:00:00",
  77. "key": 1420070400000,
  78. "doc_count": 3,
  79. "sales": {
  80. "value": 550.0
  81. }
  82. },
  83. {
  84. "key_as_string": "2015/02/01 00:00:00",
  85. "key": 1422748800000,
  86. "doc_count": 2,
  87. "sales": {
  88. "value": 60.0
  89. }
  90. },
  91. {
  92. "key_as_string": "2015/03/01 00:00:00",
  93. "key": 1425168000000,
  94. "doc_count": 2,
  95. "sales": {
  96. "value": 375.0
  97. }
  98. }
  99. ]
  100. },
  101. "max_monthly_sales": {
  102. "keys": ["2015/01/01 00:00:00"], <1>
  103. "value": 550.0
  104. }
  105. }
  106. }
  107. --------------------------------------------------
  108. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  109. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  110. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  111. <1> `keys` is an array of strings since the maximum value may be present in multiple buckets