max-bucket-aggregation.asciidoc 3.3 KB

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