Boolean Search with Synonym Filter

I'm trying to combine Boolean search using the "must" statement that takes into account the synonyms, currently set up in a synonyms text file during indexing as follows-
Indexing:
data='{"settings": {"index":{"analysis":{"analyzer":{"synonym":{"tokenizer":"whitespace","filter":["synonym"]}},"filter":{"synonym":{"type":"synonym","synonyms_path":"analysis/med_syn.txt"}}}}}}'`
response = requests.put('http://localhost:9200/all_articles_synonyms', headers=headers, data=data)

Search: The following statements work well
Synonym search:
data="{'query': {'match': {'article': {'query': 'search_term1', 'analyzer': 'synonym'}}}}"
Boolean search-individual terms:
data_now={'query': {'bool': {'must': [{'term': {'article': 'search_term1'}}, {'term': {'article': 'search_term2'}}]}}}
Boolean search as list:
data='{"query": {"bool": {"must": [{"terms": {"article":["search_term2", "search_term1"]}}]}}}'

What I'm trying to achieve is a combination of both as in:
data='{"query": {"bool": {"must": [{"terms": {"article":["search_term1","search_term2"], "analyzer": "synonym"}}]}}}'
But this statement gets: >

'{"error":{"root_cause":[{"type":"parsing_exception","reason":"[terms] query does not support [analyzer]","line":1,"col":107}],"type":"parsing_exception","reason":"[terms] query does not support [analyzer]","line":1,"col":107},"status":400}'

Can I rewrite this statement to get the Boolean search results with the synonyms filter? Is there another way I should follow?

Both term and terms query are supposed to be used for exact matching, not analyzed text search. Your example uses a synonym analyzer, this will not be used in those queries. Use a match query like in the working example instead. It also works in bool clauses.

1 Like

Thanks Christoph, problem solved with yours and this answer. The format I'm using is:
{
"query": {
"bool":{
"must":[
{
"match": {
"article": {
"query": "search_term1",
"analyzer": "synonym"
}
}
},
{
"match":{
"article": {
"query": "search_term2",
"analyzer": "synonym"
}
}
}
]
}
}
}

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