# Must\_not field exists query returning documents with those fields excluded

**URL:** https://discuss.elastic.co/t/must-not-field-exists-query-returning-documents-with-those-fields-excluded/132155
**Category:** Elasticsearch
**Created:** [May 16, 2018, 2:43pm UTC](https://discuss.elastic.co/t/must-not-field-exists-query-returning-documents-with-those-fields-excluded/132155 "2018-05-16T14:43:38Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![SergiSolet](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@SergiSolet](https://discuss.elastic.co/u/SergiSolet)
#### Post date: [May 16, 2018, 2:43pm UTC](https://discuss.elastic.co/t/must-not-field-exists-query-returning-documents-with-those-fields-excluded/132155/1 "2018-05-16T14:43:38Z")

</div>

Hi all, I don't know if it is a bug, but it seems to me or I am missing something maybe too.  
I am performing a search query using the rest API of ES. I am using 5.6.8, the body query is the following (the involved part):

{  
"query": {  
"bool": {  
"must\_not": [  
{  
"exists": {  
"field": "event.KPI.TimeE1.Value"  
}  
},  
{  
"exists": {  
"field": " **event.KPI.TimeE2.Value**"  
}  
}  
]....

in return Elasticsearch give a document with one or more of those fields that in theory I am excluding:  
"\_source": {  
"@timestamp": "2018-05-15T05:46:22.733Z",  
"@version": "1",  
" **event**": {  
" **KPI**": {  
" **TimeE2**": {  
" **Value**": "2018-05-15T05:46:20.000+0000"  
},.....

Is this because the must\_not clause is applying an OR operator instead of an AND? Can achieve the same result in Lucene Expression? I have tried but I couldn't.

---

<div class="post-metadata">

### Author: ![RahulD](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@RahulD](https://discuss.elastic.co/u/RahulD)
#### Post date: [May 16, 2018, 8:04pm UTC](https://discuss.elastic.co/t/must-not-field-exists-query-returning-documents-with-those-fields-excluded/132155/2 "2018-05-16T20:04:36Z")

</div>

You should try rewriting the query as follows:

```
"query" : {
  "bool" : {
    "must" : [
        {
            "bool" : {
				"must_not" : [
					{
						#first clause
					}
				]
			}
        },
		{
			"bool" : {
				"must_not" : [
					{
						#second clause
					}
				]
			}
		}
    ]
}
}

```

Hope that helps.

---

<div class="post-metadata">

### Author: ![SergiSolet](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@SergiSolet](https://discuss.elastic.co/u/SergiSolet)
#### Post date: [May 18, 2018, 11:12am UTC](https://discuss.elastic.co/t/must-not-field-exists-query-returning-documents-with-those-fields-excluded/132155/3 "2018-05-18T11:12:24Z")

</div>

Worked perfectly, thank you Rahul, many thanks..

---

<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: [June 15, 2018, 11:12am UTC](https://discuss.elastic.co/t/must-not-field-exists-query-returning-documents-with-those-fields-excluded/132155/4 "2018-06-15T11:12:37Z")

</div>

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