# How to use first aggregations result in second aggreagation

**URL:** https://discuss.elastic.co/t/how-to-use-first-aggregations-result-in-second-aggreagation/18364
**Category:** Elasticsearch
**Created:** [June 27, 2014, 8:12am UTC](https://discuss.elastic.co/t/how-to-use-first-aggregations-result-in-second-aggreagation/18364 "2014-06-27T08:12:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Swarna\_M](https://avatars.discourse-cdn.com/v4/letter/s/8e7dd6/32.png) [@Swarna\_M](https://discuss.elastic.co/u/Swarna_M)
#### Post date: [June 27, 2014, 8:12am UTC](https://discuss.elastic.co/t/how-to-use-first-aggregations-result-in-second-aggreagation/18364/1 "2014-06-27T08:12:01Z")

</div>

Hello,

I ran a date histogram aggregation on my time series data set and got  
results like

"aggregations": {  
"hitsperfiveminute": {  
"buckets": [  
{  
"key\_as\_string": "09/Dec/2013:16:50:00",  
"key": 1386607800000,  
"doc\_count": 23620  
},  
{  
"key\_as\_string": "09/Dec/2013:16:55:00",  
"key": 1386608100000,  
"doc\_count": 2377  
},  
{  
"key\_as\_string": "09/Dec/2013:17:00:00",  
"key": 1386608400000,  
"doc\_count": 3995  
}]}}

Now, I intend to put a range aggregation on the doc count in each bucket so  
as to finally get  
"aggregations": {  
"hitsrange": {  
"buckets": [  
{  
"from": 0,  
"to": 10000,  
"doc\_count": 2  
},  
{  
"from": 10000,  
"to": 50000,  
"doc\_count": 1  
}  
]  
}  
}

Please help me get this operation done in a single query.

Thanks and Regards  
Swarna

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/c728378e-2b46-4a9f-bac6-560bb0db5d60%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c728378e-2b46-4a9f-bac6-560bb0db5d60%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [June 27, 2014, 8:19am UTC](https://discuss.elastic.co/t/how-to-use-first-aggregations-result-in-second-aggreagation/18364/2 "2014-06-27T08:19:30Z")

</div>

You have to add a new aggs inside the previous agg.

Something like (but with another dataset then yours):

GET person/\_search  
{  
"size": 0,  
"aggs": {  
"by\_year": {  
"date\_histogram": {  
"field": "dateOfBirth",  
"interval": "year"  
},  
"aggs": {  
"by\_gender": {  
"terms": {  
"field": "gender",  
"size": 10  
},  
"aggs": {  
"children": {  
"avg": {  
"field": "children"  
}  
}  
}  
}  
}  
}  
}  
}

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr

Le 27 juin 2014 à 10:12:06, Swarna M ([mvswarna21@gmail.com](mailto:mvswarna21@gmail.com)) a écrit:

Hello,

I ran a date histogram aggregation on my time series data set and got results like

"aggregations": {  
"hitsperfiveminute": {  
"buckets": [  
{  
"key\_as\_string": "09/Dec/2013:16:50:00",  
"key": 1386607800000,  
"doc\_count": 23620  
},  
{  
"key\_as\_string": "09/Dec/2013:16:55:00",  
"key": 1386608100000,  
"doc\_count": 2377  
},  
{  
"key\_as\_string": "09/Dec/2013:17:00:00",  
"key": 1386608400000,  
"doc\_count": 3995  
}]}}

Now, I intend to put a range aggregation on the doc count in each bucket so as to finally get  
"aggregations": {  
"hitsrange": {  
"buckets": [  
{  
"from": 0,  
"to": 10000,  
"doc\_count": 2  
},  
{  
"from": 10000,  
"to": 50000,  
"doc\_count": 1  
}  
]  
}  
}

Please help me get this operation done in a single query.

## Thanks and Regards Swarna

You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/c728378e-2b46-4a9f-bac6-560bb0db5d60%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c728378e-2b46-4a9f-bac6-560bb0db5d60%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.53ad2912.46e87ccd.b75f%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.53ad2912.46e87ccd.b75f%40MacBook-Air-de-David.local).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:19am UTC](https://discuss.elastic.co/t/how-to-use-first-aggregations-result-in-second-aggreagation/18364/3 "2017-07-06T01:19:21Z")

</div>


