Doing partial matches with Solr and Sunspot

sunspot solr searching autocomplete


In case you want to do something like an autocomplete that would pre-populate a suggestion list, say for example, by typing in a Doctor's first name and / or last name, you can do this using the starting_with keyword in sunspot. Let the code speak for itself?

class Doctor
  # this model should have the typical Sunspot.setup clause somewhere else
  include Suggestions

Doctor.suggestions "John Smith D"

So the result would include all people who has John, Smith or have a first or last name starting with D. (Note that I assume you have indexed the first name and last name as text fields)

You can get the code for the whole suggestion library here at my gist.

blog comments powered by Disqus