> ## Documentation Index
> Fetch the complete documentation index at: https://new.docs.falkordb.com/llms.txt
> Use this file to discover all available pages before exploring further.

# text.levenshtein

> Computes the Levenshtein edit distance between two strings, useful for fuzzy matching and spell checking.

## Description

Computes the Levenshtein edit distance between two strings. The edit distance is the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one string into another. This is useful for fuzzy string matching, spell checking, and finding similar records.

## Syntax

```cypher theme={null}
flex.text.levenshtein(string1, string2)
```

## Parameters

| Parameter | Type   | Required | Description                  |
| --------- | ------ | -------- | ---------------------------- |
| `string1` | string | Yes      | The first string to compare  |
| `string2` | string | Yes      | The second string to compare |

## Returns

**Type:** number (integer)

The minimum number of single-character edits needed to transform `string1` into `string2`. Returns `0` if the strings are identical.

## Examples

### Example 1: Basic String Comparison

```cypher theme={null}
// Compare two similar strings
RETURN flex.text.levenshtein('kitten', 'sitting') AS distance
```

**Output:**

```text theme={null}
distance
--------
3
```

### Example 2: Finding Similar User Names

```cypher theme={null}
// Find users with names similar to "Sarah" within edit distance of 2
MATCH (u:User)
WHERE flex.text.levenshtein(u.name, 'Sarah') <= 2
RETURN u.name, u.email, flex.text.levenshtein(u.name, 'Sarah') AS distance
ORDER BY distance
```

### Example 3: Fuzzy Matching with Multiple Candidates

```cypher theme={null}
// Find the closest matching product name
WITH 'iPhone' AS search_term
MATCH (p:Product)
WITH p, flex.text.levenshtein(p.name, search_term) AS distance
WHERE distance <= 3
RETURN p.name, distance
ORDER BY distance
LIMIT 5
```

## Notes

* Handles `null` values gracefully by treating them as empty strings
* The function is symmetric: `levenshtein(a, b) = levenshtein(b, a)`
* Empty strings return the length of the non-empty string as distance
* Two `null` values return distance of `0`
* Optimized for performance with memory-efficient implementation
* Case-sensitive comparison (use `toLower()` if case-insensitive matching is needed)

## See Also

* [sim.jaccard](/udfs/flex/similarity/jaccard) - Set-based similarity for collections
* [text.jaroWinkler](/udfs/flex/text/jaroWinkler) - Alternative string similarity metric

## Frequently Asked Questions

<AccordionGroup>
  <Accordion title="What does a Levenshtein distance of 0 mean?">
    A distance of 0 means the two strings are identical — no edits are needed.
  </Accordion>

  <Accordion title="Is flex.text.levenshtein case-sensitive?">
    Yes. Uppercase and lowercase characters are considered different. Wrap inputs with `toLower()` for case-insensitive comparison.
  </Accordion>
</AccordionGroup>
