Related search in elasticsearch

I'm looking for a convenient way to search for related words to a term. For example, If I search for the word "washer", I should be getting related search terms like "dryer" with the lower score thank washer results, It means the washer documents must appear the first and then dryer documents. how can I do this functionality?

Synonyms is what you are looking for. See https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-synonym-tokenfilter.html and https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-synonym-graph-tokenfilter.html

1 Like

Thank you !
And is it possible to set weights for each synonym ,
for example [ dryer:weight1, dishwasher:weight2 ... ] if I search for washer
I want to get washer results first then dryer results then dishwasher results and so on

I don't know if there is an "automatic" way to do so. But you can create a bool query with multiple should clauses. Each clause can use different fields.
Fields can be generated at index time with multiple subfields. Each subfield with its own dictionary. Then you can boost each query to make more relevant one vs the other.

I hope this makes sense.

1 Like

Yes , It makes sense, I think I should do this in two queries, the first will fetch synonyms and the second will use the previous result in a bool query with should clauses, is that true ?

No. One single call should be enough.

1 Like

could you please write a query example of that ? I couldn't figure it out !

What did you try so far?

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

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