How to chain synonyms in Elasticsearch

In my Application I want to create the following hierarchy:

hobby -> sport -> football

When looking for the word hobby, I want to check for the words sport and football as well, when looking for the word sport I also want to check for the word football but not for hobby and when looking for the word football, I just want to check for that word and none of the others.

This is what my attempt looks like so far:

"synonyms": [
  "sport => hobby",
  "football => sport"
]

How Elasticsearch-Synonyms work is described here: Synonym token filter | Elasticsearch Guide [7.12] | Elastic

But the important part really is, that all terms on the left-hand side will be replaced with all terms on the right-hand side. When I look for the term hobby I indeed get results for sport as well and when I look for sport I also get results for football, but the problem is that the synonyms don't chain. Meaning that if I look for hobby i also want results for football, which I don't.

Of course the easy way out would be to just type the following:

"synonyms": [
  "sport, football => hobby",
  "football => sport"
]

But this would be A) redundant and B) a bad solution since the final hierarchy is going to be way larger than just this example and putting all underlying alternatives in front of hobby seems very messy and not like the intended way to solve this.

I've also tried:

"synonyms": [
  "sport => sport, hobby",
  "football => football, sport"
]

But that didn't work either.

I am looking forward for your answers! :slight_smile:

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