# Having a hard time writing a query to exclude results

**URL:** <https://discuss.elastic.co/t/having-a-hard-time-writing-a-query-to-exclude-results/275186>\
**Category:** Elasticsearch\
**Created:** [June 7, 2021, 6:25pm UTC](https://discuss.elastic.co/t/having-a-hard-time-writing-a-query-to-exclude-results/275186 "2021-06-07T18:25:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ccvanilla](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@ccvanilla](https://discuss.elastic.co/u/ccvanilla)\
**Post date:** [June 7, 2021, 6:25pm UTC](https://discuss.elastic.co/t/having-a-hard-time-writing-a-query-to-exclude-results/275186/1 "2021-06-07T18:25:00Z")

</div>

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:

```auto
{
  "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:

```auto
{
  "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

---

<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:** [July 5, 2021, 6:25pm UTC](https://discuss.elastic.co/t/having-a-hard-time-writing-a-query-to-exclude-results/275186/2 "2021-07-05T18:25:14Z")

</div>

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