# How to match every element in the array

**URL:** <https://discuss.elastic.co/t/how-to-match-every-element-in-the-array/259536>\
**Category:** Elasticsearch\
**Created:** [December 23, 2020, 10:48pm UTC](https://discuss.elastic.co/t/how-to-match-every-element-in-the-array/259536 "2020-12-23T22:48:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yaddes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yaddes/32/81342_2.png) [@yaddes](https://discuss.elastic.co/u/yaddes)\
**Post date:** [December 23, 2020, 10:48pm UTC](https://discuss.elastic.co/t/how-to-match-every-element-in-the-array/259536/1 "2020-12-23T22:48:22Z")

</div>

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:

```auto
        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.

---

<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:** [January 20, 2021, 10:48pm UTC](https://discuss.elastic.co/t/how-to-match-every-element-in-the-array/259536/2 "2021-01-20T22:48:29Z")

</div>

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