To handle typos in Elasticsearch, use a fuzzy query, or add fuzziness to a match query. Fuzzy matching finds terms within a maximum edit distance, the number of single-character changes needed to turn one word into another. A search for "elasticsarch" can then still find "elasticsearch".
The elasticsearch fuzzy query is controlled by the fuzziness parameter, usually set to AUTO. This guide explains edit distance, the available options and the limits that keep fuzzy searches fast.
Elasticsearch uses the Damerau-Levenshtein edit distance. One edit is any of these single-character changes:
quick to quikck.quick to quik.quick to quack.quick to qucik.A fuzziness of 1 allows one edit and a fuzziness of 2 allows two edits. The maximum allowed value is 2, because larger distances would match unrelated words.
Create a books index and a few documents.
The fuzzy query works on a single term. The text field holds lowercase tokens, so write the term in lowercase.
The misspelt term is one deletion away from elasticsearch, so document 1 matches. Note that the fuzzy query does not analyze its input, which is why the lowercase form is needed on a text field.
For user input with several words, add fuzziness to a match query. The text is analyzed first, and each resulting term is matched fuzzily.
This finds documents 1 and 2. This is the form most applications use for a search box, because it keeps analysis such as lowercasing and stemming.
With AUTO, the allowed edits depend on the term length.
| Term length | Edits allowed |
|---|---|
| 0 to 2 characters | 0 (must match exactly) |
| 3 to 5 characters | 1 |
| more than 5 characters | 2 |
You can change the thresholds with AUTO:low,high, for example AUTO:4,8. Short words are protected on purpose: with two edits, a three-letter word could match thousands of unrelated terms.
Fuzzy matching expands the query into many candidate terms, so a few parameters limit that work.
prefix_length sets how many leading characters must match exactly. A value of 1 or 2 skips most candidates and speeds up the search, and typos in the first letter are rare.max_expansions caps the number of candidate terms, which defaults to 50.fuzzy_transpositions, on by default, counts swapping two adjacent characters as one edit instead of two.Fuzzy matches are scored lower than exact ones, but a combined query makes this explicit and gives exact matches an advantage.
The first clause rewards exact matches, while the second one catches typos.
Fuzzy queries fix spelling mistakes but not different words, so "car" will not find "automobile". For that, use synonyms. For "did you mean" corrections, use the term or phrase suggesters. For autocomplete while typing, use search_as_you_type or the completion suggester. Also remember that, like other expensive queries, fuzzy queries can be blocked when search.allow_expensive_queries is false.
The maximum is 2 edits. Higher values are not accepted.
Use match with fuzziness for user input, because it analyzes the text. Use fuzzy only for a single raw term, such as on a keyword field.
Yes, more than an exact match, because many candidate terms are tested. Setting prefix_length and keeping max_expansions low reduces the cost.
Under AUTO, terms of 3 to 5 characters allow one edit and terms up to 2 characters allow none. A typo needing two edits will not match.
Fuzzy search tolerates typos by allowing a small edit distance, up to 2. Use match with "fuzziness": "AUTO" for search boxes, set prefix_length to keep it fast, and combine exact and fuzzy clauses in a bool query so correct spellings rank first.