# Must\_not exists Query not Working Correctly

**URL:** https://discuss.elastic.co/t/must-not-exists-query-not-working-correctly/70157
**Category:** Elasticsearch
**Created:** [December 28, 2016, 7:24pm UTC](https://discuss.elastic.co/t/must-not-exists-query-not-working-correctly/70157 "2016-12-28T19:24:25Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![krmathieu](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@krmathieu](https://discuss.elastic.co/u/krmathieu)
#### Post date: [December 28, 2016, 7:24pm UTC](https://discuss.elastic.co/t/must-not-exists-query-not-working-correctly/70157/1 "2016-12-28T19:24:26Z")

</div>

I am running the following query against the count end point trying to get a count of all of the documents where the hq field is either missing or empty and I am getting a count of zero back even though I know there are lots of records where there is no value for hq:

{  
"query": {  
"bool": {  
"must\_not": {  
"exists": {  
"field": hq  
}  
}  
}  
}  
}

In my index it appears that the hq field is present whether or not the field is actually populated and I see stuff like this :  
......,  
"hq": "",  
......., etc

I thought that the must\_not exists type of query would find documents where the field was outright missing OR empty, is that not the case? How can I get this query to work correctly? Do I need to reindex and treat the hq field differently? Right now it is just a string. Any help would be appreciated. Thanks.

Kevin

---

<div class="post-metadata">

### Author: ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)
#### Post date: [December 28, 2016, 8:25pm UTC](https://discuss.elastic.co/t/must-not-exists-query-not-working-correctly/70157/2 "2016-12-28T20:25:38Z")

</div>

Doc says :

```
Exists Query
Returns documents that have at least one non-null value in the original field:
An empty string is a non-null value.

```

So I'll say that you have to set "null" as value or default value in your mapping.

---

<div class="post-metadata">

### Author: ![krmathieu](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@krmathieu](https://discuss.elastic.co/u/krmathieu)
#### Post date: [December 28, 2016, 8:39pm UTC](https://discuss.elastic.co/t/must-not-exists-query-not-working-correctly/70157/3 "2016-12-28T20:39:28Z")

</div>

Ok, got it. Thanks for the clarification and suggestion.

Kevin

---

<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: [January 25, 2017, 8:39pm UTC](https://discuss.elastic.co/t/must-not-exists-query-not-working-correctly/70157/4 "2017-01-25T20:39:40Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
