Synonym Search in Elasticsearch and Alternatives

Berkay Yalçın28 Mar 2022

Problem

Trendyol's Çözüm Ortağım site lets customers search for campaigns. Customers type a synonym of the word used in the campaign, and a plain full-text search returns nothing for it. The team first measured how many users search with synonyms. Customers did, so synonym support was added.

Example from the article: a user types "petrol" and should find "gasoline" promotions. Without a synonym filter the search for "petrol" finds nothing, which is a poor experience.

Solution in brief

Check from search behavior that synonym queries are common, then add a synonym filter to the search-time analyzer of an Elasticsearch index. The article also describes an alternative: a key-value store (Redis, Couchbase or an in-memory cache) is checked first, and Elasticsearch full-text search runs only when no synonym matches.

Questions this case answers
  • How to make keyword search return documents for queries that use a different word with the same meaning
  • How to keep a query-expansion dictionary editable in production without rebuilding the search index
  • How to make a product or campaign search find items when users type a synonym of the word in the catalog?
  • How to decide whether synonym support is worth building before changing the search engine?

Tools and methods

  • Elasticsearch synonym filter (synonym_graph) — rewrites a query term into its defined synonyms inside a search analyzer (asciifolding, trim, stemmer, synonyms)
  • Synonym file on each node — keeps the synonym list editable without dropping the index
  • Key-value store (Redis, Couchbase, in-memory cache) — optional first lookup for synonym matches
  • Strategy pattern — two interchangeable strategies, SynonymSearchStrategy and FullTextSearchStrategy
Continue reading — it's free

Google or a one-time email code. No password.