# Fuzzy search

**URL:** <https://www.ruby-forum.com/t/fuzzy-search/95927>\
**Category:** Rails\
**Created:** [June 20, 2006, 12:02am UTC](https://www.ruby-forum.com/t/fuzzy-search/95927 "2006-06-20T00:02:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [June 20, 2006, 12:02am UTC](https://www.ruby-forum.com/t/fuzzy-search/95927/1 "2006-06-20T00:02:02Z")

</div>

This may be offtopic to Rails, but what are people doing to find records  
based on fuzzy string matches? For example, if you wanted to find a  
Person with name “David Heinemeier H.” but searched using the  
string “Dave Hansson”.

Currently I am find\_by\_sql that calls the PostgreSQL function  
“levenshtein(string1, string2)” which returns results with a score  
indicating how close the matches are. It is OK, but nowhere good as I  
would hope. Any better suggestions?

thanks,  
Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [June 20, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/fuzzy-search/95927/2 "2006-06-20T18:59:58Z")

</div>

Am 20.06.2006 um 00:02 schrieb Jeff C.:

> Currently I am find\_by\_sql that calls the PostgreSQL function  
> “levenshtein(string1, string2)” which returns results with a score  
> indicating how close the matches are. It is OK, but nowhere good as I  
> would hope. Any better suggestions?

Try to use a full text index of the database of your choice. Another  
thing you might want to try is:

“Search Word”

=\> ‘text ILIKE “%SEARCH%” OR text ILIKE “%WORD%”’

Beyond that (sophisticated and prewritten or simple and slow) it  
becomes a bit … harder. “Good Text Searching” (“Information  
Retrieval” is the academic term which is a bit more general) is one  
of the not-yet-completely-solved problems of today’s computer sciences.

Regards,

Manuel

–  
I have found an elegant and short solution for Fermat’s Theorem  
but sadly there is not enough space for it in this signature.

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [June 20, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/fuzzy-search/95927/3 "2006-06-20T19:00:31Z")

</div>

Hi Jeff,

you might want to look at ferret: [http://ferret.davebalmain.com](http://ferret.davebalmain.com) as well.  
There is a link to the API and in the QueryParser  
[http://ferret.davebalmain.com/api/classes/Ferret/QueryParser.html](http://ferret.davebalmain.com/api/classes/Ferret/QueryParser.html) you’ll  
find an explanation how to build a FuzzyQuery. When you need superfast  
search on loads of records you’ll find that sooner or later you’ll need  
a  
‘search specialist’ in your application anyway…

Regards  
Jan
