Solr Sunspot

There are a lot of search options for rails applications out there. We batted around with meta_search, and while implementing it, we ended up doing more and more straight sql searches. While this allowed greater customization and control over what was executing, the search code grew unwieldy. 

Leave it to a new guy to offer an alternative. “New Chris” (who replaced “Old Chris”) mention Apache Solr. It’s a quick little search engine that allows for faceted searches. Not sure what that is? It basically means browse-searching. Let’s say you’re at Pluckers playing trivia and the question comes up “What was the time-machine car in the Back To The Future Series?”. Well, anyone can answer that, but let’s say you’re a bit inebriated. You could search for “back to the future”, but that’ll tell you about the actors, so you decide to search for “back to the future cars”. You end up getting a list of pictures – the big truck that Marty got at the end of the 1st BttF, the futuristic police car from the 2nd BttF – what you’re doing now is browsing down to the answer. The Deloreon, you shout – 5 questions later.

Solr Sunspot is easy. You can install it while sobering up at Pluckers. Here’s how:


1. Follow all the directions at


Yup. So now for the value-add. Here’s the tips I’ve learned:

1. Reindex whenever you do a change to the :searchable parameters in a  model.

2. Put a begin, rescue, end around your searches. You are now reliant upon a seperate search engine process – and if that guy is down,  you’re screwed. So at least rescue failures, page on them, and supply a user-friendly “it’s not you, it’s us” message.

3. each_hit_with_result returns a hit object and your model object. Seperate out your searches to preserve your model object so it’s easier to display in the view. For example, if you’re doing a search against cars and movies, and you do just one search, you’ll have to determine in the view if the object you’re looking at is a car vs a movie (performance hit). But if you do one (lightning speed) search against cars and another one against movies – you can easily discern what attributes are available to those objects within the view (yay).

4. Capistrano – this is a bit different as for deployment, you’ll want to ensure proper balancing and consisten search results against all web servers. So the first step is to setup a master/slave sunspot configuration in your sunspot.yml:


Next, you’ll want to create a task in deploy.rb to handle the boucing of solr (and potential re-indexing) when you have a deployment. Your deployment will fail unless you rescue run failures. For start & reindex, that’s fine – but when I can’t stop a solr server, it’s likely because it’s down – so I want the deploy to continue. Also, it may not be necessary to reindex everytime – so comment out that line for deployment unless you do a searchable model change.





Leave a Reply

Powered by

Up ↑

%d bloggers like this: