# Sorting aggregation buckets problem

**URL:** <https://discuss.elastic.co/t/sorting-aggregation-buckets-problem/173488>\
**Category:** Elasticsearch\
**Created:** [March 22, 2019, 12:08pm UTC](https://discuss.elastic.co/t/sorting-aggregation-buckets-problem/173488 "2019-03-22T12:08:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![p.j.keukens](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@p.j.keukens](https://discuss.elastic.co/u/p.j.keukens)\
**Post date:** [March 22, 2019, 12:08pm UTC](https://discuss.elastic.co/t/sorting-aggregation-buckets-problem/173488/1 "2019-03-22T12:08:28Z")

</div>

Hi All,

I have this query and I would like to bucketed results to be sorted on the managers age, can anybody help me on how to do this?

This is (a part of) my json request, the full request has multiple aggregates on ManagerJsonString:  
{  
"size": 0,  
"query": {  
"bool": {  
"must": [  
{  
"bool": {  
"must": [  
{  
"terms": {  
"ManagerId": [  
115,  
18,  
123  
]  
}  
}  
]  
}  
}  
]  
}  
},  
"aggs": {  
"ManagerJsonString": {  
"filter": {  
"multi\_match": {  
"type": "phrase\_prefix",  
"query": "pat",  
"fields": [  
"Manager.nl\*.name^2",  
"Manager.default.name"  
]  
}  
},  
"aggs": {  
"aggr1": {  
"terms": {  
"field": "ManagerJsonString",  
}  
}  
}  
}  
}  
}

which results in this:  
{  
"took": 2,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 3,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
"total": 257,  
"max\_score": 0,  
"hits":   
},  
"aggregations": {  
"ManagerJsonString": {  
"doc\_count": 2,  
"aggr1": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [{  
"key": "Manager;{"default":{"name":"Patrick","age":35},"nl":{"name":"Patrick","age":35}}",  
"doc\_count": 1  
},  
{  
"key": "Manager;{"default":{"name":"Patricia","age":21},"nl":{"name":"Patricia","age":21}}",  
"doc\_count": 1  
}]  
}  
}  
}  
}

Now the manager also has a property age as can be seen in the ManagerJsonString (which is an accumulation of some Manager fields) so how can I sort the buckets on the age of the manager?

I tried adding a sort on the aggr1 terms and could sort on the key and doc count but not on the age of the manager

Help would be appreciated I'm kind of stuck on this one.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 19, 2019, 12:08pm UTC](https://discuss.elastic.co/t/sorting-aggregation-buckets-problem/173488/2 "2019-04-19T12:08:28Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
