# Filtering on the result of an aggregation

**URL:** <https://discuss.elastic.co/t/filtering-on-the-result-of-an-aggregation/43202>\
**Category:** Elasticsearch\
**Created:** [March 2, 2016, 7:28am UTC](https://discuss.elastic.co/t/filtering-on-the-result-of-an-aggregation/43202 "2016-03-02T07:28:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aravindh\_S](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@Aravindh\_S](https://discuss.elastic.co/u/Aravindh_S)\
**Post date:** [March 2, 2016, 7:28am UTC](https://discuss.elastic.co/t/filtering-on-the-result-of-an-aggregation/43202/1 "2016-03-02T07:28:09Z")

</div>

Hi, I have a use case and I am not sure how to accomplish it. I am doing a terms aggregation and as a result of it, I am getting list of buckets with each bucket having the `doc_count` value. Is there anyway by which I can select a subset of those buckets which matches a certain condition based on the value of `doc_count`?

Example:  
Following is a sample mapping structure:  
`{ "mappings":{ "events":{ "properties":{ "name":{ "type":"string"}, "location":{ "type":"string"}, "user_id":{ "type":"string"} } } } }`  
My requirement is to get the list of users who have done a specific event atleast n number of times.  
I am doing a filter on the event name and then grouping by user\_id. This gives me the number of times each user has done this action. Now I want to filter this result where I want to select the users who have atleast 10 entires.  
Note: atleast in the above query is just an example. I need to support atmost and other forms. So Ideally I need to select the buckets based on a predicate.

---

<div class="post-metadata">

**Author:** ![ywelsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ywelsch/32/7751_2.png) [@ywelsch](https://discuss.elastic.co/u/ywelsch)\
**Post date:** [March 2, 2016, 11:22am UTC](https://discuss.elastic.co/t/filtering-on-the-result-of-an-aggregation/43202/2 "2016-03-02T11:22:50Z")

</div>

The following might help: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html)

---

<div class="post-metadata">

**Author:** ![Aravindh\_S](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@Aravindh\_S](https://discuss.elastic.co/u/Aravindh_S)\
**Post date:** [March 3, 2016, 8:46am UTC](https://discuss.elastic.co/t/filtering-on-the-result-of-an-aggregation/43202/3 "2016-03-03T08:46:42Z")

</div>

ywelsch: Thanks. That helped.

---

<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, 11:11pm UTC](https://discuss.elastic.co/t/filtering-on-the-result-of-an-aggregation/43202/4 "2017-07-05T23:11:45Z")

</div>


