Related search in elasticsearch


(Anas Barik) #1

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?


(David Pilato) #2

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


(Anas Barik) #3

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


(David Pilato) #4

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.


(Anas Barik) #5

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 ?


(David Pilato) #6

No. One single call should be enough.


(Anas Barik) #7

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


(David Pilato) #8

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.


(system) closed #9

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