# Fuzziness in span query losing 1 edit distance

**URL:** <https://discuss.elastic.co/t/fuzziness-in-span-query-losing-1-edit-distance/299921>\
**Category:** Elasticsearch\
**Created:** [March 17, 2022, 7:35am UTC](https://discuss.elastic.co/t/fuzziness-in-span-query-losing-1-edit-distance/299921 "2022-03-17T07:35:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![GoaKoala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/goakoala/32/103150_2.png) [@GoaKoala](https://discuss.elastic.co/u/GoaKoala)\
**Post date:** [March 17, 2022, 7:35am UTC](https://discuss.elastic.co/t/fuzziness-in-span-query-losing-1-edit-distance/299921/1 "2022-03-17T07:35:12Z")

</div>

Hi,

I am trying to do a fuzzy matching with phrase search. (This is already mentioned here : [Combine Phrases search AND fuzzy matching](https://discuss.elastic.co/t/combine-phrases-search-and-fuzzy-matching/76535) and here : [Elasticsearch Fuzzy Phrases - Stack Overflow](https://stackoverflow.com/questions/38816955/elasticsearch-fuzzy-phrases/38823174#38823174))  
I managed to solve the issue using span queries as mentioned in the posts however I have an issue.

For ex; If I have this document

```auto
{
  "name" : "Gracious Knight"
}

```

a query like this searches correctly

```auto
GET [INDEX]/_search
{
	"query": {
		"span_near": {
			"clauses": [
				{
					"span_multi": {
						"match": {
							"fuzzy": {
								"name": {
									"fuzziness": "2",
									"value": "Gracious"
								}
							}
						}
					}
				},
				{
					"span_multi": {
						"match": {
							"fuzzy": {
								"name": {
									"fuzziness": "2",
									"value": "Knight"
								}
							}
						}
					}
				}
			],
			"slop": 0,
			"in_order": "true"
		}
	}
}

```

However the issue is the fuziness edit distance is losing 1 level. So in the "fuzzy" under first "span\_multi", this works;

```auto
{
	"name": {
	"fuzziness": "2",
	"value": "Grxcious"
}

```

Meanwhile this doesn't;

```auto
{
	"name": {
	"fuzziness": "2",
	"value": "Grxxious"
}

```

So when with edit distance of 2, it's ok if 1 character is changed howevre if 2 characters are changed no records are returned. If fuziness is set to 0 even if you put in the exact phrase it doesn't match. So it seems like in this type of query 1 character already seems like changed but I couldn't figure out why?

Just wondering if anybody has any idea about this?

Thanks

---

<div class="post-metadata">

**Author:** ![GoaKoala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/goakoala/32/103150_2.png) [@GoaKoala](https://discuss.elastic.co/u/GoaKoala)\
**Post date:** [March 18, 2022, 4:14am UTC](https://discuss.elastic.co/t/fuzziness-in-span-query-losing-1-edit-distance/299921/2 "2022-03-18T04:14:25Z")

</div>

Ok, found the answer for this. Lowercase tokenizer is applied to the values when indexing. And in the search queries I am giving the first letter as capital which is what eating up the edit distance. If you give the whole value as lowercase then everything works as expected.

Duh!

---

<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:** [April 15, 2022, 4:15am UTC](https://discuss.elastic.co/t/fuzziness-in-span-query-losing-1-edit-distance/299921/3 "2022-04-15T04:15:02Z")

</div>

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