# Missing filter to check the existence of field and null\_value

**URL:** <https://discuss.elastic.co/t/missing-filter-to-check-the-existence-of-field-and-null-value/10292>\
**Category:** Elasticsearch\
**Created:** [January 10, 2013, 11:31am UTC](https://discuss.elastic.co/t/missing-filter-to-check-the-existence-of-field-and-null-value/10292 "2013-01-10T11:31:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ovamsikrishna](https://avatars.discourse-cdn.com/v4/letter/o/e8c25b/32.png) [@ovamsikrishna](https://discuss.elastic.co/u/ovamsikrishna)\
**Post date:** [January 10, 2013, 11:31am UTC](https://discuss.elastic.co/t/missing-filter-to-check-the-existence-of-field-and-null-value/10292/1 "2013-01-10T11:31:25Z")

</div>

Hi All,

My Mapping example:

include Tire::Model::Search

Tire.index('account\_1') do  
create(  
:mappings =\> {  
:user =\> {  
:properties =\> {  
:name =\> { :type =\> :string, :boost =\> 10 },  
:company\_name =\> { :type =\> :string, :boost =\> 5 }  
}  
},  
:comments =\> {  
:properties =\> {  
:description =\> { :type =\> :string, :boost =\> 5 }  
}  
}  
}  
)  
end

In the above mapping I would like to filter search results with a  
particular company\_name. The company\_name can be Null also (This is just an  
example). As you can see the comments type doesn't have company\_name field.  
So, I have to use exists filter also.

search\_key = 'test'  
Tire.search [account\_1] do  
query do  
filtered do  
query { string search\_key }  
filter :or, { :not =\> { :exists =\> { :field =\>  
:company\_name } } },  
{ :term =\> { :company\_name =\> 'test' } }  
end  
end  
end

The above filter checks just the existence of company\_name field or filters  
exact company\_name if exists. I want to add null\_value check along with  
this if company\_name field exists.  
I want to avoid the results where company\_name is nil for the user type  
along with the above filter.

Yours,

Vamsi Krishna O.

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [January 14, 2013, 11:36pm UTC](https://discuss.elastic.co/t/missing-filter-to-check-the-existence-of-field-and-null-value/10292/2 "2013-01-14T23:36:58Z")

</div>

I think what you are looking for is actually called the missing[http://www.elasticsearch.org/guide/reference/query-dsl/missing-filter.html](http://www.elasticsearch.org/guide/reference/query-dsl/missing-filter.html)filter.

On Thursday, January 10, 2013 6:31:25 AM UTC-5, vamsi krishna wrote:

> Hi All,
> 
> My Mapping example:
> 
> include Tire::Model::Search
> 
> Tire.index('account\_1') do  
> create(  
> :mappings =\> {  
> :user =\> {  
> :properties =\> {  
> :name =\> { :type =\> :string, :boost =\> 10 },  
> :company\_name =\> { :type =\> :string, :boost =\> 5 }  
> }  
> },  
> :comments =\> {  
> :properties =\> {  
> :description =\> { :type =\> :string, :boost =\> 5 }  
> }  
> }  
> }  
> )  
> end
> 
> In the above mapping I would like to filter search results with a  
> particular company\_name. The company\_name can be Null also (This is just an  
> example). As you can see the comments type doesn't have company\_name field.  
> So, I have to use exists filter also.
> 
> search\_key = 'test'  
> Tire.search [account\_1] do  
> query do  
> filtered do  
> query { string search\_key }  
> filter :or, { :not =\> { :exists =\> { :field =\> :company\_name } } },  
> { :term =\> { :company\_name =\> 'test' } }  
> end  
> end  
> end
> 
> The above filter checks just the existence of company\_name field or  
> filters exact company\_name if exists. I want to add null\_value check along  
> with this if company\_name field exists.  
> I want to avoid the results where company\_name is nil for the user type  
> along with the above filter.
> 
> Yours,
> 
> Vamsi Krishna O.

--

---

<div class="post-metadata">

**Author:** ![ovamsikrishna](https://avatars.discourse-cdn.com/v4/letter/o/e8c25b/32.png) [@ovamsikrishna](https://discuss.elastic.co/u/ovamsikrishna)\
**Post date:** [January 15, 2013, 5:43am UTC](https://discuss.elastic.co/t/missing-filter-to-check-the-existence-of-field-and-null-value/10292/3 "2013-01-15T05:43:46Z")

</div>

First of all sorry for asking the question using the Tire gem functions. I  
should have posted the gist or curl example in elasticsearch group.

Thanks for the reply Igor.

Even missing filter is avoiding null values. Came to know that a field with  
a null value simply isn't even stored.

Finally achieved it with the help of suggestion given by Tire gem owner  
Karmi. I had to replace the null value in the document with mapping via the  
null\_value option, and then used the same filter.

On Tuesday, January 15, 2013 5:06:58 AM UTC+5:30, Igor Motov wrote:

> I think what you are looking for is actually called the missing[http://www.elasticsearch.org/guide/reference/query-dsl/missing-filter.html](http://www.elasticsearch.org/guide/reference/query-dsl/missing-filter.html)filter.
> 
> On Thursday, January 10, 2013 6:31:25 AM UTC-5, vamsi krishna wrote:
> 
> > Hi All,
> > 
> > My Mapping example:
> > 
> > include Tire::Model::Search
> > 
> > Tire.index('account\_1') do  
> > create(  
> > :mappings =\> {  
> > :user =\> {  
> > :properties =\> {  
> > :name =\> { :type =\> :string, :boost =\> 10 },  
> > :company\_name =\> { :type =\> :string, :boost =\> 5 }  
> > }  
> > },  
> > :comments =\> {  
> > :properties =\> {  
> > :description =\> { :type =\> :string, :boost =\> 5 }  
> > }  
> > }  
> > }  
> > )  
> > end
> > 
> > In the above mapping I would like to filter search results with a  
> > particular company\_name. The company\_name can be Null also (This is just an  
> > example). As you can see the comments type doesn't have company\_name field.  
> > So, I have to use exists filter also.
> > 
> > search\_key = 'test'  
> > Tire.search [account\_1] do  
> > query do  
> > filtered do  
> > query { string search\_key }  
> > filter :or, { :not =\> { :exists =\> { :field =\> :company\_name } } },  
> > { :term =\> { :company\_name =\> 'test' } }  
> > end  
> > end  
> > end
> > 
> > The above filter checks just the existence of company\_name field or  
> > filters exact company\_name if exists. I want to add null\_value check along  
> > with this if company\_name field exists.  
> > I want to avoid the results where company\_name is nil for the user type  
> > along with the above filter.
> > 
> > Yours,
> > 
> > Vamsi Krishna O.

--

---

<div class="post-metadata">

**Author:** ![ovamsikrishna](https://avatars.discourse-cdn.com/v4/letter/o/e8c25b/32.png) [@ovamsikrishna](https://discuss.elastic.co/u/ovamsikrishna)\
**Post date:** [January 15, 2013, 5:51am UTC](https://discuss.elastic.co/t/missing-filter-to-check-the-existence-of-field-and-null-value/10292/4 "2013-01-15T05:51:35Z")

</div>

The original requirement is that, I have documents one with the field  
company\_name - Test, other with out the field company\_name and another with  
company\_name as NULL.

Now, from these documents, I need the results where company\_name = Test or  
company\_name field doesn't exist. I don't need the document with  
company\_name = NULL.

On Tuesday, January 15, 2013 5:06:58 AM UTC+5:30, Igor Motov wrote:

> I think what you are looking for is actually called the missing[http://www.elasticsearch.org/guide/reference/query-dsl/missing-filter.html](http://www.elasticsearch.org/guide/reference/query-dsl/missing-filter.html)filter.
> 
> On Thursday, January 10, 2013 6:31:25 AM UTC-5, vamsi krishna wrote:
> 
> > Hi All,
> > 
> > My Mapping example:
> > 
> > include Tire::Model::Search
> > 
> > Tire.index('account\_1') do  
> > create(  
> > :mappings =\> {  
> > :user =\> {  
> > :properties =\> {  
> > :name =\> { :type =\> :string, :boost =\> 10 },  
> > :company\_name =\> { :type =\> :string, :boost =\> 5 }  
> > }  
> > },  
> > :comments =\> {  
> > :properties =\> {  
> > :description =\> { :type =\> :string, :boost =\> 5 }  
> > }  
> > }  
> > }  
> > )  
> > end
> > 
> > In the above mapping I would like to filter search results with a  
> > particular company\_name. The company\_name can be Null also (This is just an  
> > example). As you can see the comments type doesn't have company\_name field.  
> > So, I have to use exists filter also.
> > 
> > search\_key = 'test'  
> > Tire.search [account\_1] do  
> > query do  
> > filtered do  
> > query { string search\_key }  
> > filter :or, { :not =\> { :exists =\> { :field =\> :company\_name } } },  
> > { :term =\> { :company\_name =\> 'test' } }  
> > end  
> > end  
> > end
> > 
> > The above filter checks just the existence of company\_name field or  
> > filters exact company\_name if exists. I want to add null\_value check along  
> > with this if company\_name field exists.  
> > I want to avoid the results where company\_name is nil for the user type  
> > along with the above filter.
> > 
> > Yours,
> > 
> > Vamsi Krishna O.

--

---

<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:56am UTC](https://discuss.elastic.co/t/missing-filter-to-check-the-existence-of-field-and-null-value/10292/5 "2017-07-06T02:56:27Z")

</div>


