Find doucment with blank nested field


(Jagdish) #1

Hi, I have nested property in my index document. I want to find all documents where nested field is blank.
example: I am indexing below document, how can I search for document where contact is not available ?

  1. {"Name": John, "contact": [{"office": 1234, "home":2345 }] }
  2. {"Name": Tom }

Thanks !!!


(Isabel Drost-Fromm) #2

I think the exits/missing filters might help you here. To quote from the documentation at https://www.elastic.co/guide/en/elasticsearch/guide/current/_dealing_with_null_values.html

When dealing with null values "The first tool in your arsenal is the exists filter. This filter will return documents that have any value in the specified field. [...] The missing filter is essentially the inverse of exists: it returns documents where there is no value for a particular field. [...] The exists and missing filters also work on inner objects, not just core types."

DELETE index

PUT index/type/1 {
    "Name": "John", "contact": [{"office": "1234", "home":"2345" }] 
}

PUT index/type/2 {
  "Name": "Tom" 
}

GET index/_search {
    "query": {
        "missing" : { "field" : "contact" }
    }
}

Hope this helps you,

Isabel


(Jagdish) #3

@mainec, I have tried with missing and its working fine if field is normal field. In my case contact in nested property, when I try the same query, its giving all records. Below is the query I am using, Am I doing anything wrong ?

POST index/_search
{
"query" : {
"filtered" : {
"filter": {
"missing" : { "field" : "contact" }
}
}
}
}


(Isabel Drost-Fromm) #4

I see - I naively assumed you were simply putting the document you supplied to the index and relied on automatic mappings. Seems like you supplied an explicit mapping beforehand then, right?

I think the information on the following github issue might help you: https://github.com/elastic/elasticsearch/issues/14112

Hope this helps,
Isabel


(Jagdish) #5

Thanks @mainec, Yes I applied explicit mapping. Seems this is know issue.


(system) #6