Find documents in an aggregation of a certain size

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?

See if this is helpful Bucket Selector Aggregation

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

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>


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.

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