liveBook · Manning

Fuzzy query

As explained in Elasticsearch in Action, Second Edition

A fuzzy query is an Elasticsearch query that matches terms within a permitted edit distance of the supplied term.

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

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains edit-distance mutations, explicit and AUTO settings, and query forms, but does not detail execution performance.
Examples
Uses misspelled book tags, “rama” for “drama,” “ama” with distance 2, and “Pattenrs~” in a query_string request.
What it assumes
Assumes familiarity with Elasticsearch fields and basic queries while introducing edit distance and spelling tolerance from concrete examples.
What you can do after
Apply fuzzy matching through Elasticsearch REST JSON using match, fuzzy, or query_string query syntax.
How it connects
Connects fuzzy queries to match queries, query_string queries, term-level queries, wildcard queries, Levenshtein distance, and Damerau–Levenshtein distance.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.