# Elasticsearch query to aggregate results of buckets

**URL:** https://discuss.elastic.co/t/elasticsearch-query-to-aggregate-results-of-buckets/60213
**Category:** Elasticsearch
**Created:** [September 10, 2016, 10:07am UTC](https://discuss.elastic.co/t/elasticsearch-query-to-aggregate-results-of-buckets/60213 "2016-09-10T10:07:53Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![manaswini](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@manaswini](https://discuss.elastic.co/u/manaswini)
#### Post date: [September 10, 2016, 10:07am UTC](https://discuss.elastic.co/t/elasticsearch-query-to-aggregate-results-of-buckets/60213/1 "2016-09-10T10:07:53Z")

</div>

Hi ,  
here is my query:  
GET /presence\_client/client\_stats/\_search  
{  
"size" : 0,  
"aggs": {  
"macs": {  
"terms": {  
"field": "\_parent"  
},  
"aggs": {  
"by\_store": {  
"terms": {  
"field": "store\_id"  
}  
}  
}  
}  
}  
}

and the result is

{  
"took": 5,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"failed": 0  
},  
"hits": {  
"total": 5,  
"max\_score": 0,  
"hits": []  
},  
"aggregations": {  
"macs": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"key": "mac\_2",  
"doc\_count": 3,  
"by\_store": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"key": 101,  
"doc\_count": 2  
},  
{  
"key": 100,  
"doc\_count": 1  
}  
]  
}  
},  
{  
"key": "mac\_1",  
"doc\_count": 2,  
"by\_store": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"key": 100,  
"doc\_count": 2  
}  
]  
}  
}  
]  
}  
}  
}

From here i want to consume the result of by\_store aggregation to count how many macs, had a doc\_count in the range

[0- 5]  
[5 - 10]  
[10 -20] etc ..

Basically i want to do a range operation on the doc\_count returned by each bucket of "by\_store" aggregation.

Thanks in Advance!  
Manaswini

---

<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 5, 2017, 10:21pm UTC](https://discuss.elastic.co/t/elasticsearch-query-to-aggregate-results-of-buckets/60213/2 "2017-07-05T22:21:24Z")

</div>


