# Query to fetch Document if certain nested document does not exists

**URL:** <https://discuss.elastic.co/t/query-to-fetch-document-if-certain-nested-document-does-not-exists/224904>\
**Category:** Elasticsearch\
**Created:** [March 24, 2020, 6:14pm UTC](https://discuss.elastic.co/t/query-to-fetch-document-if-certain-nested-document-does-not-exists/224904 "2020-03-24T18:14:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![farhanAamir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/farhanaamir/32/65002_2.png) [@farhanAamir](https://discuss.elastic.co/u/farhanAamir)\
**Post date:** [March 24, 2020, 6:14pm UTC](https://discuss.elastic.co/t/query-to-fetch-document-if-certain-nested-document-does-not-exists/224904/1 "2020-03-24T18:14:38Z")

</div>

Hi,

I am new to elastic search and got stuck in a scenario. I have document mapping as below:

```auto
    {
        "mappings": {
            "customer": {
                "properties": {
                    "customerEmails": {
                        "type": "nested",
                        "properties": {
                            "address": {
                                "type": "text"
                            },
                            "type": {
                                "type": "text"
                            }
                        }
                    }
                }
            }
        }
    }

```

Type field in customerEmails object could be Home, Work, and Mobile. I want a query to fetch those document where no email address exists with type Mobile.

---

<div class="post-metadata">

**Author:** ![inhinyera16](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/inhinyera16/32/61625_2.png) [@inhinyera16](https://discuss.elastic.co/u/inhinyera16)\
**Post date:** [March 24, 2020, 7:32pm UTC](https://discuss.elastic.co/t/query-to-fetch-document-if-certain-nested-document-does-not-exists/224904/2 "2020-03-24T19:32:51Z")

</div>

```
GET indexname/_search
{
 "query": {
   "nested": {
     "path": "customer.customerEmails",
     "query": {
       "bool": {
         "must_not": [
           {
             "exists": {
               "field": "address"
             }
           }
         ],
         "must": [
           {
             "term": {
               "customer.customerEmails.type.keyword": {
                 "value": "Mobile"
               }
             }
           }
         ]
       }
     },
     "inner_hits": {
       "name": "customerEmailsFetchResult"
     }
   }
 }
}
```

---

<div class="post-metadata">

**Author:** ![farhanAamir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/farhanaamir/32/65002_2.png) [@farhanAamir](https://discuss.elastic.co/u/farhanAamir)\
**Post date:** [March 26, 2020, 7:49am UTC](https://discuss.elastic.co/t/query-to-fetch-document-if-certain-nested-document-does-not-exists/224904/3 "2020-03-26T07:49:03Z")

</div>

Thanks for reply. The query seems to me logically perfect. But it somehow do not bring any result when I hit it in Kibana dev tool not even a single document. I have multiple document that should meeting the query criteria.

---

<div class="post-metadata">

**Author:** ![inhinyera16](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/inhinyera16/32/61625_2.png) [@inhinyera16](https://discuss.elastic.co/u/inhinyera16)\
**Post date:** [March 26, 2020, 12:52pm UTC](https://discuss.elastic.co/t/query-to-fetch-document-if-certain-nested-document-does-not-exists/224904/4 "2020-03-26T12:52:36Z")

</div>

you can try one condition at a time and see which part causes this

---

<div class="post-metadata">

**Author:** ![farhanAamir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/farhanaamir/32/65002_2.png) [@farhanAamir](https://discuss.elastic.co/u/farhanAamir)\
**Post date:** [April 9, 2020, 2:49pm UTC](https://discuss.elastic.co/t/query-to-fetch-document-if-certain-nested-document-does-not-exists/224904/5 "2020-04-09T14:49:00Z")

</div>

Sorry for the late reply. I was able to get the required document with following query:

```auto
           {
                "query": {
                    "nested": {
                        "path": "customer.customerEmails",
                        "query": {
                            "bool": {
                                "must_not": [
                                    {
                                        "match": {
                                            "customer.customerEmails": {
                                                "type": "address"
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
           }

```

Thanks for your help.

---

<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:** [May 7, 2020, 2:49pm UTC](https://discuss.elastic.co/t/query-to-fetch-document-if-certain-nested-document-does-not-exists/224904/6 "2020-05-07T14:49:10Z")

</div>

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