Sunspot and Rails 3 (with websolr)

ootoovak edited this page Sep 14, 2010 · 7 revisions

Installation

Below is out of date. See Using-Sunspot-With-Rails instead

First, you’ll need to install the sunspot gem. In your Gemfile add the line:

gem 'sunspot', :require => 'sunspot'

Then run the standard bundle install to retrieve the latest version of sunspot.

Since we’re not going to be using Sunspot::Rails, we need to copy the adapters.rb file to lib/sunspot/rails.

Also we’ll need to add the next couple of lines to config/initializers/sunspot.rb:

require File.join(Rails.root, 'lib', 'sunspot', 'rails', 'adapters')

# Sunspot.session = Sunspot::Rails.build_session
Sunspot::Adapters::InstanceAdapter.register(Sunspot::Rails::Adapters::ActiveRecordInstanceAdapter, ActiveRecord::Base)
Sunspot::Adapters::DataAccessor.register(Sunspot::Rails::Adapters::ActiveRecordDataAccessor, ActiveRecord::Base)

Finally, we need to add connection string information to our environments. For example, in config/environments/development.rb we’ll add:

Sunspot.config.solr.url = 'http://localhost:8982/solr'

Running Sunspot Without Rake Tasks

Now that your rails app is setup, you need to start a solr instance for sunspot to connect to. Locally we can run:

sunspot-solr start -p 8982 -d solr/data/development -s solr --pid-dir=tmp/pids -l FINE --log-file=log/sunspot-solr-development.log

And to stop the instance we can use:

sunspot-solr stop -p 8982 -d solr/data/development -s solr --pid-dir=tmp/pids -l FINE --log-file=log/sunspot-solr-development.log

Websolr via Heroku

In addition to the steps above, for websolr we can use our private websolr url. If you’re on Heroku you can see this by running heroku config --long in your app’s directory, or heroku config --app app_name --long anywhere in the console. If websolr is enabled for the app, you should see something like:

WEBSOLR_PWD    => 12345abcde
WEBSOLR_URL    => http://index.websolr.com/solr/1a2b3c4d5e
WEBSOLR_USER   => heroku-12345abcde

Take note of the WEBSOLR_URL variable. In your config/environments/production.rb you need to add the line:

Sunspot.config.solr.url = ENV['WEBSOLR_URL']

You may also need to login into Websolr console and change client type to *Sunpot 1.1 Experimental"

That should be all you need to do to get websolr working!