Skip to content

Commit 0382e6a

Browse files
Add docs
1 parent ba4ff0e commit 0382e6a

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

docs/reference/aggregations/pipeline.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,5 @@ include::pipeline/movavg-aggregation.asciidoc[]
234234
include::pipeline/cumulative-sum-aggregation.asciidoc[]
235235
include::pipeline/bucket-script-aggregation.asciidoc[]
236236
include::pipeline/bucket-selector-aggregation.asciidoc[]
237+
include::pipeline/bucket-sort-aggregation.asciidoc[]
237238
include::pipeline/serial-diff-aggregation.asciidoc[]
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
[[search-aggregations-pipeline-bucket-sort-aggregation]]
2+
=== Bucket Sort Aggregation
3+
4+
A parent pipeline aggregation which sorts the buckets of its parent multi-bucket aggregation.
5+
Zero or more sort fields may be specified together with the corresponding sort order.
6+
Each bucket may be sorted based on its `_key`, `_count` or its sub-aggregations.
7+
In addition, parameters `from` and `size` may be set in order to truncate the result buckets.
8+
9+
NOTE: The `bucket_sort` aggregation, like all pipeline aggregations, is executed after all other sibling aggregations.
10+
This means the sorting only applies to whatever buckets are already returned from the parent aggregation. For example,
11+
if the parent aggregation is `terms` and its `size` is set to `10`, the `bucket_sort` will only sort over those 10
12+
returned term buckets.
13+
14+
==== Syntax
15+
16+
A `bucket_sort` aggregation looks like this in isolation:
17+
18+
[source,js]
19+
--------------------------------------------------
20+
{
21+
"bucket_sort": {
22+
"sort": [
23+
{"sort_field_1": {"order": "asc"},<1>
24+
{"sort_field_2": {"order": "desc"},
25+
"sort_field_3"
26+
],
27+
"from": 1,
28+
"size": 3
29+
}
30+
}
31+
--------------------------------------------------
32+
// NOTCONSOLE
33+
<1> Here, `sort_field_1` is the bucket path to the variable to be used as the primary sort and its order
34+
is ascending.
35+
36+
.`bucket_sort` Parameters
37+
|===
38+
|Parameter Name |Description |Required |Default Value
39+
|`sort` |The list of fields to sort on. See <<search-request-sort,`sort`>> for more details. |Optional |
40+
|`from` |Buckets in positions prior to the set value will be truncated. |Optional | `0`
41+
|`size` |The number of buckets to return. Defaults to all buckets of the parent aggregation. |Optional |
42+
|`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
43+
details)|Optional |`skip`
44+
|===
45+
46+
The following snippet returns the buckets corresponding to the 3 months with the highest total sales in descending order:
47+
48+
[source,js]
49+
--------------------------------------------------
50+
POST /sales/_search
51+
{
52+
"size": 0,
53+
"aggs" : {
54+
"sales_per_month" : {
55+
"date_histogram" : {
56+
"field" : "date",
57+
"interval" : "month"
58+
},
59+
"aggs": {
60+
"total_sales": {
61+
"sum": {
62+
"field": "price"
63+
}
64+
},
65+
"sales_bucket_sort": {
66+
"bucket_sort": {
67+
"sort": [
68+
{"total_sales": {"order": "desc"}}<1>
69+
],
70+
"size": 3<2>
71+
}
72+
}
73+
}
74+
}
75+
}
76+
}
77+
--------------------------------------------------
78+
// CONSOLE
79+
// TEST[setup:sales]
80+
<1> `sort` is set to use the values of `total_sales` in descending order
81+
<2> `size` is set to `3` meaning only the top 3 months in `total_sales` will be returned
82+
83+
And the following may be the response:
84+
85+
[source,js]
86+
--------------------------------------------------
87+
{
88+
"took": 82,
89+
"timed_out": false,
90+
"_shards": ...,
91+
"hits": ...,
92+
"aggregations": {
93+
"sales_per_month": {
94+
"buckets": [
95+
{
96+
"key_as_string": "2015/01/01 00:00:00",
97+
"key": 1420070400000,
98+
"doc_count": 3,
99+
"total_sales": {
100+
"value": 550.0
101+
}
102+
},
103+
{
104+
"key_as_string": "2015/03/01 00:00:00",
105+
"key": 1425168000000,
106+
"doc_count": 2,
107+
"total_sales": {
108+
"value": 375.0
109+
},
110+
},
111+
{
112+
"key_as_string": "2015/02/01 00:00:00",
113+
"key": 1422748800000,
114+
"doc_count": 2,
115+
"total_sales": {
116+
"value": 60.0
117+
},
118+
}
119+
]
120+
}
121+
}
122+
}
123+
--------------------------------------------------
124+
// TESTRESPONSE[s/"took": 82/"took": $body.took/]
125+
// TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
126+
// TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]

0 commit comments

Comments
 (0)