# How to query documents for a field to be null?

**URL:** <https://discuss.elastic.co/t/how-to-query-documents-for-a-field-to-be-null/7137>\
**Category:** Elasticsearch\
**Created:** [March 27, 2012, 1:47am UTC](https://discuss.elastic.co/t/how-to-query-documents-for-a-field-to-be-null/7137 "2012-03-27T01:47:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![alichi](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@alichi](https://discuss.elastic.co/u/alichi)\
**Post date:** [March 27, 2012, 1:47am UTC](https://discuss.elastic.co/t/how-to-query-documents-for-a-field-to-be-null/7137/1 "2012-03-27T01:47:53Z")

</div>

I am trying to run a query like below:  
{"query" : {"term" : { "my\_field": null } } }

but it seems that it is not working, it does not return anything (using the  
curl command and also the HEAD plugin).

Thanks,  
Ali

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 27, 2012, 4:57am UTC](https://discuss.elastic.co/t/how-to-query-documents-for-a-field-to-be-null/7137/2 "2012-03-27T04:57:55Z")

</div>

See [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/missing-filter.html)

David 😉  
Twitter : @dadoonet / @elasticsearchfr

Le 27 mars 2012 à 03:47, Ali Loghmani [loghmani@gmail.com](mailto:loghmani@gmail.com) a écrit :

> I am trying to run a query like below:  
> {"query" : {"term" : { "my\_field": null } } }
> 
> but it seems that it is not working, it does not return anything (using the curl command and also the HEAD plugin).
> 
> Thanks,  
> Ali

---

<div class="post-metadata">

**Author:** ![alichi](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@alichi](https://discuss.elastic.co/u/alichi)\
**Post date:** [March 27, 2012, 7:58pm UTC](https://discuss.elastic.co/t/how-to-query-documents-for-a-field-to-be-null/7137/3 "2012-03-27T19:58:29Z")

</div>

cool, easy 😉

On Monday, March 26, 2012 6:47:53 PM UTC-7, Ali Loghmani wrote:

> I am trying to run a query like below:  
> {"query" : {"term" : { "my\_field": null } } }
> 
> but it seems that it is not working, it does not return anything (using  
> the curl command and also the HEAD plugin).
> 
> Thanks,  
> Ali

---

<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:** [March 28, 2012, 10:31am UTC](https://discuss.elastic.co/t/how-to-query-documents-for-a-field-to-be-null/7137/4 "2012-03-28T10:31:27Z")

</div>

Mmm, wondering if we can do that automatically and translate term query /  
filter against null to missing one where applicable. Can you open an issue?

On Tue, Mar 27, 2012 at 9:58 PM, Ali Loghmani [loghmani@gmail.com](mailto:loghmani@gmail.com) wrote:

> cool, easy 😉
> 
> On Monday, March 26, 2012 6:47:53 PM UTC-7, Ali Loghmani wrote:
> 
> > I am trying to run a query like below:  
> > {"query" : {"term" : { "my\_field": null } } }
> > 
> > but it seems that it is not working, it does not return anything (using  
> > the curl command and also the HEAD plugin).
> > 
> > Thanks,  
> > Ali

---

<div class="post-metadata">

**Author:** ![Anurag\_Biyani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anurag_biyani/32/2844_2.png) [@Anurag\_Biyani](https://discuss.elastic.co/u/Anurag_Biyani)\
**Post date:** [April 13, 2012, 1:36am UTC](https://discuss.elastic.co/t/how-to-query-documents-for-a-field-to-be-null/7137/5 "2012-04-13T01:36:04Z")

</div>

Aren't {"x": "blah", "y": null}, AND, {"x": "blah"} two different  
JSON obejcts ? Shouldn't a json document with a field having value  
null should be treated differently then a json document which is  
missing the field all together ?  
It seems that elasticsearch's "missing" filter treats json value  
"null" as equivalent to absence of field. Is this the desired  
behavior ?

On Mar 28, 3:31 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> Mmm, wondering if we can do that automatically and translate term query /  
> filter against null to missing one where applicable. Can you open an issue?
> 
> On Tue, Mar 27, 2012 at 9:58 PM, Ali Loghmani [loghm...@gmail.com](mailto:loghm...@gmail.com) wrote:
> 
> > cool, easy 😉
> 
> > On Monday, March 26, 2012 6:47:53 PM UTC-7, Ali Loghmani wrote:
> 
> > > I am trying to run a query like below:  
> > > {"query" : {"term" : { "my\_field": null } } }
> 
> > > but it seems that it is not working, it does not return anything (using  
> > > the curl command and also the HEAD plugin).
> 
> > > Thanks,  
> > > Ali

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [April 13, 2012, 10:30am UTC](https://discuss.elastic.co/t/how-to-query-documents-for-a-field-to-be-null/7137/6 "2012-04-13T10:30:09Z")

</div>

On Thu, 2012-04-12 at 18:36 -0700, Anurag Biyani wrote:

> Aren't {"x": "blah", "y": null}, AND, {"x": "blah"} two different  
> JSON obejcts ? Shouldn't a json document with a field having value  
> null should be treated differently then a json document which is  
> missing the field all together ?  
> It seems that elasticsearch's "missing" filter treats json value  
> "null" as equivalent to absence of field. Is this the desired  
> behavior ?

From a programmer's perspective, yes, they are different. From ES'  
perspective, they aren't.

You could work around this by providing a null\_value setting in the  
mapping which would be indexed when the property is provided but has a  
null value.

That, of course, assumes that the value you choose for the null\_value  
could never occur as a real value for that property

clint

> On Mar 28, 3:31 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:
> 
> > Mmm, wondering if we can do that automatically and translate term query /  
> > filter against null to missing one where applicable. Can you open an issue?
> > 
> > On Tue, Mar 27, 2012 at 9:58 PM, Ali Loghmani [loghm...@gmail.com](mailto:loghm...@gmail.com) wrote:
> > 
> > > cool, easy 😉
> > 
> > > On Monday, March 26, 2012 6:47:53 PM UTC-7, Ali Loghmani wrote:
> > 
> > > > I am trying to run a query like below:  
> > > > {"query" : {"term" : { "my\_field": null } } }
> > 
> > > > but it seems that it is not working, it does not return anything (using  
> > > > the curl command and also the HEAD plugin).
> > 
> > > > Thanks,  
> > > > Ali

---

<div class="post-metadata">

**Author:** ![Anurag\_Biyani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anurag_biyani/32/2844_2.png) [@Anurag\_Biyani](https://discuss.elastic.co/u/Anurag_Biyani)\
**Post date:** [April 13, 2012, 5:37pm UTC](https://discuss.elastic.co/t/how-to-query-documents-for-a-field-to-be-null/7137/7 "2012-04-13T17:37:47Z")

</div>

Thanks for the quick reply! 🙂

On Apr 13, 3:30 am, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:

> On Thu, 2012-04-12 at 18:36 -0700,AnuragBiyaniwrote:
> 
> > Aren't {"x": "blah", "y": null}, AND, {"x": "blah"} two different  
> > JSON obejcts ? Shouldn't a json document with a field having value  
> > null should be treated differently then a json document which is  
> > missing the field all together ?  
> > It seems that elasticsearch's "missing" filter treats json value  
> > "null" as equivalent to absence of field. Is this the desired  
> > behavior ?
> 
> From a programmer's perspective, yes, they are different. From ES'  
> perspective, they aren't.
> 
> You could work around this by providing a null\_value setting in the  
> mapping which would be indexed when the property is provided but has a  
> null value.
> 
> That, of course, assumes that the value you choose for the null\_value  
> could never occur as a real value for that property
> 
> clint
> 
> > On Mar 28, 3:31 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:
> > 
> > > Mmm, wondering if we can do that automatically and translate term query /  
> > > filter against null to missing one where applicable. Can you open an issue?
> 
> > > On Tue, Mar 27, 2012 at 9:58 PM, Ali Loghmani [loghm...@gmail.com](mailto:loghm...@gmail.com) wrote:
> > > 
> > > > cool, easy 😉
> 
> > > > On Monday, March 26, 2012 6:47:53 PM UTC-7, Ali Loghmani wrote:
> 
> > > > > I am trying to run a query like below:  
> > > > > {"query" : {"term" : { "my\_field": null } } }
> 
> > > > > but it seems that it is not working, it does not return anything (using  
> > > > > the curl command and also the HEAD plugin).
> 
> > > > > Thanks,  
> > > > > Ali

---

<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, 3:32am UTC](https://discuss.elastic.co/t/how-to-query-documents-for-a-field-to-be-null/7137/8 "2017-07-06T03:32:38Z")

</div>


