Fuzzy searching in SOLR with Sunspot

solr sunspot ohm fuzzy


At one point or another, you'll need this functionality:

Find all people whose names start with Jo Sm <- (i.e. the famous John Smith)

Step 1. Make it work

How do we go about that in SOLR (specifically using sunspot?). Here's my brain dump based off of a gist:

Turns out, starting_with in conjunction with a text index works only for a string in lowercase form. No biggie, we just have to remember.

Step 2. Make it pretty?

Doing that for every type of search for lots of fields will quickly become cumbersome. How do we clean that up? Why a DSL of course!

Luckily ruby allows us to extend classes pretty easily. We can do the following:

After that, the example above will look a lot simpler:

blog comments powered by Disqus