# Using must and must\_not on same field

**URL:** https://discuss.elastic.co/t/using-must-and-must-not-on-same-field/152656
**Category:** Elasticsearch
**Created:** [October 16, 2018, 12:34pm UTC](https://discuss.elastic.co/t/using-must-and-must-not-on-same-field/152656 "2018-10-16T12:34:35Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![prasad.ram1431](https://avatars.discourse-cdn.com/v4/letter/p/c89c15/32.png) [@prasad.ram1431](https://discuss.elastic.co/u/prasad.ram1431)
#### Post date: [October 16, 2018, 12:34pm UTC](https://discuss.elastic.co/t/using-must-and-must-not-on-same-field/152656/1 "2018-10-16T12:34:35Z")

</div>

Hi All,

I have a requirement like, need to extract documents based on two fields and out of those two fields i need to exclude one field(optional), and extract data. So for this requirement below is my ES query.

```
{
"query": {
    "bool": {
        "must": [
            {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "keys": {
                                    "value": "216340000",
                                    "boost": 1
                                }
                            }
                        },
                        {
                            "term": {
                                "code": {
                                    "value": 1067,
                                    "boost": 1
                                }
                            }
                        }
                    ],
                    "adjust_pure_negative": true,
                    "boost": 1
                }
            },
            {
                "bool": {
                    "must_not": [
                        {
                            "term": {
                                "keys": {
                                    "value": "216340000",
                                    "boost": 1
                                }
                            }
                        }
                    ],
                    "adjust_pure_negative": true,
                    "boost": 1
                }
            }
        ],
        "adjust_pure_negative": true,
        "boost": 1
    }
}

```

}

When I am executing above query, I am getting zero count. But I am suppose to get some count like 20+ count.

Could someone suggest me on my query whether I have written write or I did any mistake in query.

Thanks,  
Ram Prasad G

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [October 17, 2018, 7:27am UTC](https://discuss.elastic.co/t/using-must-and-must-not-on-same-field/152656/2 "2018-10-17T07:27:06Z")

</div>

I'm not sure I fully understand the query. You want the field `keys` to have the value `216340000` and also to not have that value? That seems contradictory unless I misread the query.

Can you show a sample document that should match this?

---

<div class="post-metadata">

### Author: ![prasad.ram1431](https://avatars.discourse-cdn.com/v4/letter/p/c89c15/32.png) [@prasad.ram1431](https://discuss.elastic.co/u/prasad.ram1431)
#### Post date: [October 22, 2018, 9:51am UTC](https://discuss.elastic.co/t/using-must-and-must-not-on-same-field/152656/3 "2018-10-22T09:51:41Z")

</div>

Its a kind of contradictory, but not as per the requirement. User will be having two different kinds of criterias, one is match criteria and other is exclude criteria. So, there is a chance of selecting same field in both the criteria's so, the above query i have written. Thats the reason I have given complete query with match query with two fields and must\_not with one field.

Please let me know if we can do this in ES.

---

<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: [November 19, 2018, 9:52am UTC](https://discuss.elastic.co/t/using-must-and-must-not-on-same-field/152656/4 "2018-11-19T09:52:55Z")

</div>

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