# Fuzzy not matching with two substitutions (distance=2)

**URL:** <https://discuss.elastic.co/t/fuzzy-not-matching-with-two-substitutions-distance-2/204931>\
**Category:** Elasticsearch\
**Created:** [October 23, 2019, 5:55pm UTC](https://discuss.elastic.co/t/fuzzy-not-matching-with-two-substitutions-distance-2/204931 "2019-10-23T17:55:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benjamin\_Gathmann](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjamin_gathmann/32/7561_2.png) [@Benjamin\_Gathmann](https://discuss.elastic.co/u/Benjamin_Gathmann)\
**Post date:** [October 23, 2019, 5:55pm UTC](https://discuss.elastic.co/t/fuzzy-not-matching-with-two-substitutions-distance-2/204931/1 "2019-10-23T17:55:12Z")

</div>

In ES 7.4, I have created a simple index with default mapping.  
There is a string field "authors". I have indexed a doc where "authors" is "Benjamin".

Now I use this query:

```
GET my_index/_search
{
  "query": {
    "fuzzy": {
      "authors": {
        "value":"<testvalue>",
        "fuzziness": 2
      }
    }
  }
}

```

I get a match for the following `<testvalue>`:

- `Benjaman`
- `enjaman`
- `Benjamni`
- `enjamni` (!)

But I got NO match for `Banjaman`!

How does that match with the principle of Levenshtein distance?

`Banjaman` has a distance of 2, just like e.g. `enjamni`, so it is not logical.

By the way, transposition as `in` -\> `ni` is not "pure" Levenshein distance, but a derived concept ("Damerau-Levenshtein distance"), this confused me a bit at first.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 24, 2019, 5:48pm UTC](https://discuss.elastic.co/t/fuzzy-not-matching-with-two-substitutions-distance-2/204931/2 "2019-10-24T17:48:49Z")

</div>

The fuzzy query is a [term-level query](https://www.elastic.co/guide/en/elasticsearch/reference/current/term-level-queries.html), which means it does not [analyze](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html) the query terms. As a result, this query is case sensitive.

The author `Benjamin`, when indexed using the default mapping does get analyzed, with the [standard analyzer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-standard-analyzer.html). As a result, what gets indexed is the all-lower case term `benjamin`.

The term `Banjaman` differs 3 characters from `benjamin`, because of the upper/lower case `B`. As a result, the fuzzy query with a fuzziness of 2 returns no hits.

How to solve this? Use the match query with fuziness instead. The match query does analyze the query terms, and gives you case insensitivity:

```auto
GET my_index/_search
{
  "query": {
    "match": {
      "authors":{
        "query": "Banjaman",
        "fuzziness": 2
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Benjamin\_Gathmann](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjamin_gathmann/32/7561_2.png) [@Benjamin\_Gathmann](https://discuss.elastic.co/u/Benjamin_Gathmann)\
**Post date:** [October 25, 2019, 11:47am UTC](https://discuss.elastic.co/t/fuzzy-not-matching-with-two-substitutions-distance-2/204931/3 "2019-10-25T11:47:51Z")

</div>

Awesome explanation, thanks!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 22, 2019, 11:47am UTC](https://discuss.elastic.co/t/fuzzy-not-matching-with-two-substitutions-distance-2/204931/4 "2019-11-22T11:47:54Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
