I am trying to search a word in a list of documents in elasticsearch, while excluding an expression that contain that word.
For instance I would like to search, in the following list, the word "good" but exclude the expression "quite good" in a soft way:
So that it would return 1 and 3, because it has an instance of "good" that is not contained in the expression "quite good". I tried using a BooleanQuery or a SpanNotQuery but document n°3 is excluded every time.
Another way would be to use an analyzer that would remove the expressions I do not want to run the search on. However, this makes my exclusion very static and it cannot be set at query time.
Do you know if and how this would be possible ?
My bad, SpanNotQuery actually did the trick, I must have tested it wrong in the first place.
This query give the expected result:
{
"query": {
"span_not": {
"include": {
"span_term": { "field": "good" }
},
"exclude": {
"span_near": {
"clauses": [
{ "span_term": {"field": "quite" }},
{ "span_term": {"field": "good" }}
],
"slop": 0,
"in_order": true
}
}
}
}
}