Query to fetch Document if certain nested document does not exists

Hi,

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

    {
        "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.

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"
     }
   }
 }
}

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.

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

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

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

Thanks for your help.

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