Recipe
Recipe: Did-you-mean / spell-correct
Build a typo-tolerant search layer that suggests corrected queries when users misspell product names, categories, or commands.
Overview
This recipe wires a lightweight Levenshtein distance engine into your search pipeline. When a query returns zero results, the system scans your indexed terms, ranks candidates by edit distance, and surfaces the top suggestion with a “Did you mean?” prompt.
Ingredients
- Pre-built term index (product names, tags, slugs)
- Levenshtein distance function (max threshold: 3)
- Search endpoint with zero-result fallback path
- UI slot for suggestion display
Steps
- Index terms. Extract unique lowercase tokens from your catalog into a flat array at build time.
- Wire the distance check. On zero-result, iterate the index, compute edit distance, and collect candidates within threshold.
- Rank and surface. Sort by distance ascending, pick the best match, and return it alongside the empty result set.
- Render the prompt. Display “Did you mean corrected-term?” as a clickable link that re-issues the search.
Considerations
Keep the term index under 10k entries for sub-50ms cold-path latency. Cache frequent corrections in an LRU map. For multi-word queries, split on whitespace and score each token independently, then average the distances.