I'm trying to write an ElasticSearch query that allows for filtering the results set. The application provides a filter for job titles and also an exclusion filter for the very same job titles. So for example, in the data set bellow, I want to filter for Engineer, but also exclude Software Engineer. The problem is that now the query also excludes Principal Software Engineer and it shoudn't.
Here's the data I'm using:
{
"data": [
{
"email": "[email protected]",
"job_title": "Industrial Electrical Engineer"
},
{
"email": "[email protected]",
"job_title": "Chief Revenue Officer"
},
{
"email": "[email protected]",
"job_title": "Principal Software Engineer"
},
{
"email": "[email protected]",
"job_title": "Software Engineer"
},
{
"email": "[email protected]",
"job_title": "Engineer"
},
{
"email": "[email protected]",
"job_title": "Design Engineer"
},
{
"email": "[email protected]",
"job_title": "Software Designer"
},
{
"email": "[email protected]",
"job_title": "Engineer"
},
{
"email": "[email protected]",
"job_title": "Mechanical Design Engineer"
},
{
"email": "[email protected]",
"job_title": "Electrical Engineer"
},
{
"email": "[email protected]",
"job_title": "Chief Executive Officer"
}
]
}
And here is the ElasticSearch query:
{
"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"
}
}
]
}
}
]
}
}
}