1
0

bucket-sort-aggregation.asciidoc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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` Parameters
  31. |===
  32. |Parameter Name |Description |Required |Default Value
  33. |`sort` |The list of fields to sort on. See <<search-request-sort,`sort`>> for more details. |Optional |
  34. |`from` |Buckets in positions prior to the set value will be truncated. |Optional | `0`
  35. |`size` |The number of buckets to return. Defaults to all buckets of the parent aggregation. |Optional |
  36. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  37. details)|Optional |`skip`
  38. |===
  39. The following snippet returns the buckets corresponding to the 3 months with the highest total sales in descending order:
  40. [source,js]
  41. --------------------------------------------------
  42. POST /sales/_search
  43. {
  44. "size": 0,
  45. "aggs" : {
  46. "sales_per_month" : {
  47. "date_histogram" : {
  48. "field" : "date",
  49. "interval" : "month"
  50. },
  51. "aggs": {
  52. "total_sales": {
  53. "sum": {
  54. "field": "price"
  55. }
  56. },
  57. "sales_bucket_sort": {
  58. "bucket_sort": {
  59. "sort": [
  60. {"total_sales": {"order": "desc"}}<1>
  61. ],
  62. "size": 3<2>
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. --------------------------------------------------
  70. // CONSOLE
  71. // TEST[setup:sales]
  72. <1> `sort` is set to use the values of `total_sales` in descending order
  73. <2> `size` is set to `3` meaning only the top 3 months in `total_sales` will be returned
  74. And the following may be the response:
  75. [source,js]
  76. --------------------------------------------------
  77. {
  78. "took": 82,
  79. "timed_out": false,
  80. "_shards": ...,
  81. "hits": ...,
  82. "aggregations": {
  83. "sales_per_month": {
  84. "buckets": [
  85. {
  86. "key_as_string": "2015/01/01 00:00:00",
  87. "key": 1420070400000,
  88. "doc_count": 3,
  89. "total_sales": {
  90. "value": 550.0
  91. }
  92. },
  93. {
  94. "key_as_string": "2015/03/01 00:00:00",
  95. "key": 1425168000000,
  96. "doc_count": 2,
  97. "total_sales": {
  98. "value": 375.0
  99. },
  100. },
  101. {
  102. "key_as_string": "2015/02/01 00:00:00",
  103. "key": 1422748800000,
  104. "doc_count": 2,
  105. "total_sales": {
  106. "value": 60.0
  107. },
  108. }
  109. ]
  110. }
  111. }
  112. }
  113. --------------------------------------------------
  114. // TESTRESPONSE[s/"took": 82/"took": $body.took/]
  115. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  116. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  117. ==== Truncating without sorting
  118. It is also possible to use this aggregation in order to truncate the result buckets
  119. without doing any sorting. To do so, just use the `from` and/or `size` parameters
  120. without specifying `sort`.
  121. The following example simply truncates the result so that only the second bucket is returned:
  122. [source,js]
  123. --------------------------------------------------
  124. POST /sales/_search
  125. {
  126. "size": 0,
  127. "aggs" : {
  128. "sales_per_month" : {
  129. "date_histogram" : {
  130. "field" : "date",
  131. "interval" : "month"
  132. },
  133. "aggs": {
  134. "bucket_truncate": {
  135. "bucket_sort": {
  136. "from": 1,
  137. "size": 1
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }
  144. --------------------------------------------------
  145. // CONSOLE
  146. // TEST[setup:sales]
  147. Response:
  148. [source,js]
  149. --------------------------------------------------
  150. {
  151. "took": 11,
  152. "timed_out": false,
  153. "_shards": ...,
  154. "hits": ...,
  155. "aggregations": {
  156. "sales_per_month": {
  157. "buckets": [
  158. {
  159. "key_as_string": "2015/02/01 00:00:00",
  160. "key": 1422748800000,
  161. "doc_count": 2
  162. }
  163. ]
  164. }
  165. }
  166. }
  167. --------------------------------------------------
  168. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  169. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  170. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]