How to match every element in the array

I am trying to perform an elastic-search query that will return documents where "every" element of the nested collection has a match, not just one.
For example, I have a Driver object, with the List of cars, and each car has a color attribute.
Driver index:

curl --location --request PUT 'localhost:9200/driver' \ --header 'Content-Type: application/json' \ --data-raw '{ "mappings": { "properties": { "driver": { "type": "nested", "properties": { "name": { "type": "text" }, "car": { "type": "nested", "properties": { "color": { "type": "text" } } } } } } } }'

With the following data (Driver John with a green and red car, and driver Bob with two green cars):
curl --location --request PUT 'localhost:9200/driver/_doc/1' \ --header 'Content-Type: application/json' \ --data-raw '{ "driver": { "name": "John", "car": [ { "color": "red" }, { "color": "green" } ] } }'

curl --location --request PUT 'localhost:9200/driver/_doc/2' \ --header 'Content-Type: text/plain' \ --data-raw '{ "driver": { "name": "Bob", "car": [ { "color": "green" }, { "color": "green" } ] } }'

I want to find the driver that has ONLY green cars (i.e. Bob). I tried the following query, but it returns a driver that has at least one car that matches color:

        curl --location --request GET 'localhost:9200/driver/_search' \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "query": {
            "nested": {
                "path": "driver",
                "query": {
                    "nested": {
                        "path": "driver.car",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "match": {
                                            "driver.car.color": "green"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        }
    }'

This query returns every driver that has at least one green car. What is the fix?
Thank you.

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