# Must\_not is not excluding results when used in query

**URL:** <https://discuss.elastic.co/t/must-not-is-not-excluding-results-when-used-in-query/270194>\
**Category:** Elasticsearch\
**Created:** [April 15, 2021, 7:03am UTC](https://discuss.elastic.co/t/must-not-is-not-excluding-results-when-used-in-query/270194 "2021-04-15T07:03:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![M\_Gibbs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m_gibbs/32/45665_2.png) [@M\_Gibbs](https://discuss.elastic.co/u/M_Gibbs)\
**Post date:** [April 15, 2021, 7:03am UTC](https://discuss.elastic.co/t/must-not-is-not-excluding-results-when-used-in-query/270194/1 "2021-04-15T07:03:47Z")

</div>

Hi all,

Having some issues, this is my query below. I'm returning results, but the must\_not section is not succesfully removing results. It's narrowing, to include the word developer. So I'm getting results with web designer and developer in them. Instead of removing any results that contain developer.

Can anyone tell me what I'm doing wrong?

```
{
  "query": {
    "function_score": {
      "boost_mode": "sum",
      "query": {
        "bool": {
          "must": [
            {
              "bool": {
                "should": [
                  {
                    "match": {
                      "job_title": "web designer"
                    }
                  }
                ],
                "minimum_should_match": "75%",
                "boost": 1
              }
            }
          ],
          "must_not": [
            {
              "bool": {
                "must_not": [
                  {
                    "match": {
                      "job_title": "developer"
                    }
                  }
                ]
              }
            }
          ],
          "filter": []
        }
      },
      "functions": [
        {
          "field_value_factor": {
            "field": "importance",
            "factor": 1,
            "modifier": "none",
            "missing": 1
          }
        }
      ]
    }
  },
  "track_total_hits": true,
  "size": "25",
  "from": 0
}
```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [April 15, 2021, 7:18am UTC](https://discuss.elastic.co/t/must-not-is-not-excluding-results-when-used-in-query/270194/2 "2021-04-15T07:18:32Z")

</div>

You have a must\_not within a must\_not, so a double negative which turns into a positive.

---

<div class="post-metadata">

**Author:** ![M\_Gibbs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m_gibbs/32/45665_2.png) [@M\_Gibbs](https://discuss.elastic.co/u/M_Gibbs)\
**Post date:** [April 15, 2021, 7:24am UTC](https://discuss.elastic.co/t/must-not-is-not-excluding-results-when-used-in-query/270194/3 "2021-04-15T07:24:27Z")

</div>

Thanks for your reply.

I've tried replacing the double must\_not with 'must' (the nested must\_not) and still not having much luck. Should the must\_not section be completely rewritten, shortened?

```
{
  "query": {
    "function_score": {
      "boost_mode": "sum",
      "query": {
        "bool": {
          "must": [
            {
              "bool": {
                "should": [
                  {
                    "match": {
                      "job_title": "web designer"
                    }
                  }
                ],
                "minimum_should_match": "75%",
                "boost": 1
              }
            }
          ],
          "must_not": [
            {
              "bool": {
                "must": [
                  {
                    "match": {
                      "job_title": "developer"
                    }
                  }
                ]
              }
            }
          ],
          "filter": []
        }
      },
      "functions": [
        {
          "field_value_factor": {
            "field": "importance",
            "factor": 1,
            "modifier": "none",
            "missing": 1
          }
        }
      ]
    }
  },
  "track_total_hits": true,
  "size": "25",
  "from": 0
}
```

---

<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:** [May 13, 2021, 7:24am UTC](https://discuss.elastic.co/t/must-not-is-not-excluding-results-when-used-in-query/270194/4 "2021-05-13T07:24:55Z")

</div>

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