# Finding documents that have a specific number of values

**URL:** <https://discuss.elastic.co/t/finding-documents-that-have-a-specific-number-of-values/265380>\
**Category:** Elasticsearch\
**Created:** [February 24, 2021, 5:12pm UTC](https://discuss.elastic.co/t/finding-documents-that-have-a-specific-number-of-values/265380 "2021-02-24T17:12:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Bourne](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kevin_bourne/32/53053_2.png) [@Kevin\_Bourne](https://discuss.elastic.co/u/Kevin_Bourne)\
**Post date:** [February 24, 2021, 5:12pm UTC](https://discuss.elastic.co/t/finding-documents-that-have-a-specific-number-of-values/265380/1 "2021-02-24T17:12:17Z")

</div>

Let's say I have a mapping like this where there is a nested field within another nested field:

```
"myIndex": {
    "properties": {
        "id": {
            "type": "keyword"
        },
        "stuff": {
            "type": "nested",
            "properties": {
                "pairs": {
                    "type": "nested",
                    "properties": {
                        "key": {
                            "type": "keyword"
                        },
                        "value": {
                             "type": "text"
                        }
                  }
            }
        }
    }
}

```

Is there a way to select documents that have a specific number of values in the pairs nested field above? So with this data that has 2 documents:

```
{
    "id": "5",
    "stuff": [
        {
            "pairs": [
                {
                    "key": "key1",
                    "value": "someValue"
                }
                {
                    "key": "key2",
                    "value": "someOtherValue"
                } ]
        }
    ]
},
{
    "id": "6",
    "stuff": [
        {
            "pairs": [
                {
                    "key": "key1",
                    "value": "someValue"
                },
                {
                    "key": "key2",
                    "value": "someValue"
                }
            ]
        }
    ]
}

```

I want a query that returns documents that has the value "someValue" in exactly 2 items within pairs? Or that has at least 2? In this case document id "6" would only be returned.

---

<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:** [March 24, 2021, 5:12pm UTC](https://discuss.elastic.co/t/finding-documents-that-have-a-specific-number-of-values/265380/2 "2021-03-24T17:12:46Z")

</div>

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