A Search That Forgives Typos: How Typo Tolerance Actually Works

by Luca Cavallini Technical guides
A Search That Forgives Typos: How Typo Tolerance Actually Works

The problem, stated precisely

A shopper who searches for "hummingbrid" almost certainly means "hummingbird". A search box that returns zero results on that query is not being strict for a good reason, it is failing at the one thing a search box exists to do: find what the person meant, not just what they typed. Typo tolerance is the general name for the mechanisms that close that gap. It sounds like one feature. It is actually at least two different mechanisms, with different costs and different failure modes, and mixing them up is where most naive implementations go wrong.

The building block: edit distance

Almost every typo-tolerance mechanism, whatever the engine, is built on the same underlying idea: the edit distance between two strings, most commonly the Levenshtein distance, which counts the minimum number of single-character insertions, deletions or substitutions needed to turn one string into the other. "Hummingbrid" is one transposition away from "hummingbird" (two edits under the plain Levenshtein definition, one under the Damerau-Levenshtein variant that also counts a transposition as a single edit). "Cat" and "hat" are one edit apart. The number by itself does not decide anything; what a system does with it does.

The detail worth getting right from the start: an edit distance of 1 or 2 is generous for a long word and dangerous for a short one. Two edits on an eight-letter word barely changes it. Two edits on a three-letter word can turn it into a completely different word: "car" and "cat" are one edit apart, but so are "car" and "far", and so are "car" and "can". A typo-tolerance system that applies the same distance threshold regardless of word length will happily "correct" short, distinct words into each other. The usual, honest fix is a length-scaled threshold: no correction on very short terms, one edit allowed from a handful of characters up, two edits only once the word is long enough that two edits still leaves most of it recognisable.

Two different mechanisms, often confused as one feature

"Typo tolerance" usually means one of two genuinely different things, and which one a system offers changes what it can and cannot do.

1. Fuzzy matching at query time

Elasticsearch, OpenSearch and similar engines can run a fuzzy query (or the fuzziness parameter on a match query) that compares the search term against indexed terms using edit distance at query time, without needing to know in advance which misspellings exist. This works because the engine already holds an inverted index of every term appearing in the catalog; a fuzzy query is, roughly, "find indexed terms within N edits of this one, and score matches by how close they are." It needs no separate maintenance step and it degrades gracefully: a slightly-off query still ranks the closest indexed terms highest. Its real cost shows up on a high-cardinality field or a very large vocabulary, where computing edit distance against a large set of candidate terms is heavier than an exact term lookup, which is why fuzzy matching is usually applied selectively (the main free-text field, not every keyword field) rather than everywhere by default.

2. Dictionary-based correction, applied only when needed

A system without a search engine that scores fuzzy matches natively, for instance a plain SQL-backed search, has to build the mechanism itself: a dictionary of known-good terms, built from the catalog's own product and brand names, against which a search term that returned nothing can be compared to find the closest real word. The dictionary itself is commonly indexed by character trigrams (three-letter fragments) rather than compared letter by letter against every word, both because a trigram index narrows the candidate list to plausibly-close words before computing exact edit distance on a much smaller set, and because it needs no external engine at all.

The detail that makes this approach cheap in practice, rather than a performance trap, is when the lookup runs. If every search paid the cost of a dictionary lookup, typo tolerance would slow down every query, including the ones that were typed correctly and already found results. The lookup only has a reason to run once the exact search has already come back empty: a correct query never touches it, and only the zero-result case pays the extra cost. Measured on a real catalog, a correctly spelled query and a misspelt one that triggers the dictionary lookup took the same, small amount of time either way (both in the 0.16 to 0.19 second range on that particular setup): the dictionary path only adds cost to searches that would otherwise have returned nothing at all.

The dictionary also needs upkeep that a fuzzy query does not: it has to learn new words as the catalog grows and forget words that belonged to products that no longer exist, or it slowly drifts out of date and starts suggesting corrections toward discontinued products. A dictionary rebuilt on a schedule (for instance weekly, plus a lazy top-up the first time a zero-result search needs a word it does not have yet) keeps it aligned with the catalog without rebuilding it on every single request.

Where the easy version breaks

  • A flat edit-distance threshold on words of every length, as above: generous correction on short words turns distinct words into each other.
  • Correcting a query that already has results. If a search that already found products also gets "corrected" toward a different, more popular term, the shopper can be redirected away from what they actually typed and toward whatever the engine considers close and popular. Correction belongs after a genuine zero-result search, not as a constant background rewrite of every query.
  • A correction that quietly fires when it should not. On a faceted search page, a filter selection can legitimately produce zero results (a shopper picked a combination of size and colour nobody stocks). That is not a spelling problem, and a typo-tolerance layer that cannot tell the difference will try to "correct" a perfectly well-spelt search term because the filters, not the word, emptied the page.
  • Trusting fuzzy matching to also solve facet correctness. Making a misspelt query still find products is a different problem from counting how many products match each filter value correctly. A typo-tolerant engine that finds results for "hummingbrid" will confidently compute facet counts for that same fuzzy match; if anything downstream (an autocomplete list of "popular searches", for instance) assumes "found at least one product" means "this was a real, correctly spelt term", it will happily publish the store's own typos back to every visitor. A typo-tolerant engine being good at its job is exactly what makes that particular mistake easy to make elsewhere in the same system.
  • Silence instead of transparency. A search that quietly substitutes a different term than the one typed, with no indication that a substitution happened, leaves a shopper wondering why the results don't quite match what they searched for. A visible, honest line, "No results for X. Showing results for Y instead", costs one sentence and avoids that confusion entirely.

A short checklist

  • Scale the allowed edit distance to word length; do not apply the same threshold to a three-letter word and a twelve-letter one.
  • Only correct after a genuine zero-result search, never as a silent rewrite of a query that already found something.
  • Distinguish "the word was misspelt" from "the filters emptied the page": they need different handling, and conflating them produces a correction attempt where none was warranted.
  • If you maintain your own dictionary instead of relying on an engine's built-in fuzzy scoring, keep it current: rebuild it on a schedule and let it learn from the catalog as it changes, or it will start suggesting words that no longer exist.
  • Tell the shopper when a substitution happened. Do not let a "successful" fuzzy match hide the fact that what they typed and what they got are not the same word.

In short

Typo tolerance is not one on/off setting, it is a choice between two different mechanisms, engine-native fuzzy matching at query time or a maintained dictionary consulted only on a genuine zero-result search, each with its own cost and its own way of quietly getting it wrong. The two mistakes worth watching for are correcting words that were never actually misspelt, and trusting a fuzzy match to answer questions it was never built to answer, like an exact facet count. We handle this inside cll_essearch, our PrestaShop search and faceted-navigation module, both with Elasticsearch's native fuzzy matching and with our own trigram-based dictionary for shops running the built-in database driver, if you want to see the distinction applied to an actual online store.

Elasticsearch Search & Faceted NavigationLightning-fast, typo-tolerant search and modern modal faceted navigation with the built-in engine, or powered…€99.99View product

Leave a comment

Comments are moderated before publication.

← Blog

Loading...
Back to top