# Elasticsearch filter document ids with same field values count

**URL:** <https://discuss.elastic.co/t/elasticsearch-filter-document-ids-with-same-field-values-count/223635>\
**Category:** Elasticsearch\
**Created:** [March 14, 2020, 8:05pm UTC](https://discuss.elastic.co/t/elasticsearch-filter-document-ids-with-same-field-values-count/223635 "2020-03-14T20:05:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iwave\_Nice](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iwave_nice/32/54609_2.png) [@Iwave\_Nice](https://discuss.elastic.co/u/Iwave_Nice)\
**Post date:** [March 14, 2020, 8:05pm UTC](https://discuss.elastic.co/t/elasticsearch-filter-document-ids-with-same-field-values-count/223635/1 "2020-03-14T20:05:58Z")

</div>

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

```
_search?filter_path=hits.total,hits.hits._id

"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?

---

<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:** [April 11, 2020, 8:06pm UTC](https://discuss.elastic.co/t/elasticsearch-filter-document-ids-with-same-field-values-count/223635/2 "2020-04-11T20:06:00Z")

</div>

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