# Filter by existance of property?

**URL:** <https://discuss.elastic.co/t/filter-by-existance-of-property/4877>\
**Category:** Elasticsearch\
**Created:** [July 16, 2011, 7:09pm UTC](https://discuss.elastic.co/t/filter-by-existance-of-property/4877 "2011-07-16T19:09:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joel\_Abrahamsson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joel_abrahamsson/32/1924_2.png) [@Joel\_Abrahamsson](https://discuss.elastic.co/u/Joel_Abrahamsson)\
**Post date:** [July 16, 2011, 7:09pm UTC](https://discuss.elastic.co/t/filter-by-existance-of-property/4877/1 "2011-07-16T19:09:58Z")

</div>

Hi!

Seeing how this is my first post here I want to start by thanking all  
contributors for a great project!

Now on to my question. I'm wondering if it's possible to filter  
documents based on the existence of a JSON property. Let's say I have  
two documents:

FIRST:  
{  
"Title":"Great rock",  
"Author":{  
"Id":1,  
"Name":"Joel"  
}  
}

SECOND:  
{  
"Title": "More great rock music"  
}

I would like to find only those that have an author. However, both the  
Id and Name of the author can be null.  
My first instinct was to try:  
{  
"filter":{  
"exists":{  
"field":"Author"  
}  
}  
}

However that doesn't work. I'm guessing that's the case because Author  
isn't actually a field, only it's child properties are?

I could of course filter on one of the Author properties child-  
properties but seeing how they could both be null I would probably  
have to do an OR filter on both which seems like a last resort.

Any ideas?

Sorry if this is a question that has been answered before. I've done  
my best trying to find discussions about similar problems but with no  
luck.

Thanks in advance! /Joel

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [July 16, 2011, 8:43pm UTC](https://discuss.elastic.co/t/filter-by-existance-of-property/4877/2 "2011-07-16T20:43:29Z")

</div>

Heya,

Yes, you will need to explicitly OR between the author fields, no way  
around it unless you add a special marker boolean field on each author  
object, and only ask on it (better for perf).

-shay.banon

On Sat, Jul 16, 2011 at 10:09 PM, Joel Abrahamsson \<[mail@joelabrahamsson.com](mailto:mail@joelabrahamsson.com)

> wrote:

> Hi!
> 
> Seeing how this is my first post here I want to start by thanking all  
> contributors for a great project!
> 
> Now on to my question. I'm wondering if it's possible to filter  
> documents based on the existence of a JSON property. Let's say I have  
> two documents:
> 
> FIRST:  
> {  
> "Title":"Great rock",  
> "Author":{  
> "Id":1,  
> "Name":"Joel"  
> }  
> }
> 
> SECOND:  
> {  
> "Title": "More great rock music"  
> }
> 
> I would like to find only those that have an author. However, both the  
> Id and Name of the author can be null.  
> My first instinct was to try:  
> {  
> "filter":{  
> "exists":{  
> "field":"Author"  
> }  
> }  
> }
> 
> However that doesn't work. I'm guessing that's the case because Author  
> isn't actually a field, only it's child properties are?
> 
> I could of course filter on one of the Author properties child-  
> properties but seeing how they could both be null I would probably  
> have to do an OR filter on both which seems like a last resort.
> 
> Any ideas?
> 
> Sorry if this is a question that has been answered before. I've done  
> my best trying to find discussions about similar problems but with no  
> luck.
> 
> Thanks in advance! /Joel

---

<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, 4:00am UTC](https://discuss.elastic.co/t/filter-by-existance-of-property/4877/3 "2017-07-06T04:00:24Z")

</div>

Closed on July 6, 2017, 4:00am UTC


