Elasticsearch Fuzzy Search and Autocomplete
On this page
Why search needs typo tolerance
A user types wireles mose. A strict search finds nothing, and the user assumes you don't sell mice. Fuzzy matching, prefix matching and autocomplete turn near-misses into results.
Fuzzy matching and edit distance
fuzziness allows terms within a few edits (insertions, deletions, substitutions, transpositions) of the query term. "AUTO" allows 0 edits for 1 to 2 characters, 1 for 3 to 5, and 2 for longer terms.
Run against the course's product index, where name uses the english analyzer:
GET /products/_search
{ "query": { "match": { "name": { "query": "wireles mose", "fuzziness": "AUTO" } } } }total 0Nothing, even with fuzziness. The analyzer explains why:
english analyzer: "wireless mouse wireles mose" -> ['wireless', 'mous', 'wirel', 'mose']The stemmer turned the misspelled wireles into wirel, now three edits from wireless, beyond the limit. Fuzzy matching runs on analyzed terms, and stemming distorted the typo.
The fix is a subfield with lighter analysis. name.suggest uses the standard analyzer:
GET /products/_search
{ "query": { "match": { "name.suggest": { "query": "wireles mose", "fuzziness": "AUTO", "operator": "and" } } } }total 1
101 2.438 Wireless MouseFuzzy matching on a non-stemmed field finds exactly the intended product. Without "operator": "and" it returned four products, with Wireless Mouse first. Combine both fields in a bool should so exact stemmed matches and fuzzy matches each contribute.
Keep fuzziness for text the user typed. Fuzzy queries are costlier than exact ones; prefix_length: 1 (the first character must match) cuts that cost a lot.
Autocomplete with search_as_you_type
A search_as_you_type field (here the name.suggest subfield, Day 5) indexes the text with extra subfields of word pairs and triples (shingles) and edge n-grams, so partial input matches efficiently:
GET /products/_search
{ "query": { "multi_match": { "query": "wir", "type": "bool_prefix",
"fields": ["name.suggest", "name.suggest._2gram", "name.suggest._3gram"] } } }total 3
101 1.000 Wireless Mouse
102 1.000 Wireless Keyboard
112 1.000 Wireless EarbudsAnd with more words, the shingle subfields keep results tight: noise canc returned only "Noise Cancelling Headphones". Add popularity scoring (Day 10) so the best sellers show first among equal prefixes.
Alternatives:
completionsuggester: an in-memory structure, extremely fast for pure prefix suggestions, but less flexible (no filters beyond contexts, rebuilds on reindex).edge_ngramanalyzers: full control, more index space.
Wildcard and regexp queries, and their cost
{ "wildcard": { "name.keyword": { "value": "*mouse*" } } }Leading wildcards must walk the whole term dictionary: slow on large indices, and dangerous on user input. Prefer prefix-friendly fields, n-grams, or the wildcard field type (built for log-style "contains" searches). Many clusters block expensive queries with search.allow_expensive_queries: false.
Check yourself
The stemmer turned "wireles" into "wirel", which is three edits from the indexed term "wireless", beyond the two edits that fuzziness: AUTO allows for terms of that length. Fuzzy queries work better on a lightly analyzed subfield.
Shingle subfields of two and three consecutive words plus an edge n-gram subfield of prefixes. Prefixes and partial phrases then match as ordinary terms, so autocomplete stays fast without expensive prefix or wildcard queries at search time.
A pattern like *phone can't use the sorted term dictionary, so Elasticsearch scans every term, which is slow on large indices and lets users trigger expensive queries. Use n-grams or the wildcard field type instead.
No edits for terms up to 2 characters, one edit for 3 to 5 characters, and two edits for longer terms, balancing typo tolerance with precision.
Requiring the first one or two characters to match exactly greatly reduces the terms checked, making fuzzy queries faster with little loss.