# Behavior of missing and exists with nested objects

**URL:** <https://discuss.elastic.co/t/behavior-of-missing-and-exists-with-nested-objects/15185>\
**Category:** Elasticsearch\
**Created:** [January 9, 2014, 11:47pm UTC](https://discuss.elastic.co/t/behavior-of-missing-and-exists-with-nested-objects/15185 "2014-01-09T23:47:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathan_2/32/1875_2.png) [@Nathan\_2](https://discuss.elastic.co/u/Nathan_2)\
**Post date:** [January 9, 2014, 11:47pm UTC](https://discuss.elastic.co/t/behavior-of-missing-and-exists-with-nested-objects/15185/1 "2014-01-09T23:47:15Z")

</div>

I am trying to figure out how missing/exists works with nested objects, and  
what I am seeing is very strange. Here is a gist to demonstrate:  
[https://gist.github.com/nathanmoon/8344115](https://gist.github.com/nathanmoon/8344115)

I have a nested object, ratings, with a mapping like:

```
        "ratings" : {
            "type" : "nested",
            "properties" : {
                "rater_username" : {
                    "type" : "string",
                    "index" : "not_analyzed"
                },
                "rating" : {
                    "type" : "integer",
                    "index" : "not_analyzed"
                }
            }
        }

```

My data looks like this:

curl -XPOST "[http://localhost:9200/nestedfilters/item/](http://localhost:9200/nestedfilters/item/)" -d '  
{  
"description" : "Rated by user1",  
"ratings" : [{  
"rater\_username" : "user1",  
"rating" : 10  
}]  
}  
'

curl -XPOST "[http://localhost:9200/nestedfilters/item/](http://localhost:9200/nestedfilters/item/)" -d '  
{  
"description" : "Rated but missing username",  
"ratings" : [{  
"rating" : 10  
}]  
}  
'

curl -XPOST "[http://localhost:9200/nestedfilters/item/](http://localhost:9200/nestedfilters/item/)" -d '  
{  
"description" : "Rated by empty set",  
"ratings" : []  
}  
'

curl -XPOST "[http://localhost:9200/nestedfilters/item/](http://localhost:9200/nestedfilters/item/)" -d '  
{  
"description" : "Rated by nobody",  
}  
'

Here is what I'm getting with various filters (sorry for the formatting):

* * *

FILTER:

```
"filter" : {
    "missing" : {
        "field" : "ratings"
    }
}

```

WHAT I WOULD EXPECT:

"Rated by empty set"  
"Rated by nobody"

IT RETURNS:

"Rated by user1"  
"Rated but missing username"  
"Rated by empty set"

* * *

FILTER:

```
"filter" : {
    "nested" : {
        "path" : "ratings",
        "filter" : {
            "missing" : {
                "field" : "ratings.rating"
            }
        }
    }
}

```

WHAT I WOULD EXPECT:

"Rated by empty set"  
"Rated by nobody"

IT RETURNS:

[empty set]

* * *

FILTER:

```
"filter" : {
    "not" : {
        "nested" : {
            "path" : "ratings",
            "filter" : {
                "exists" : {
                    "field" : "ratings.rating"
                }
            }
        }
    }
}

```

WHAT I WOULD EXPECT:

"Rated by empty set"  
"Rated by nobody"

IT RETURNS:

"Rated by empty set"

* * *

FILTER:

```
"filter" : {
    "not" : {
        "nested" : {
            "path" : "ratings",
            "filter" : {
                "exists" : {
                    "field" : "ratings.rater_username"
                }
            }
        }
    }
}

```

WHAT I WOULD EXPECT:

"Rated but missing username"  
"Rated by empty set"  
"Rated by nobody"

IT RETURNS:

"Rated but missing username"  
"Rated by empty set"

* * *

Can anyone explain what I am seeing, and what is the best way to index and  
query for 'missing' nested fields? Thank you!

Nathan

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/eee98195-312f-4722-81ae-4c8c39e1f029%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/eee98195-312f-4722-81ae-4c8c39e1f029%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [July 6, 2017, 1:57am UTC](https://discuss.elastic.co/t/behavior-of-missing-and-exists-with-nested-objects/15185/2 "2017-07-06T01:57:34Z")

</div>


