# Query help must\_not except when

**URL:** https://discuss.elastic.co/t/query-help-must-not-except-when/172935
**Category:** Elasticsearch
**Created:** [March 19, 2019, 10:03am UTC](https://discuss.elastic.co/t/query-help-must-not-except-when/172935 "2019-03-19T10:03:38Z")
**Posts on this page:** 7
**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: [March 19, 2019, 10:03am UTC](https://discuss.elastic.co/t/query-help-must-not-except-when/172935/1 "2019-03-19T10:03:38Z")

</div>

I wonder if someone would be able to help me with this query

The query works and returns results, but I want to change the must not bit, instead of

"must\_not": [ { "match": { "notificationType": "88" }}

I want to say do not include records where notificationType = 88 except if brand="Fiction" and PublicationDate=now

I don't understand how to add in that exception to the main query.

Any help would be appreciated

Thanks

Grant

```
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "pub_date_calculated": {
              "lt": "now+180d"
            }
          }
        }

      ],
      "must_not": [
        {
          "match": {
            "notificationType": "88"
          }
        },
        {
          "match": {
            "productForm": "DH"
          }
        }
   ] }
  }
}
```

---

<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 19, 2019, 10:20am UTC](https://discuss.elastic.co/t/query-help-must-not-except-when/172935/2 "2019-03-19T10:20:54Z")

</div>

I think you need to add a new `bool` query inside your `must_not` clause.  
This new `bool` query would then be a `should` array with 2 clauses:

- One for `notificationType = 88`
- Another one with a bool query containing a must\_not array with 2 clauses for:
  - `brand="Fiction"`
  - `PublicationDate=now`

HTH

---

<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: [March 19, 2019, 11:18am UTC](https://discuss.elastic.co/t/query-help-must-not-except-when/172935/3 "2019-03-19T11:18:07Z")

</div>

Thanks for the guidance, I still don't seem to be able to get this to work. I would be expecting the number of records to increase. Have I done this must\_not query correctly?

Ant help would be appreciated

Grant

```
  "must_not": [
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "notificationType": "88"
              }
            },
            {
              "query": {
                "bool": {
                  "must": [
                    {
                      "match": {
                        "brand": "Fiction"
                      }
                    },
                    {
                      "range": {
                        "pub_date_calculated": {
                          "lt": "now+280d"
                        }
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    },
    {
      "match": {
        "notificationType": "88"
      }
    },
    {
      "match": {
        "productForm": "DH"
      }
    }

```

]

---

<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 19, 2019, 11:30am UTC](https://discuss.elastic.co/t/query-help-must-not-except-when/172935/4 "2019-03-19T11:30:53Z")

</div>

I believe this because you let the following clauses:

```auto
{
      "match": {
        "notificationType": "88"
      }
    },
    {
      "match": {
        "productForm": "DH"
      }
    }

```

I'd probably remove that.

If you need further help, could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

---

<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: [March 19, 2019, 12:36pm UTC](https://discuss.elastic.co/t/query-help-must-not-except-when/172935/5 "2019-03-19T12:36:08Z")

</div>

OK, I have a simpler version below

Get records pub\_date\_calculated less than now + 1 day  
Exclude records notificationType = 88  
Include all records that have an Imprint of Avon (even if they have a notificationType = 88)

Thanks

Grant

```
productindex/_search

{
  "query": {
"bool": {
  "must": [
    {
      "range": {
        "pub_date_calculated": {
          "lt": "now+1d"
        }
      }
    }
  ],
  "must_not": [
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "notificationType": "88"
              }
            },
            {
              "query": {
                "bool": {
                  "must_not": [
                    {
                      "match": {
                        "imprint": "Avon"
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  ]
}
  }
}
```

---

<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 19, 2019, 2:03pm UTC](https://discuss.elastic.co/t/query-help-must-not-except-when/172935/6 "2019-03-19T14:03:18Z")

</div>

Does it work?

---

<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: [April 16, 2019, 2:03pm UTC](https://discuss.elastic.co/t/query-help-must-not-except-when/172935/7 "2019-04-16T14:03:25Z")

</div>

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