I'm using ElasticSearch (elasticsearch-dsl) and searching for keywords, but would like to ignore some phrases from search. E.g. searching for "best"
and ignore phrase "best regards"
:
it should find document "The Best
Car bla-bla-bla Best Regards" (first match is correct, second one ignored), but shouldn't match "Bla-bla-bla Best Regards".
Ideally it should also highlight only actual match without words in ignore phrase.
I'm thinking about kicking 2 queries and then removing results of ignore phrase from actual search. Is it somehow possible to get a list of matched tokens with their positions? Or is there any better approach? I can't add stop phrases to index, because search is dynamic, and ignore phrases vary from user to user.
Asked
Active
Viewed 2,892 times
3

Alexey
- 158
- 3
- 10
1 Answers
2
I'm not sure i quite fully understand your approach however it sounds like you want to add to your query a must not query combined with an exact phrase match. it would look a little something like this:
{
"query": {
"bool" : {
"must" : {
"term" : { "text_field" : "best" }
},
"must_not" : {
query_string: {default_field: "text_field", query: '\"best regards\"'}
},
}
}
if you have multiple phrases to exclude the string syntax is:
'\"text"\ OR \"text1"\ OR ... \"textn\"'
also it took me some time to realise how powerful elasticsearch custom analyzers are, i'm not sure exactly how your data looks and what your needs are but if you haven't you should take a look into it, it might save you a lot of trouble and make your queries more efficient.

Tom Slabbaert
- 21,288
- 10
- 30
- 43