Elasticsearch filter document ids with same field values count

I need to return ids of documents that have the same myField value repeated [range] times. i.e. I have clientName field and want to find ids of all documents where clientName = 'John' but only if this name is found in 2-3 documents.

I have the following query


"query": {
    some filters that return matching document ids
"post_filter": {
    "bool": {
        "should": [
                "range": {
                    "myField": {
                        "lte": 3,
                        "gte": 2
        "minimum_should_match": 1

but it returns the range of myField values and not myField values count. I tried using aggregations but aggregation filters do not apply to the main query results. How do I modify my post_filter to get the results I require?

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