# How do I use doc\_count in an aggregations range query in ElasticSearch 1.0?

**URL:** <https://discuss.elastic.co/t/how-do-i-use-doc-count-in-an-aggregations-range-query-in-elasticsearch-1-0/16304>\
**Category:** Elasticsearch\
**Created:** [March 11, 2014, 11:30pm UTC](https://discuss.elastic.co/t/how-do-i-use-doc-count-in-an-aggregations-range-query-in-elasticsearch-1-0/16304 "2014-03-11T23:30:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Axel\_Dahl](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@Axel\_Dahl](https://discuss.elastic.co/u/Axel_Dahl)\
**Post date:** [March 11, 2014, 11:30pm UTC](https://discuss.elastic.co/t/how-do-i-use-doc-count-in-an-aggregations-range-query-in-elasticsearch-1-0/16304/1 "2014-03-11T23:30:50Z")

</div>

I have a bunch of user generated events in my ES cluster. Each event  
contains the user's UUID.

I'm trying to write a query that buckets users into low, medium and high  
activity based on the number of events each user generates.

I'm using this query to get the number of events generated by each user:

{  
"aggs" : {  
"users" : {  
"terms" : { "field" : "user\_id.raw" }  
}  
}  
}

This works fine, but I need to further bucket the results into a range  
query using the previous results "doc\_count", so that I can sort each user  
into a low, med, high activity bucket. The buckets are fixed (at least for  
now). so low=0-5, med=5-10 high=10+

I tried a bunch of ways to access the doc\_count field using a  
sub-aggregation but never manage to get it work. I figured this would be a  
fairly common use case, but can't seem to crack it, so any help would be  
much appreciated.

--  
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/d75e8326-5eec-452d-8221-ab38c5f86604%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d75e8326-5eec-452d-8221-ab38c5f86604%40googlegroups.com).  
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:44am UTC](https://discuss.elastic.co/t/how-do-i-use-doc-count-in-an-aggregations-range-query-in-elasticsearch-1-0/16304/2 "2017-07-06T01:44:03Z")

</div>


