liveBook · Manning

Edit distance

As explained in Elasticsearch in Action, Second Edition

The Levenshtein distance algorithm computes the minimum number of insertions, deletions, and substitutions needed to transform one string into another.

Levenshtein distance measures the minimum number of single-character insertions, deletions, and substitutions needed to transform one string into another. In search, it lets Elasticsearch find likely matches despite spelling mistakes. section 10.10 section 2.3.8

A distance of 1 forgives one character change. Thus, a search for “rama” can find “drama,” while “ama” requires distance 2. Fuzziness can be set explicitly or left as AUTO, which chooses a distance from word length. 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
      }
    }
  }
}

Use a match query with fuzziness 1 to let a books search match the misspelled term “Komputer” with “Computer.” section 2.3.8

Fuzzy movie search

A fuzzy query for “rama” with fuzziness 1 can return drama movies. Unlike wildcard queries, fuzzy queries use edit distance rather than * or ? operators. section 9.9

Term-level fuzzy queries use Levenshtein distance. Full-text query_string fuzziness uses a default edit distance of 2 and may use Damerau–Levenshtein, which also supports adjacent-character transpositions. section 9.10 section 10.10

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Defines insertions, deletions, and substitutions, then applies distances through fuzzy queries without deriving the algorithm internally.
Examples
Uses “Komputer” versus “Computer,” “rama” versus “drama,” and “cake” versus similar words as spelling examples.
What it assumes
Assumes familiarity with Elasticsearch search queries while explaining spelling tolerance through concrete word transformations.
What you can do after
Apply it via Elasticsearch fuzzy, match, or query_string queries to retrieve results containing spelling variations.
How it connects
Connects fuzzy queries, match queries, term-level queries, full-text search, wildcard queries, and Damerau–Levenshtein distance.

Also explained in

Compare how 6 books explain edit 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.