# Fuzzy query scoring based on levenshtein distance

**URL:** <https://discuss.elastic.co/t/fuzzy-query-scoring-based-on-levenshtein-distance/11116>\
**Category:** Elasticsearch\
**Created:** [March 12, 2013, 5:45pm UTC](https://discuss.elastic.co/t/fuzzy-query-scoring-based-on-levenshtein-distance/11116 "2013-03-12T17:45:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![the\_blab](https://avatars.discourse-cdn.com/v4/letter/t/8e7dd6/32.png) [@the\_blab](https://discuss.elastic.co/u/the_blab)\
**Post date:** [March 12, 2013, 5:45pm UTC](https://discuss.elastic.co/t/fuzzy-query-scoring-based-on-levenshtein-distance/11116/1 "2013-03-12T17:45:09Z")

</div>

Hi,

I have a question about scoring for fuzzy queries. If I understand  
correctly, fuzzy queries find any appropriate matches by calculating  
similarity using the levenshtein distance, but this similarity value is not  
used when calculating the document's score. Instead the document's score is  
based on the tf/idf of the matched term. Is this correct? Is it possible to  
instead score based on similarity to the queried term for fuzzy queries?  
E.g. I have the below custom\_score query. I'd like the score returned to be  
the similarity score used to evaluate against the min\_similarity.

{  
"query": {  
"custom\_score" : {  
"query": {  
"fuzzy": {  
"firstname": {  
"value": "Jack",  
"min\_similarity": "0.5",  
"max\_expansions": 1  
}  
}  
},  
"script" : "\_score"  
}  
}  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![simonw\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simonw_2/32/1130_2.png) [@simonw\_2](https://discuss.elastic.co/u/simonw_2)\
**Post date:** [March 14, 2013, 7:02am UTC](https://discuss.elastic.co/t/fuzzy-query-scoring-based-on-levenshtein-distance/11116/2 "2013-03-14T07:02:03Z")

</div>

Hey,

this is not entirely true. The FuzzyQuery uses the Levenshtein Distance to  
find the terms in the index that are subsequentially used in a Boolean OR  
query or in a ConstantScore Filter depending on the rewrite method you  
choose. The default also just takes the top 50 terms within a certain LD  
and then builds a query out of it. The scoring will just be the similarity  
of you scoring model so TF/IDF (VectorSpace) by default.

I don't understand your last sentence, what do you mean by 'against the  
min\_similarity'?

simon

On Tuesday, March 12, 2013 6:45:09 PM UTC+1, the blab wrote:

> Hi,
> 
> I have a question about scoring for fuzzy queries. If I understand  
> correctly, fuzzy queries find any appropriate matches by calculating  
> similarity using the levenshtein distance, but this similarity value is not  
> used when calculating the document's score. Instead the document's score is  
> based on the tf/idf of the matched term. Is this correct? Is it possible to  
> instead score based on similarity to the queried term for fuzzy queries?  
> E.g. I have the below custom\_score query. I'd like the score returned to be  
> the similarity score used to evaluate against the min\_similarity.
> 
> {  
> "query": {  
> "custom\_score" : {  
> "query": {  
> "fuzzy": {  
> "firstname": {  
> "value": "Jack",  
> "min\_similarity": "0.5",  
> "max\_expansions": 1  
> }  
> }  
> },  
> "script" : "\_score"  
> }  
> }  
> }

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![the\_blab](https://avatars.discourse-cdn.com/v4/letter/t/8e7dd6/32.png) [@the\_blab](https://discuss.elastic.co/u/the_blab)\
**Post date:** [March 14, 2013, 5:52pm UTC](https://discuss.elastic.co/t/fuzzy-query-scoring-based-on-levenshtein-distance/11116/3 "2013-03-14T17:52:02Z")

</div>

Thanks for your response. By "against the min\_similarity" I meant the  
minimum value for the similarity of the fuzzy terms, i.e. the  
min\_similarity parameter provided in the query I posted.

To clarify there are two "scores" being calculated in the query: the  
"levenshtein distance" to determine what terms to use, and the actual  
scoring of the returned results. I wanted the levenshtein distance to be  
used to score the returned results, but I don't think this is possible.

For future readers I solved this issue by creating a custom score query and  
writing a (native) script to handle the scoring.

Thanks

On Thursday, March 14, 2013 7:02:03 AM UTC, simonw wrote:

> Hey,
> 
> this is not entirely true. The FuzzyQuery uses the Levenshtein Distance to  
> find the terms in the index that are subsequentially used in a Boolean OR  
> query or in a ConstantScore Filter depending on the rewrite method you  
> choose. The default also just takes the top 50 terms within a certain LD  
> and then builds a query out of it. The scoring will just be the similarity  
> of you scoring model so TF/IDF (VectorSpace) by default.
> 
> I don't understand your last sentence, what do you mean by 'against the  
> min\_similarity'?
> 
> simon
> 
> On Tuesday, March 12, 2013 6:45:09 PM UTC+1, the blab wrote:
> 
> > Hi,
> > 
> > I have a question about scoring for fuzzy queries. If I understand  
> > correctly, fuzzy queries find any appropriate matches by calculating  
> > similarity using the levenshtein distance, but this similarity value is not  
> > used when calculating the document's score. Instead the document's score is  
> > based on the tf/idf of the matched term. Is this correct? Is it possible to  
> > instead score based on similarity to the queried term for fuzzy queries?  
> > E.g. I have the below custom\_score query. I'd like the score returned to be  
> > the similarity score used to evaluate against the min\_similarity.
> > 
> > {  
> > "query": {  
> > "custom\_score" : {  
> > "query": {  
> > "fuzzy": {  
> > "firstname": {  
> > "value": "Jack",  
> > "min\_similarity": "0.5",  
> > "max\_expansions": 1  
> > }  
> > }  
> > },  
> > "script" : "\_score"  
> > }  
> > }  
> > }

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Octavian](https://avatars.discourse-cdn.com/v4/letter/o/54ee81/32.png) [@Octavian](https://discuss.elastic.co/u/Octavian)\
**Post date:** [October 29, 2014, 3:54pm UTC](https://discuss.elastic.co/t/fuzzy-query-scoring-based-on-levenshtein-distance/11116/4 "2014-10-29T15:54:22Z")

</div>

Hi Blab,

I also want to return a score based on levenshtein distance from a fuzzy  
query. Can you elaborate more on "writing a (native) script to handle the  
scoring.", please? Did you actually write a script that calculates the  
distance or did you use some ES properties?

Thank you,

On Thursday, March 14, 2013 7:52:02 PM UTC+2, the blab wrote:

> Thanks for your response. By "against the min\_similarity" I meant the  
> minimum value for the similarity of the fuzzy terms, i.e. the  
> min\_similarity parameter provided in the query I posted.
> 
> To clarify there are two "scores" being calculated in the query: the  
> "levenshtein distance" to determine what terms to use, and the actual  
> scoring of the returned results. I wanted the levenshtein distance to be  
> used to score the returned results, but I don't think this is possible.
> 
> For future readers I solved this issue by creating a custom score query  
> and writing a (native) script to handle the scoring.
> 
> Thanks
> 
> On Thursday, March 14, 2013 7:02:03 AM UTC, simonw wrote:
> 
> > Hey,
> > 
> > this is not entirely true. The FuzzyQuery uses the Levenshtein Distance  
> > to find the terms in the index that are subsequentially used in a Boolean  
> > OR query or in a ConstantScore Filter depending on the rewrite method you  
> > choose. The default also just takes the top 50 terms within a certain LD  
> > and then builds a query out of it. The scoring will just be the similarity  
> > of you scoring model so TF/IDF (VectorSpace) by default.
> > 
> > I don't understand your last sentence, what do you mean by 'against the  
> > min\_similarity'?
> > 
> > simon
> > 
> > On Tuesday, March 12, 2013 6:45:09 PM UTC+1, the blab wrote:
> > 
> > > Hi,
> > > 
> > > I have a question about scoring for fuzzy queries. If I understand  
> > > correctly, fuzzy queries find any appropriate matches by calculating  
> > > similarity using the levenshtein distance, but this similarity value is not  
> > > used when calculating the document's score. Instead the document's score is  
> > > based on the tf/idf of the matched term. Is this correct? Is it possible to  
> > > instead score based on similarity to the queried term for fuzzy queries?  
> > > E.g. I have the below custom\_score query. I'd like the score returned to be  
> > > the similarity score used to evaluate against the min\_similarity.
> > > 
> > > {  
> > > "query": {  
> > > "custom\_score" : {  
> > > "query": {  
> > > "fuzzy": {  
> > > "firstname": {  
> > > "value": "Jack",  
> > > "min\_similarity": "0.5",  
> > > "max\_expansions": 1  
> > > }  
> > > }  
> > > },  
> > > "script" : "\_score"  
> > > }  
> > > }  
> > > }

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/1012a30f-cdc9-4170-8b3f-c83866e2425d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1012a30f-cdc9-4170-8b3f-c83866e2425d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 12:53am UTC](https://discuss.elastic.co/t/fuzzy-query-scoring-based-on-levenshtein-distance/11116/5 "2017-07-06T00:53:10Z")

</div>


