Finding documents that have a specific number of values

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.

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