# Check if field exists in a nested object

**URL:** <https://discuss.elastic.co/t/check-if-field-exists-in-a-nested-object/13845>\
**Category:** Elasticsearch\
**Created:** [October 4, 2013, 12:04pm UTC](https://discuss.elastic.co/t/check-if-field-exists-in-a-nested-object/13845 "2013-10-04T12:04:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maximilian\_S](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@Maximilian\_S](https://discuss.elastic.co/u/Maximilian_S)\
**Post date:** [October 4, 2013, 12:04pm UTC](https://discuss.elastic.co/t/check-if-field-exists-in-a-nested-object/13845/1 "2013-10-04T12:04:19Z")

</div>

In my database I always have an object "foo", and I want the results where  
the field "bar" exists. Independendtly of its value ("baz" or something  
else).

{  
"foo":{  
"bar":"baz"  
}  
}

Which query and which filter do I need?

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [October 4, 2013, 6:23pm UTC](https://discuss.elastic.co/t/check-if-field-exists-in-a-nested-object/13845/2 "2013-10-04T18:23:41Z")

</div>

Perhaps the exists filter will work in your situation:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Cheers,

Ivan

On Fri, Oct 4, 2013 at 5:04 AM, Maximilian S. \<  
[maximilian.schlederer@gmail.com](mailto:maximilian.schlederer@gmail.com)\> wrote:

> In my database I always have an object "foo", and I want the results where  
> the field "bar" exists. Independendtly of its value ("baz" or something  
> else).
> 
> {  
> "foo":{  
> "bar":"baz"  
> }  
> }
> 
> Which query and which filter do I need?
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Maximilian\_S](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@Maximilian\_S](https://discuss.elastic.co/u/Maximilian_S)\
**Post date:** [October 4, 2013, 8:03pm UTC](https://discuss.elastic.co/t/check-if-field-exists-in-a-nested-object/13845/3 "2013-10-04T20:03:59Z")

</div>

Problem is that I don't know how to apply this filter to my nested  
object... And which query should I use?

Am Freitag, 4. Oktober 2013 20:23:41 UTC+2 schrieb Ivan Brusic:

> Perhaps the exists filter will work in your situation:
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-exists-filter.html)
> 
> Cheers,
> 
> Ivan
> 
> On Fri, Oct 4, 2013 at 5:04 AM, Maximilian S. \<[maximilian...@gmail.com](mailto:maximilian...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > In my database I always have an object "foo", and I want the results  
> > where the field "bar" exists. Independendtly of its value ("baz" or  
> > something else).
> > 
> > {  
> > "foo":{  
> > "bar":"baz"  
> > }  
> > }
> > 
> > Which query and which filter do I need?
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [October 4, 2013, 8:27pm UTC](https://discuss.elastic.co/t/check-if-field-exists-in-a-nested-object/13845/4 "2013-10-04T20:27:34Z")

</div>

You can use a match all query.

curl -X GET "[http://localhost:9200/products/product/\_search?pretty=true](http://localhost:9200/products/product/_search?pretty=true)" -d  
'  
{  
"query" : {  
"nested" : {  
"path" : "foo",  
"query" : { "match\_all" : { } },  
"filter" : {  
"exists" : { "field" : "foo.bar" }  
}  
}  
},  
"size" : 0  
}  
'

You can also turn this into a filtered query depending on your use case.

Cheers,

Ivan

On Fri, Oct 4, 2013 at 1:03 PM, Maximilian S. \<  
[maximilian.schlederer@gmail.com](mailto:maximilian.schlederer@gmail.com)\> wrote:

> Problem is that I don't know how to apply this filter to my nested  
> object... And which query should I use?
> 
> Am Freitag, 4. Oktober 2013 20:23:41 UTC+2 schrieb Ivan Brusic:
> 
> > Perhaps the exists filter will work in your situation:
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/**guide/en/elasticsearch/)\*\*  
> > reference/current/query-dsl-\*\*exists-filter.html[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-exists-filter.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-exists-filter.html)
> > 
> > Cheers,
> > 
> > Ivan
> > 
> > On Fri, Oct 4, 2013 at 5:04 AM, Maximilian S. \<maximilian...@gmail.\*\*com\>wrote:
> > 
> > > In my database I always have an object "foo", and I want the results  
> > > where the field "bar" exists. Independendtly of its value ("baz" or  
> > > something else).
> > > 
> > > {  
> > > "foo":{  
> > > "bar":"baz"  
> > > }  
> > > }
> > > 
> > > Which query and which filter do I need?
> > > 
> > > --  
> > > 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 elasticsearc...@\*\*[googlegroups.com](http://googlegroups.com).
> > > 
> > > For more options, visit [https://groups.google.com/\*\*groups/opt\_out](https://groups.google.com/**groups/opt_out)[https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out)  
> > > .
> > 
> > --  
> > 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).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
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, 2:13am UTC](https://discuss.elastic.co/t/check-if-field-exists-in-a-nested-object/13845/5 "2017-07-06T02:13:41Z")

</div>


