# Aggregating by count of another aggregation?

**URL:** <https://discuss.elastic.co/t/aggregating-by-count-of-another-aggregation/56682>\
**Category:** Elasticsearch\
**Created:** [July 28, 2016, 11:01pm UTC](https://discuss.elastic.co/t/aggregating-by-count-of-another-aggregation/56682 "2016-07-28T23:01:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrey81inmd](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@andrey81inmd](https://discuss.elastic.co/u/andrey81inmd)\
**Post date:** [July 28, 2016, 11:01pm UTC](https://discuss.elastic.co/t/aggregating-by-count-of-another-aggregation/56682/1 "2016-07-28T23:01:29Z")

</div>

I'm trying to figure out how to group by a count coming out of another aggregation. Let's say I have data that has events about users, with user\_id as one of the fields. For example, it could look like this  
user\_id = user1  
user\_id = user1  
user\_id = user2  
user\_id = user3  
user\_id = user3  
user\_id = user3  
user\_id = user4  
user\_id = user5

I want the count of how many user\_id's occur exactly 1, 2, 3, ... etc times in the list. In this case, the result would be:  
1=\>3 (user2, user4, user5)  
2=\>1 (user1)  
3=\>1 (user3)

I just need the counts, not who those users are.

I can get to the point of counting the number of occurrences of each user\_id by using term aggregation. From there, I tried another term aggregation on doc\_count or \_count of the first aggregation, but it doesn't seem to work. I was not able to find clear documentation on the syntax for that, so that may be my problem.

Please let me know if anyone has a solution.

Thanks!  
Andrey

---

<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:31pm UTC](https://discuss.elastic.co/t/aggregating-by-count-of-another-aggregation/56682/2 "2017-07-05T22:31:55Z")

</div>


