Skip to content

Commit e28d3ad

Browse files
dmvasshonzakral
authored andcommitted
Add abilities for Bucket Sort Aggregation (Closes #893) (#894)
1 parent 8480b19 commit e28d3ad

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

elasticsearch_dsl/aggs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,6 @@ class StatsBucket(Pipeline):
291291

292292
class SumBucket(Pipeline):
293293
name = 'sum_bucket'
294+
295+
class BucketSort(Pipeline):
296+
name = 'bucket_sort'

test_elasticsearch_dsl/test_aggs.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,72 @@ def test_filters_correctly_identifies_the_hash():
183183
} == a.to_dict()
184184
assert a.filters.group_a == query.Q('term', group='a')
185185

186+
def test_bucket_sort_agg():
187+
bucket_sort_agg = aggs.BucketSort(
188+
sort=[{"total_sales": {"order": "desc"}}],
189+
size=3
190+
)
191+
assert bucket_sort_agg.to_dict() == {
192+
"bucket_sort": {
193+
"sort": [
194+
{"total_sales": {"order": "desc"}}
195+
],
196+
"size": 3
197+
}
198+
}
199+
200+
a = aggs.DateHistogram(field='date', interval='month')
201+
a.bucket('total_sales', 'sum', field='price')
202+
a.bucket(
203+
'sales_bucket_sort',
204+
'bucket_sort',
205+
sort=[{"total_sales": {"order": "desc"}}],
206+
size=3
207+
)
208+
assert {
209+
"date_histogram": {
210+
"field": "date",
211+
"interval": "month"
212+
},
213+
"aggs": {
214+
"total_sales": {
215+
"sum": {
216+
"field": "price"
217+
}
218+
},
219+
"sales_bucket_sort": {
220+
"bucket_sort": {
221+
"sort": [
222+
{"total_sales": {"order": "desc"}}
223+
],
224+
"size": 3
225+
}
226+
}
227+
}
228+
} == a.to_dict()
229+
230+
def test_bucket_sort_agg_only_trnunc():
231+
bucket_sort_agg = aggs.BucketSort(**{'from': 1, 'size': 1})
232+
assert bucket_sort_agg.to_dict() == {
233+
"bucket_sort": {
234+
"from": 1,
235+
"size": 1
236+
}
237+
}
238+
239+
a = aggs.DateHistogram(field='date', interval='month')
240+
a.bucket('bucket_truncate', 'bucket_sort', **{'from': 1, 'size': 1})
241+
assert {
242+
"date_histogram": {
243+
"field": "date",
244+
"interval": "month"
245+
},
246+
"aggs": {
247+
"bucket_truncate": {
248+
"bucket_sort": {
249+
"from": 1,
250+
"size": 1
251+
}
252+
}
253+
}
254+
} == a.to_dict()

0 commit comments

Comments
 (0)