# Find documents in an aggregation of a certain size

**URL:** <https://discuss.elastic.co/t/find-documents-in-an-aggregation-of-a-certain-size/126258>\
**Category:** Elasticsearch\
**Created:** [March 30, 2018, 4:50pm UTC](https://discuss.elastic.co/t/find-documents-in-an-aggregation-of-a-certain-size/126258 "2018-03-30T16:50:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![esUser2](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@esUser2](https://discuss.elastic.co/u/esUser2)\
**Post date:** [March 30, 2018, 4:50pm UTC](https://discuss.elastic.co/t/find-documents-in-an-aggregation-of-a-certain-size/126258/1 "2018-03-30T16:50:39Z")

</div>

I want to do a query that returns documents that belong to an aggregation with a specific size.

For example, if I have a field called "category" and do a term aggregation I will get buckets. I want to return documents that belong to an aggregation that have a bucket size between a specific range. How would I do that?

---

<div class="post-metadata">

**Author:** ![Sravanthi\_N\_S\_CH](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sravanthi_n_s_ch/32/50473_2.png) [@Sravanthi\_N\_S\_CH](https://discuss.elastic.co/u/Sravanthi_N_S_CH)\
**Post date:** [March 31, 2018, 6:07am UTC](https://discuss.elastic.co/t/find-documents-in-an-aggregation-of-a-certain-size/126258/2 "2018-03-31T06:07:30Z")

</div>

See if this is helpful [Bucket Selector Aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/master/search-aggregations-pipeline-bucket-selector-aggregation.html)

---

<div class="post-metadata">

**Author:** ![esUser2](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@esUser2](https://discuss.elastic.co/u/esUser2)\
**Post date:** [April 3, 2018, 5:44pm UTC](https://discuss.elastic.co/t/find-documents-in-an-aggregation-of-a-certain-size/126258/3 "2018-04-03T17:44:31Z")

</div>

Yes, that worked for me but I don't know how to filter documents using the aggregator.

---

<div class="post-metadata">

**Author:** ![sylvain.mazet](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@sylvain.mazet](https://discuss.elastic.co/u/sylvain.mazet)\
**Post date:** [April 3, 2018, 10:02pm UTC](https://discuss.elastic.co/t/find-documents-in-an-aggregation-of-a-certain-size/126258/4 "2018-04-03T22:02:29Z")

</div>

I guess you mean you want the documents inside the buckets that have a certain size range.  
With the bucket selector and the "\_count" field, you could get the buckets that have the wanted size:  
{ "query" : { "match\_all" : {} },  
"aggs": { "categories": {  
"terms": {"field": "category", "size":20},  
"aggs": { "my\_bucket\_selection": { "bucket\_selector" : {"buckets\_path" : {"count": "\_count"},  
"script": "params.count\>10000 &&params.count\<100000"}  
}  
}  
}  
}  
}

After that, one way I know of for retrieving the documents of a specific bucket is to do a query: query the document that have "category"=\<bucket\_key\>

HTH

---

<div class="post-metadata">

**Author:** ![esUser2](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@esUser2](https://discuss.elastic.co/u/esUser2)\
**Post date:** [April 4, 2018, 5:37pm UTC](https://discuss.elastic.co/t/find-documents-in-an-aggregation-of-a-certain-size/126258/5 "2018-04-04T17:37:49Z")

</div>

Ok, so the only way to do this is in two queries?

The other issue is I have thousands of buckets so I'm not sure how I would page results doing this in two queries.

---

<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:** [May 2, 2018, 5:38pm UTC](https://discuss.elastic.co/t/find-documents-in-an-aggregation-of-a-certain-size/126258/6 "2018-05-02T17:38:18Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
