Prompt
Building a search experience with synonyms and typo tolerance
Latest observation
Here’s a practical blueprint for building a search experience with synonyms and typo tolerance.
1) Define the search goals
Decide what “good search” means for your product:
- Recall: find results even when users use alternate wording
- Precision: avoid irrelevant matches
- Latency: keep queries fast
- Control: tune behavior per field (title vs body vs tags)
2) Core search pipeline
A typical pipeline looks like:
-
Normalize text
- lowercase
- strip punctuation
- handle accents/diacritics
- standardize whitespace
-
Analyze text
- tokenize
- stem or lemmatize if appropriate
- apply synonyms
- optionally edge n-grams or autocomplete index
-
Query expansion / matching
- synonym expansion
- fuzzy matching / edit distance
- phonetic matching for names if needed
-
Ranking
- exact matches > synonym matches > fuzzy matches
- boost important fields
- use popularity, freshness, click-through, etc.
3) Synonyms
Approaches
A. Index-time synonyms
- Expand documents when indexing.
- Example: “tv” becomes “television” and vice versa.
- Pros: faster queries
- Cons: harder to update synonyms; can bloat index; tricky phrase handling
B. Query-time synonyms
- Expand user query at search time.
- Example: query “tv stand” also searches for “television stand”
- Pros: easier to update; more flexible
- Cons: query gets more complex; can impact latency
Best practices
- Use a curated synonym list for important terms
- Support:
- one-way synonyms:
laptop => notebook - equivalent synonyms:
soda, pop, soft drink
- one-way synonyms:
- Be careful with ambiguous terms:
- “apple” could mean fruit or company
- Test synonyms per category/vertical, not globally if possible
Example synonym groups
sofa, couchtv, televisioncellphone, mobile, smartphonenyc, new york city
4) Typo tolerance
Typo tolerance helps users who misspell queries.
Common techniques
A. Fuzzy matching / edit distance
- Match terms within 1–2 edits
- Example: “iphnoe” → “iphone”
B. N-grams
- Index character n-grams for approximate matching
- Useful for short queries and autocomplete
- Can increase index size significantly
C. Spell correction
- Detect likely misspellings and suggest corrections
- Can be separate from retrieval
- Often used with “did you mean?”
Best practices
- Apply typo tolerance more aggressively to:
- short queries
- names/brands
- user-generated text
- Be conservative for:
- exact IDs
- codes
- technical terms
- Avoid fuzzy matching on all fields; usually limit it to title/name fields
Example
Query: wirless earbuds
- Correct to
wireless earbuds - Retrieve exact/fuzzy matches and maybe suggest correction
5) Combine synonyms + typo tolerance carefully
These two features can interact badly if over-applied.
Recommended ranking order
- Exact match
- Phrase match
- Synonym-expanded match
- Fuzzy/typo-tolerant match
Guardrails
- Don’t expand synonyms and fuzziness everywhere
- Use boosts to prefer exact terms
- Limit fuzzy expansions on high-frequency words
- Monitor for query explosion
Example:
Query: couch
- Exact result “couch” gets top boost
- “sofa” also matches via synonym
- “coach” should not appear unless fuzzy rules are very permissive
6) Field-specific search
Different fields need different treatment:
- Title/name
- synonyms + typo tolerance
- high boost
- Description/body
- synonyms, less typo tolerance
- Tags/categories
- strong exact/synonym matching
- IDs/SKUs
- no fuzziness, minimal normalization
7) Relevance tuning
You’ll likely need to tune ranking beyond lexical matching:
- exact term frequency
- field boosts
- recency
- popularity
- user behavior signals
- availability/in-stock status
- personalization
A good rule: use synonyms and typo tolerance to find results, then ranking to prioritize the best ones.
8) Evaluation
Measure whether your search is actually improving.
Metrics
- Precision@k
- Recall@k
- MRR / NDCG
- query success rate
- zero-result rate
- reformulation rate
- click-through rate
Build a test set
Include queries with:
- synonyms
- misspellings
- ambiguous terms
- brand names
- plural/singular forms
- abbreviations
Example test cases:
couch→ should find sofasiphnoe→ should find iPhonenyc hotel→ should find New York City hotelstv stand→ should find television stands
9) Operational considerations
- Maintain synonyms in version-controlled files or a management UI
- Log zero-result and low-click queries
- Add analytics to discover real synonym candidates
- Roll out changes gradually and A/B test
- Rebuild/reindex if using index-time synonym expansion
10) Suggested implementation pattern
If you’re using a search engine like Elasticsearch/OpenSearch/Algolia/Meilisearch/Solr:
- Use a custom analyzer
- Add a synonym filter
- Enable fuzzy matching selectively on query fields
- Use boosted multi-field search
- Add spell correction or “did you mean” suggestions
- Monitor relevance continuously
Simple example strategy
For product search:
title: synonym + fuzzy match, high boostbrand: exact + synonym, medium boostdescription: synonym only, low boostsku: exact only
Query example:
wireless headset
- exact match on title
- synonym:
headset = headphones / earphonesif curated - fuzzy:
wireles→wirelessif misspelled
If you want, I can also give you:
- a technical design for Elasticsearch/OpenSearch,
- a synonym + typo tolerance ranking strategy, or
- a sample implementation in code.