bucket-sort-aggregation.asciidoc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. [[search-aggregations-pipeline-bucket-sort-aggregation]]
  2. === Bucket Sort Aggregation
  3. A parent pipeline aggregation which sorts the buckets of its parent multi-bucket aggregation.
  4. Zero or more sort fields may be specified together with the corresponding sort order.
  5. Each bucket may be sorted based on its `_key`, `_count` or its sub-aggregations.
  6. In addition, parameters `from` and `size` may be set in order to truncate the result buckets.
  7. NOTE: The `bucket_sort` aggregation, like all pipeline aggregations, is executed after all other non-pipeline aggregations.
  8. This means the sorting only applies to whatever buckets are already returned from the parent aggregation. For example,
  9. if the parent aggregation is `terms` and its `size` is set to `10`, the `bucket_sort` will only sort over those 10
  10. returned term buckets.
  11. ==== Syntax
  12. A `bucket_sort` aggregation looks like this in isolation:
  13. [source,js]
  14. --------------------------------------------------
  15. {
  16. "bucket_sort": {
  17. "sort": [
  18. {"sort_field_1": {"order": "asc"}},<1>
  19. {"sort_field_2": {"order": "desc"}},
  20. "sort_field_3"
  21. ],
  22. "from": 1,
  23. "size": 3
  24. }
  25. }
  26. --------------------------------------------------
  27. // NOTCONSOLE
  28. <1> Here, `sort_field_1` is the bucket path to the variable to be used as the primary sort and its order
  29. is ascending.
  30. [[bucket-sort-params]]
  31. .`bucket_sort` Parameters
  32. [options="header"]
  33. |===
  34. |Parameter Name |Description |Required |Default Value
  35. |`sort` |The list of fields to sort on. See <<search-request-sort,`sort`>> for more details. |Optional |
  36. |`from` |Buckets in positions prior to the set value will be truncated. |Optional | `0`
  37. |`size` |The number of buckets to return. Defaults to all buckets of the parent aggregation. |Optional |
  38. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  39. details)|Optional |`skip`
  40. |===
  41. The following snippet returns the buckets corresponding to the 3 months with the highest total sales in descending order:
  42. [source,js]
  43. --------------------------------------------------
  44. POST /sales/_search
  45. {
  46. "size": 0,
  47. "aggs" : {
  48. "sales_per_month" : {
  49. "date_histogram" : {
  50. "field" : "date",
  51. "calendar_interval" : "month"
  52. },
  53. "aggs": {
  54. "total_sales": {
  55. "sum": {
  56. "field": "price"
  57. }
  58. },
  59. "sales_bucket_sort": {
  60. "bucket_sort": {
  61. "sort": [
  62. {"total_sales": {"order": "desc"}}<1>
  63. ],
  64. "size": 3<2>
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. --------------------------------------------------
  72. // CONSOLE
  73. // TEST[setup:sales]
  74. <1> `sort` is set to use the values of `total_sales` in descending order
  75. <2> `size` is set to `3` meaning only the top 3 months in `total_sales` will be returned
  76. And the following may be the response:
  77. [source,js]
  78. --------------------------------------------------
  79. {
  80. "took": 82,
  81. "timed_out": false,
  82. "_shards": ...,
  83. "hits": ...,
  84. "aggregations": {
  85. "sales_per_month": {
  86. "buckets": [
  87. {
  88. "key_as_string": "2015/01/01 00:00:00",
  89. "key": 1420070400000,
  90. "doc_count": 3,
  91. "total_sales": {
  92. "value": 550.0
  93. }
  94. },
  95. {
  96. "key_as_string": "2015/03/01 00:00:00",
  97. "key": 1425168000000,
  98. "doc_count": 2,
  99. "total_sales": {
  100. "value": 375.0
  101. },
  102. },
  103. {
  104. "key_as_string": "2015/02/01 00:00:00",
  105. "key": 1422748800000,
  106. "doc_count": 2,
  107. "total_sales": {
  108. "value": 60.0
  109. },
  110. }
  111. ]
  112. }
  113. }
  114. }
  115. --------------------------------------------------
  116. // TESTRESPONSE[s/"took": 82/"took": $body.took/]
  117. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  118. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  119. ==== Truncating without sorting
  120. It is also possible to use this aggregation in order to truncate the result buckets
  121. without doing any sorting. To do so, just use the `from` and/or `size` parameters
  122. without specifying `sort`.
  123. The following example simply truncates the result so that only the second bucket is returned:
  124. [source,js]
  125. --------------------------------------------------
  126. POST /sales/_search
  127. {
  128. "size": 0,
  129. "aggs" : {
  130. "sales_per_month" : {
  131. "date_histogram" : {
  132. "field" : "date",
  133. "calendar_interval" : "month"
  134. },
  135. "aggs": {
  136. "bucket_truncate": {
  137. "bucket_sort": {
  138. "from": 1,
  139. "size": 1
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. --------------------------------------------------
  147. // CONSOLE
  148. // TEST[setup:sales]
  149. Response:
  150. [source,js]
  151. --------------------------------------------------
  152. {
  153. "took": 11,
  154. "timed_out": false,
  155. "_shards": ...,
  156. "hits": ...,
  157. "aggregations": {
  158. "sales_per_month": {
  159. "buckets": [
  160. {
  161. "key_as_string": "2015/02/01 00:00:00",
  162. "key": 1422748800000,
  163. "doc_count": 2
  164. }
  165. ]
  166. }
  167. }
  168. }
  169. --------------------------------------------------
  170. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  171. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  172. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]