# Query nested array for empty and specific value

**URL:** <https://discuss.elastic.co/t/query-nested-array-for-empty-and-specific-value/316263>\
**Category:** Elasticsearch\
**Tags:** eql-elastic-query-language\
**Created:** [October 11, 2022, 3:50am UTC](https://discuss.elastic.co/t/query-nested-array-for-empty-and-specific-value/316263 "2022-10-11T03:50:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kunwarshukla](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kunwarshukla](https://discuss.elastic.co/u/kunwarshukla)\
**Post date:** [October 11, 2022, 3:50am UTC](https://discuss.elastic.co/t/query-nested-array-for-empty-and-specific-value/316263/1 "2022-10-11T03:50:56Z")

</div>

**Documents structure** -

```auto
{
  "hits": [
    {
      "_type": "_doc",
      "_id": "ef0a2c44179a513476b080cc2a585d95",
      "_source": {
        "DIVISION_NUMBER": 44,
        "MATCHES": [
          {
            "MATCH_STATUS": "APPROVED",
            "UPDATED_ON": 1599171303000
          }
        ]
      }
    },
    {
      "_type": "_doc",
      "_id": "ef0a2c44179a513476b080cc2a585d95",
      "_source": {
        "DIVISION_NUMBER": 44,
        "MATCHES": []
      }
    }
  ]
}

```

**Question** - MATCHES is a nested array inside there is a text field MATCH\_STATUS that can have any values say "APPROVED","REJECTED".  
I am looking to search ALL documents that contain MATCH\_STATUS having values say "APPROVED", "RECOMMENDED" as well as where there is no data in MATCHES (empty array "MATCHES": [] ). **Please note I want this in a single query.**

I am able to do this in two separate queries like this -  
**GET all matches with status = RECOMMENDED, APPROVED**

```auto
"must": [
        {
          "nested": {
            "path": "MATCHES",
            "query": {
              "terms": {
                "MATCHES.MATCH_STATUS.keyword": [
                  "APPROVED",
                  "RECOMMENDED"
                ]
              }
            }
          }
        }
      ]

```

**GET all matches having empty array "MATCHES" : []**

```auto
{
  "size": 5000,
  "query": {
    "bool": {
      "filter": [],
      "must_not": [
        {
          "nested": {
            "path": "MATCHES",
            "query": {
              "exists": {
                "field": "MATCHES"
              }
            }
          }
        }
      ]
    }
  },
  "from": 0
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [October 25, 2022, 4:43pm UTC](https://discuss.elastic.co/t/query-nested-array-for-empty-and-specific-value/316263/2 "2022-10-25T16:43:05Z")

</div>

Hello @kunwarshukla

If you using "should", maybe works.

```auto
{
   "query":{
      "bool":{
         "should":[
            {
               "bool":{
                  "must_not":[
                     {
                        "nested":{
                           "path":"MATCHES",
                           "query":{
                              "exists":{
                                 "field":"MATCHES"
                              }
                           }
                        }
                     }
                  ]
               }
            },
            {
               "bool":{
                  "must":[
                     {
                        "nested":{
                           "path":"MATCHES",
                           "query":{
                              "terms":{
                                 "MATCHES.MATCH_STATUS.keyword":[
                                    "APPROVED",
                                    "RECOMMENDED"
                                 ]
                              }
                           }
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}

```

---

<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:** [November 22, 2022, 4:44pm UTC](https://discuss.elastic.co/t/query-nested-array-for-empty-and-specific-value/316263/3 "2022-11-22T16:44:01Z")

</div>

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