Must_not is not excluding results when used in query

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
}

You have a must_not within a must_not, so a double negative which turns into a positive.

1 Like

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
}

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