# Search documents with array elements matching two conditions

**URL:** <https://discuss.elastic.co/t/search-documents-with-array-elements-matching-two-conditions/165409>\
**Category:** Elasticsearch\
**Created:** [January 23, 2019, 12:49pm UTC](https://discuss.elastic.co/t/search-documents-with-array-elements-matching-two-conditions/165409 "2019-01-23T12:49:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvienne](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@mvienne](https://discuss.elastic.co/u/mvienne)\
**Post date:** [January 23, 2019, 12:49pm UTC](https://discuss.elastic.co/t/search-documents-with-array-elements-matching-two-conditions/165409/1 "2019-01-23T12:49:01Z")

</div>

Hi,

I want to search documents in an index which contains an array type with two documents :

```
PUT my_index/doc/1
{
    "message": "some arrays in this document...",
    "tags": ["elasticsearch", "wow"], 
    "lists": [
        {
            "name": "cool_list",
            "description": "cool stuff list"
        }
    ]
}

PUT my_index/doc/2 
{
    "message": "some arrays in this document...",
    "tags": "elasticsearch",
    "lists": [ 
        {
            "name": "cool_list",
            "description": "just a cool list"
        },
        {
            "name": "other_list",
            "description": "uncool stuff list"
        }
    ]
}

```

How do I get the documents in which the attribute "lists" contains an element which matches both of the following criterias :

- "name" matches "cool\_list"
- "description" matches "stuff"

With the following search query, I am not getting the results that I want, because I also get doc 2 in which none of the two items match both the two criterias, but each item matches one of the two different criterias :

```
GET my_index/_search
{
    "query": {
        "bool": {
            "must": [
                { "match": { "lists.name": "cool_list" } },
                { "match": { "lists.description": "stuff" } }
            ]
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 23, 2019, 1:00pm UTC](https://discuss.elastic.co/t/search-documents-with-array-elements-matching-two-conditions/165409/2 "2019-01-23T13:00:40Z")

</div>

You need to define lists as a nested type and use then nested queries.

---

<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:** [February 20, 2019, 1:00pm UTC](https://discuss.elastic.co/t/search-documents-with-array-elements-matching-two-conditions/165409/3 "2019-02-20T13:00:46Z")

</div>

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