# Must\_not but include some data

**URL:** https://discuss.elastic.co/t/must-not-but-include-some-data/219753
**Category:** Elasticsearch
**Created:** [February 18, 2020, 10:02am UTC](https://discuss.elastic.co/t/must-not-but-include-some-data/219753 "2020-02-18T10:02:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![grant\_donovan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/grant_donovan/32/42314_2.png) [@grant\_donovan](https://discuss.elastic.co/u/grant_donovan)
#### Post date: [February 18, 2020, 10:02am UTC](https://discuss.elastic.co/t/must-not-but-include-some-data/219753/1 "2020-02-18T10:02:34Z")

</div>

I am trying to do the following, exclude data that matches restricted\_general\_calculated = "all" unless another field(s) = something

This is the must\_not

```
{
  "query": {
"bool": {
  "must_not": [
    {
      "terms": {
        "restricted_general_calculated": [
          "all"
        ]
      }
    }
  ],
  "must": []
}
  }
}

```

I then want to use this to include records even if restricted\_general\_calculated="all"

```
    {
      "nested": {
        "path": "salesRights",
        "query": {
          "nested": {
            "path": "salesRights.salesRestriction",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "salesRights.salesRestriction.salesRestrictionType": "04"
                    }
                  },
                  {
                    "match": {
                      "salesRights.salesRestriction.salesOutletName": "www.mysite.co.uk"
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }

```

I tried various combinations but have just got myself into a muddle

Any help would be appreciated

Thanks

Grant

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [February 18, 2020, 10:08am UTC](https://discuss.elastic.co/t/must-not-but-include-some-data/219753/2 "2020-02-18T10:08:57Z")

</div>

You need two bools then - the first has the `should` clause to represent the OR that is the root of your logic.  
Rough structure is:

```
bool
    should
         field = something
         bool
            must_not
                restricted_general_calculated = all
```

---

<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: [March 17, 2020, 10:09am UTC](https://discuss.elastic.co/t/must-not-but-include-some-data/219753/3 "2020-03-17T10:09:04Z")

</div>

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