# Exist filter also matching on nested fields

**URL:** <https://discuss.elastic.co/t/exist-filter-also-matching-on-nested-fields/22066>\
**Category:** Elasticsearch\
**Created:** [February 10, 2015, 2:52pm UTC](https://discuss.elastic.co/t/exist-filter-also-matching-on-nested-fields/22066 "2015-02-10T14:52:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jilles\_van\_Gurp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jilles_van_gurp/32/879_2.png) [@Jilles\_van\_Gurp](https://discuss.elastic.co/u/Jilles_van_Gurp)\
**Post date:** [February 10, 2015, 2:52pm UTC](https://discuss.elastic.co/t/exist-filter-also-matching-on-nested-fields/22066/1 "2015-02-10T14:52:18Z")

</div>

I'm trying to filter documents that have a particular field at the top  
level. Like for example:

{  
"group\_id":"xxx"  
}

so I wrote the following query:

GET inbot\_users/usercontact/\_search  
{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"exists": {  
"field": "group\_id"  
}  
}  
}  
}  
}

But now my problem is that it is also returning documents with a nested  
group\_id property:

{  
"nested":{  
"group\_id":"xxx"  
}  
}

In this case I wouldn't expect a match because I specified "group\_id" and  
not "nested.group\_id".

How can I make it filter just documents that have the field at the top  
level like is clearly the intention here?

--  
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/d2019aac-77e8-43ce-a7c3-2afb4a8403e3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d2019aac-77e8-43ce-a7c3-2afb4a8403e3%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Hannes\_Korte](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hannes_korte/32/909_2.png) [@Hannes\_Korte](https://discuss.elastic.co/u/Hannes_Korte)\
**Post date:** [February 10, 2015, 3:40pm UTC](https://discuss.elastic.co/t/exist-filter-also-matching-on-nested-fields/22066/2 "2015-02-10T15:40:37Z")

</div>

Hi Jilles,

I just tried, but wasn't able to reproduce this behavior. In my test ES  
only returned the correct document. I created a gist for that:

> <https://gist.github.com/hkorte/ca5f91e2f4838213d956>

I tried it using ES 1.4.1. Which version are you using? Does my gist  
work for you? I noticed that it doesn't matter whether or not it is a  
nested document.

Best regards,  
Hannes

On 10.02.2015 15:52, Jilles van Gurp wrote:

> I'm trying to filter documents that have a particular field at the top  
> level. Like for example:
> 
> {  
> "group\_id":"xxx"  
> }
> 
> so I wrote the following query:
> 
> GET inbot\_users/usercontact/\_search  
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "match\_all": {}  
> },  
> "filter": {  
> "exists": {  
> "field": "group\_id"  
> }  
> }  
> }  
> }  
> }
> 
> But now my problem is that it is also returning documents with a nested  
> group\_id property:
> 
> {  
> "nested":{  
> "group\_id":"xxx"  
> }  
> }
> 
> In this case I wouldn't expect a match because I specified "group\_id" and  
> not "nested.group\_id".
> 
> How can I make it filter just documents that have the field at the top  
> level like is clearly the intention here?

--  
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/54DA2675.1080401%40hkorte.com](https://groups.google.com/d/msgid/elasticsearch/54DA2675.1080401%40hkorte.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Jilles\_van\_Gurp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jilles_van_gurp/32/879_2.png) [@Jilles\_van\_Gurp](https://discuss.elastic.co/u/Jilles_van_Gurp)\
**Post date:** [February 10, 2015, 5:20pm UTC](https://discuss.elastic.co/t/exist-filter-also-matching-on-nested-fields/22066/3 "2015-02-10T17:20:11Z")

</div>

Thanks, I've been trying to reproduce this myself with a minimal  
example but so far it seems specific to our index in production.

Our mapping there is kind of large (over 7000 lines:  
[es mapping · GitHub](https://gist.github.com/jillesvangurp/4328b0df634b09c60f44)) with lots of  
auto generated fields, so that doesn't help. As you can see, the group\_id  
field comes back in a few places.

We use parent child relations, default templates, etc. So far all the  
mapping variations I've tried on my minimal example fail to reproduce the  
problem. Obviously it somehow is related to my mapping but can't spot how  
or why.

I'll keep on digging to see if I can find anything.

Jilles

On Tuesday, February 10, 2015 at 4:40:54 PM UTC+1, Hannes Korte wrote:

> Hi Jilles,
> 
> I just tried, but wasn't able to reproduce this behavior. In my test ES  
> only returned the correct document. I created a gist for that:  
> [An Elasticsearch gist for the thread "exist filter also matching on nested fields": https://groups.google.com/forum/#!topic/elasticsearch/8R9XgVkCZIU · GitHub](https://gist.github.com/hkorte/ca5f91e2f4838213d956)
> 
> I tried it using ES 1.4.1. Which version are you using? Does my gist  
> work for you? I noticed that it doesn't matter whether or not it is a  
> nested document.
> 
> Best regards,  
> Hannes
> 
> On 10.02.2015 15:52, Jilles van Gurp wrote:
> 
> > I'm trying to filter documents that have a particular field at the top  
> > level. Like for example:
> > 
> > {  
> > "group\_id":"xxx"  
> > }
> > 
> > so I wrote the following query:
> > 
> > GET inbot\_users/usercontact/\_search  
> > {  
> > "query": {  
> > "filtered": {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "filter": {  
> > "exists": {  
> > "field": "group\_id"  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > But now my problem is that it is also returning documents with a nested  
> > group\_id property:
> > 
> > {  
> > "nested":{  
> > "group\_id":"xxx"  
> > }  
> > }
> > 
> > In this case I wouldn't expect a match because I specified "group\_id"  
> > and  
> > not "nested.group\_id".
> > 
> > How can I make it filter just documents that have the field at the top  
> > level like is clearly the intention here?

--  
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/a90712c5-c916-4a37-92c9-6778eeed645f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a90712c5-c916-4a37-92c9-6778eeed645f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:33am UTC](https://discuss.elastic.co/t/exist-filter-also-matching-on-nested-fields/22066/4 "2017-07-06T00:33:52Z")

</div>


