$fuzzy operator matches terms that are similar but not identical to the search term.
This is useful for handling typos, misspellings, and minor variations in user input.
Levenshtein Distance
Fuzzy matching uses Levenshtein distance (also called edit distance) to measure similarity between terms. The distance is the minimum number of single-character edits needed to transform one word into another. These edits include:- Insertions: Adding a character (“headphone” → “headphones”)
- Deletions: Removing a character (“headphones” → “headphone”)
- Substitutions: Replacing a character (“headphones” → “headphonez”)
Distance Parameter
Thedistance parameter sets the maximum Levenshtein distance allowed for a match.
The default is 1, and the maximum allowed value is 2.
| Distance | Matches |
|---|---|
| 1 | Words with 1 edit (handles most single typos) |
| 2 | Words with up to 2 edits (handles more severe misspellings) |
Transposition Cost
A transposition swaps two adjacent characters (e.g., “teh” → “the”). By default, a transposition counts as 1 edit. SettingtranspositionCostOne: false counts transpositions as two edits (one deletion + one insertion) instead.
For example, searching for “haedphone” to find “headphone”:
- With
transpositionCostOne: true: Distance is 1 (swap counts as 1) - Without
transpositionCostOne: Distance is 2 (swap “ae” → “ea” costs 2)
Prefix Matching
Settingprefix: true enables fuzzy prefix matching, which matches terms that start with a fuzzy
variation of the search term. This combines the typo tolerance of fuzzy matching with prefix
matching for incomplete words.
For example, searching for “headpho” with prefix: true:
- Matches “headphones” (prefix match)
- Matches “headphone” (prefix match)
- Matches “haedphones” with
transpositionCostOne: true(fuzzy prefix, handles typo + incomplete word)
Multiple Words
When you provide multiple words to$fuzzy, each word is matched with fuzzy tolerance and combined using
a boolean $must query.
This means all terms must match (with their respective fuzzy tolerance) for a document to be returned.
For example, searching for “wireles headphons” will match documents containing both “wireless” and “headphones”, even with the typos.
Compatibility
| Field Type | Supported |
|---|---|
| TEXT | Yes |
| U64/I64/F64 | No |
| DATE | No |
| BOOL | No |
Examples
- TypeScript
- Redis CLI