I'm Co-Founder & CTO of BlockAvenue. , connect with me, or subscribe.

Using Sunspot, Websolr, and Solr on Heroku

Having recently deployed a Rails app using Sunspot and Solr on Heroku, here are some tips for getting started, testing, searching, and deploying.

Getting the right gem

As noted in this post, getting the right gem for Websolr is important (yet confusing): the Heroku documentation is not consistent on which gem to use. In short, use the websolr-sunspot_rails gem which tracks the version of Sunspot needed by Websolr.

The following gems are used:

UPDATE (per Nick Zadrozny):

the docs at https://devcenter.heroku.com/articles/websolr are correct: You want to use Sunspot directly, not websolr-sunspot_rails. The only thing the devcenter document gets wrong right now is the version. The current version of Sunspot is 1.3.0. I’ll add that to my todo list ;)

The websolr-sunspot_rails gem was only needed for earlier versions of Sunspot in order to support the WEBSOLR_URL variable, which is supported natively in Sunspot as of its 1.2.0.

So all you need is gem 'sunspot_rails' in your Gemfile and you’re all set!

Configuration

config/sunspot.yml provides a base config file for development and production only. ENV['SOLR_URL'] and ENV['WEBSOLR_URL'] override any values in this file.

Starting Solr (locally)

Solr can be started (and stopped) locally with:

Testing

If using rspec the following can get you started:

Your search spec may look like this:

The following allows Cucumber features not to explode:

Searching and paginating

Perform the search in the controller:

In the template show results and paging: