Search and Elasticsearch learning path Level 2 This level · 2 of 18
Medium search elasticsearch

How do you add typo tolerance without making every search slow?

The 30 second answer

Fuzzy matching expands each word into terms within an edit distance, then searches all of them, so cost grows with the number of expansions. I use fuzziness AUTO, which allows no edits for words up to two characters, one for three to five, and two above five. I set prefix_length so the first characters must match, keep max_expansions low, and often run fuzzy only when the exact query finds little.

Could you say this in an interview?

What fuzzy means. An edit is inserting, deleting or changing one character, or swapping two neighbours (transpositions are on by default). "sneakres" is one swap away from "sneakers". A fuzzy query finds all indexed terms within N edits of the shopper's word, then searches for all of them.

Why it costs. The work is in the expansion step: walking the terms dictionary to find candidates and then querying each one. The docs warn: "Avoid using a high value in the max_expansions parameter, especially if the prefix_length parameter value is 0", because of "the high number of variations examined". Defaults are max_expansions: 50 and prefix_length: 0.

**AUTO.** The docs recommend fuzziness: AUTO, which is AUTO:3,6: terms of length 0 to 2 must match exactly, 3 to 5 allow one edit, longer than 5 allow two.

Badly written. A fixed edit distance of 2 on every word, no prefix, across many fields. Short words like "red" match "bed", "rod" and "ref":

{ "query": { "multi_match": { "query": "red sneakres",
    "fields": ["name", "brand", "description"], "fuzziness": 2 } } }

Better. AUTO, a required prefix, fewer expansions and fewer fields, with exact matches still ranked above fuzzy ones:

GET products/_search
{ "query": { "bool": {
    "should": [
      { "match": { "name": { "query": "red sneakres", "operator": "and", "boost": 3 } } },
      { "match": { "name": { "query": "red sneakres", "operator": "and",
                             "fuzziness": "AUTO", "prefix_length": 1, "max_expansions": 20 } } }
    ],
    "minimum_should_match": 1,
    "filter": [ { "term": { "market": "de" } } ]
}}}

Two-step alternative. Run the exact query first. Only if it returns fewer than N hits, run the fuzzy version or a "did you mean" suggestion. Most traffic then never pays for fuzziness. The cost is a second round trip for the minority of searches with typos.

Trade-offs.

  • prefix_length 1 or 2 cuts expansions a lot but misses typos in the first letters.
  • Fuzziness on brand names and IDs often does more harm than good ("Nike" versus "Mike"). Apply it to descriptive fields only.
  • A spelling-correction step in query understanding, trained on query logs, is often better than fuzziness in the engine.

Alternate interview scenarios

"Latency doubled after the team enabled fuzziness on all fields. What do you change first?"

Measure with the Profile API, then restrict fuzziness to one or two fields, add prefix_length, lower max_expansions, and move to fuzzy-only-on-low-results.

"Shoppers type 'tshirt', 't shirt' and 't-shirt'. Is fuzziness the fix?"

No, that's tokenization. Fix it in analysis, with a word-delimiter or synonym rule, so all forms produce the same tokens.

Sources (3)
  1. Elasticsearch docs: Fuzzy query (max_expansions, prefix_length, transpositions)elastic.co
  2. Elasticsearch docs: Common options (fuzziness AUTO)elastic.co
  3. Elasticsearch docs: Match query (fuzziness)elastic.co
Previous
Next