# Sorting "children" aggregations

**URL:** https://discuss.elastic.co/t/sorting-children-aggregations/89944
**Category:** Elasticsearch
**Created:** [June 19, 2017, 1:55pm UTC](https://discuss.elastic.co/t/sorting-children-aggregations/89944 "2017-06-19T13:55:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![blaineparker](https://avatars.discourse-cdn.com/v4/letter/b/8dc957/32.png) [@blaineparker](https://discuss.elastic.co/u/blaineparker)
#### Post date: [June 19, 2017, 1:55pm UTC](https://discuss.elastic.co/t/sorting-children-aggregations/89944/1 "2017-06-19T13:55:10Z")

</div>

I am looking to aggregate children based on parent fields, but I want to filter on both children and parent fields. The problem is when I do this, I get results where parents match the criteria but no children match the criteria and because the parents which match the criteria have many children, these entries are at the top of the list.

As a result, I want to order the "children" aggregation based on the number of children which match the criteria and order the parent aggregation based on that ordering. Can I achieve this?

Below is the query structure and problematic results

{  
"aggregations" : {  
“parentFilter" : {  
"filter" : {  
"bool" : {  
"must" : {  
"match\_all" : { }  
}  
}  
},  
"aggregations" : {  
“parentCount" : {  
"terms" : {  
"field" : “parentField",  
"size" : 10,  
"order" : {  
“childCount" : "desc"  
}  
},  
"aggregations" : {  
“childCount" : {  
"children" : {  
"type" : “childType"  
},  
"aggregations" : {  
“filteredChildren" : {  
"filter" : {  
"bool" : {  
"must" : {  
"terms" : {  
“childField" : [“childCriteria"]  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}

Result:  
"aggregations": {  
“parentFilter": {  
"doc\_count": 105,  
“parentCount": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"key": “field2",  
"doc\_count": 6,  
“childCount": {  
"doc\_count": 50,  
"filteredChildren": {  
"doc\_count": 0  
}  
}  
},  
{  
"key": “field0",  
"doc\_count": 7,  
"childCount": {  
"doc\_count": 30,  
"filteredChildren": {  
"doc\_count": 5  
}  
}  
},  
{  
"key": “field1",  
"doc\_count": 7,  
“childCount": {  
"doc\_count": 30,  
"filteredChildren": {  
"doc\_count": 4  
}  
}  
}  
]  
}  
}  
}

Desired Result:  
"aggregations": {  
“parentFilter": {  
"doc\_count": 105,  
“parentCount": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"key": “field0",  
"doc\_count": 7,  
"childCount": {  
"doc\_count": 30,  
"filteredChildren": {  
"doc\_count": 5  
}  
}  
},  
{  
"key": “field1",  
"doc\_count": 7,  
“childCount": {  
"doc\_count": 30,  
"filteredChildren": {  
"doc\_count": 4  
}  
},  
{  
"key": “field2",  
"doc\_count": 6,  
“childCount": {  
"doc\_count": 50,  
"filteredChildren": {  
"doc\_count": 0  
}  
}  
}  
}  
]  
}  
}  
}

Is there anyway to achieve this ordering?

---

<div class="post-metadata">

### Author: ![blaineparker](https://avatars.discourse-cdn.com/v4/letter/b/8dc957/32.png) [@blaineparker](https://discuss.elastic.co/u/blaineparker)
#### Post date: [June 19, 2017, 3:00pm UTC](https://discuss.elastic.co/t/sorting-children-aggregations/89944/2 "2017-06-19T15:00:03Z")

</div>

Hi All. I have sorted it out.

To the current ordering - I added an aggregation path.

so "order" : {"childCount" : "desc"} becomes "order" : {"childCount\>childFilter" : "desc"}

---

<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: [July 17, 2017, 3:00pm UTC](https://discuss.elastic.co/t/sorting-children-aggregations/89944/3 "2017-07-17T15:00:15Z")

</div>

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