Having a hard time writing a query to exclude results

Hello!

I'm having a hard time writing a query to exclude certain results. I want to be able to search the index for one or many job titles, but also exclude one or many job titles from the results.

For example, I want to find all the contacts that have the job title Engineer, but exclude the ones that have the job title Software Engineer. The problem is that the query I wrote now also excludes Principal Software Engineer and I don't want that to happen.

I also tried using match_phrase instead of match, but this didn't fix the problem.

The query that I have so far is as following:

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "job_title": {
                    "query": "Engineer",
                    "operator": "and"
                  }
                }
              }
            ]
          }
        }
      ],
      "filter": [
        {
          "term": {
            "user_id": 1
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "match": {
                  "job_title": "Software Engineer"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

The index is currently populated with the following data:

{
  "data": [
    {
      "email": "chris@example.com",
      "job_title": "Industrial Electrical Engineer"
    },
    {
      "email": "esther@example.com",
      "job_title": "Chief Revenue Officer"
    },
    {
      "email": "george@example.com",
      "job_title": "Principal Software Engineer"
    },
    {
      "email": "helena@example.com",
      "job_title": "Software Engineer"
    },
    {
      "email": "john@example.com",
      "job_title": "Engineer"
    },
    {
      "email": "judith@example.com",
      "job_title": "Design Engineer"
    },
    {
      "email": "katy@example.com",
      "job_title": "Software Designer"
    },
    {
      "email": "mark@example.com",
      "job_title": "Engineer"
    },
    {
      "email": "mary@example.com",
      "job_title": "Mechanical Design Engineer"
    },
    {
      "email": "tom@example.com",
      "job_title": "Electrical Engineer"
    },
    {
      "email": "tony@example.com",
      "job_title": "Chief Executive Officer"
    }
  ]
}

Any help is highly appreciated. Thanks

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