# Elastic search query to return documents matching all elements in an array

**URL:** <https://discuss.elastic.co/t/elastic-search-query-to-return-documents-matching-all-elements-in-an-array/208424>\
**Category:** Elasticsearch\
**Created:** [November 19, 2019, 5:08am UTC](https://discuss.elastic.co/t/elastic-search-query-to-return-documents-matching-all-elements-in-an-array/208424 "2019-11-19T05:08:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![achand45](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@achand45](https://discuss.elastic.co/u/achand45)\
**Post date:** [November 19, 2019, 5:08am UTC](https://discuss.elastic.co/t/elastic-search-query-to-return-documents-matching-all-elements-in-an-array/208424/1 "2019-11-19T05:08:40Z")

</div>

I have a structure similar to this:

Document 1:

nestedobject: {

uniqueid: "12345",

field: [ {id: 1,

```
      color: blue,

     fruit:banana},

```

{id: 2,

```
      color: red,

```

fruit:apple},

]

}

Document 2: (in same index)

nestedobject: {

uniqueid:23456,

field: [ {id: 3,

```
      color: blue, 

```

fruit:banana},

{id: 4,

```
      color: blue,

```

fruit:banana},

]

}

the field mappings can be seen as :

{"mappings":

"nestedobject":{

"properties":{

"uniqueid":{

"type":"text",

"fields":{

"keyword":{

"type":"keyword",

"ignore\_above":256

}

}

}

"field":{

"type":"nested",

"id":{

"type":"text",

"fields":{

"keyword":{

"type":"keyword",

"ignore\_above":256

}

}

},

"color":{

"type":"text",

"fields":{

"keyword":{

"type":"keyword",

"ignore\_above":256

}

}

},

"fruit":{

"type":"text",

"fields":{

"keyword":{

"type":"keyword",

"ignore\_above":256

}

}

}

}

}

}

}

Now, I try to query this index with 2 documents and I want only the document which has all the elements in the field array with color blue and fruit as banana - NOT ATLEAST 1.

Right now, with the query, I get returned both the documents as it matches with the first element of the first document and returns that.

How to make this possible?

{

```
"query": {

    "nested" : {

        "path" : "nestedobject.field",

        "query" : {

            "bool" : {

                "must" : [

                { "match" : {"nestedobject.field.color" : "blue"} },

                { "match" : {"nestedobject.field.fruit" : "banana"}}

                ]

            }

        }

    }

}

```

}

---

<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:** [December 17, 2019, 5:08am UTC](https://discuss.elastic.co/t/elastic-search-query-to-return-documents-matching-all-elements-in-an-array/208424/2 "2019-12-17T05:08:41Z")

</div>

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