A fuzzy query forgives spelling mistakes by matching terms within a permitted edit distance. Elasticsearch uses character insertions, deletions, substitutions, and, in full-text query strings, adjacent-character transpositions. section 2.3.8 section 10.10
Choose the form and distance that fit the search scenario:
- Use a term-level fuzzy query when a field value such as genre should match a similar term; setting fuzziness to 1 allows one character mutation. section 9.9
- Use a match query with fuzziness to handle misspellings in full-text input such as “Komputer” matching “Computer.” section 2.3.8
- Use the AUTO setting when the required distance is uncertain: short words receive little or no fuzziness, while longer words receive more. section 9.9
- Use a query_string query with a tilde, such as “Pattenrs~” or “Pattenrs~1”; its default edit distance is 2. section 10.10
Finding drama movies from “rama”
A fuzzy query on the genre field with value “rama” and fuzziness 1 can return “drama” movies. Increasing the distance to 2 handles a value such as “ama,” but broader matching may be unnecessary when AUTO is suitable. section 9.9
Listing 2.14 Matching a phrase with a misspelling (using fuzziness)
GET books/_search
{
"query": {
"match": {
"tags": {
"query": "Komputer", #1
"fuzziness": 1 #2
}
}
}
}
Inspect a runnable REST request that matches a misspelled book tag using a match query and fuzziness. section 2.3.8
Listing 10.27 A fuzzy query string query
GET books/_search
{
"query": {
"query_string": {
"query": "Pattenrs~", #1
"default_field": "title"
}
}
}
Inspect the query_string form, which expresses fuzziness with a trailing tilde and targets a default field. section 10.10
