Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
dev_tasks Use appraisals to test against the dependencies (#895) Mar 25, 2018
gemfiles Use appraisals to test against the dependencies (#895) Mar 25, 2018
lib Fix #939 (#944) Jul 5, 2019
spec Proper update_format configuration for sunspot_rails (#947) Jul 12, 2019
.gitignore Use appraisals to test against the dependencies (#895) Mar 25, 2018
.rspec Port to Rails 3 Aug 9, 2010
Gemfile Use appraisals to test against the dependencies (#895) Mar 25, 2018
History.txt Ensure all models will load even with an empty string passed as an Sep 5, 2013
LICENSE Moving Sunspot::Rails project files into subdirectory Jan 18, 2010
MIT-LICENSE Moving Sunspot::Rails project files into subdirectory Jan 18, 2010
Rakefile Rely on RDoc::Task rather than the defunct Rake::RDocTask Oct 29, 2013
TODO Use session proxy for disconnecting Sunspot in specs Jan 26, 2010
install.rb Moving Sunspot::Rails project files into subdirectory Jan 18, 2010



Sunspot::Rails is a Rails plugin that provides drop-in integration of the Sunspot Solr search library with Rails. It provides the following features:

  • Configure Sunspot using config/sunspot.yml

  • Extend ActiveRecord for easy index configuration, search, and indexing

  • Automatically index ActiveRecord objects when they are saved, and remove them from the index when they are destroyed (can be disabled)

  • Automatically commit Solr changes at the end of each request (can be disabled)

  • Provide utility methods to find and fix orphaned documents and rebuild the Solr index for a given class

  • Provide rake tasks for starting and stopping the development Solr instance, using the configuration in sunspot.yml

Sunspot::Rails has been tested with Rails 3.0, 3.1, 3.2, and 4.0

Installation in Rails 3

In your Gemfile:

gem 'sunspot_rails'
# This is an optional packaged Solr:
group :test, :development do
  gem 'sunspot_solr'

Using Sunspot::Rails

If you wish to make modifications to the Solr schema, you can create a custom Solr home in your project directory. In order to do so, create the directory RAILS_ROOT/solr/conf, and copy in the contents of the Solr gem's solr/solr/conf directory. If the files are in the right place, Sunspot::Rails will detect them and tell Solr to use your local configurations. Use caution when modifying schema.xml - Sunspot relies on the field naming scheme in the packaged schema file.

To start up a Solr instance, issue the following (requires sunspot_solr to be installed):

rake sunspot:solr:start

Note that using the built-in Solr instance packaged with Sunspot is great for development, but is not recommended for production. See the Sunspot documentation for more information.



In order for an ActiveRecord model to be indexable and searchable, it must be configured for search. For example:

class Post < ActiveRecord::Base
  searchable do
    text :title, :body
    integer :blog_id
    time :updated_at
    string :sort_title do
      title.downcase.sub(/^(an?|the) /, '')

See the documentation for Sunspot.setup for full details on what can go in the configuration block.


By default, models are indexed whenever they are saved, and removed from the index whenever they are destroyed. This behavior can be disabled:

class Post < ActiveRecord::Base
  searchable :auto_index => false, :auto_remove => false do
    # setup...

Note that using the :auto_remove option is not recommended , as destroying an object without removing it from the index will create an orphaned document in the index, which is a Bad Thing. Turning off :auto_index is perfectly safe if you prefer to manage indexing manually (perhaps using a background job).

If you have disabled lifecycle indexing hooks, you can invoke indexing operations directly on your model:

post = Post.create


When data is changed in Solr, it is initially stored in memory and not made available to the currently running searcher instance. Issuing a commit to Solr will cause it to write the changes to disk, and instantiate a new searcher instance. This operation is fairly expensive, so rather than issuing a commit every time a document is added or removed, Sunspot::Rails issues a commit at the end of any request where data has been added to or removed from Solr. If you need to immediately issue a commit, bang!-versions of the methods are available:

post = Post.create
# this is the same as...

When writing tests outside of the context of a controller request, you will want to use one of these two approaches.


Do it like this: do
  with :blog_id, 1
  with(:updated_at).greater_than( - 2.weeks)
  order_by :sort_title, :asc
  paginate :page => 1, :per_page => 15

See the documentation for for all the options available in the search block, and the information available in the result block.

Searching for IDs

In some situations, you may want to get the IDs for models returned by a search without actually loading the models out of the database. For that, you can call search_ids, using the same block format as #search. This will return an array of IDs.

Searching for multiple types

Sunspot is entirely agnostic about whether searches are for one or more types; the only restriction is that columns used for restriction, ordering, etc. are defined in the same way for all types being searched. Sunspot::Rails does not provide any additional support for this, since there is not anything useful to be added, so just use the interface provided by Sunspot:, Comment) do
  with :blog_id, 1
  order_by :created_at, :asc

Be sure to check out the Sunspot documentation for all the details.

Adding search functionality in mixins

Sunspot does not require that search setup for a given class happen all in one place; it is perfectly acceptable to call the Sunspot.setup method more than once. This capability is particularly useful for adding search functionality in mixins. For instance, if you have a Ratable module, you may wish to add additional search fields for searchable classes that mix in that module. For example:

module Ratable
  def self.included(base)
    if base.searchable?
      base.searchable do
        float :average_rating do

Note the use of base.searchable? - this ensures that only classes that already have search enabled will have the additional configuration added. The above pattern requires that the class be declared searchable before the module is mixed in; other patterns (such as passing a :searchable option to an acts_as_-style declaration) may be more flexible.

Utility methods

If you need to completely reindex all of the instances of a given model class, you can issue:


If for some reason models get deleted from the database, but not from the index, they will become index orphans - not a good situation. To get IDs that exist in the index but not the database, you can use the index_orphans method; to remove those documents from the index, use clean_index_orphans. Note that neither of these operations should be needed if Sunspot and Sunspot::Rails are used as intended.


ActiveRecord index hooks

By default, `sunspot_rails` uses `after_save` and `after_destroy` hooks to automatically index or remove ActiveRecord models. When you're using any sort of asynchronous indexing like [sunspot_index_queue]( or [sunspot-queue]( you may want these to be after_commit hooks or you may have timing issues.

To do this, add the following to your `sunspot.yml` for each environment:

  # ...
  auto_index_callback: after_commit
  auto_remove_callback: after_commit

Note that if you set these to `after_commit` in the `test` environment you may need if you use `transactionnal_fixtures = true`.

Testing Solr integration using RSpec

To disable the sunspot-solr integration for your active record models, require the file `sunspot/rails/spec_helper`

Then, in your spec, use the #disconnect_sunspot method:

require 'sunspot/rails/spec_helper'

describe Post do

  it 'should have some behavior' do
    # ...

In all of the examples in this group, all Sunspot calls will be stubbed out. The Sunspot#search method will return a stub search object that mimics a search with no results.

Further Reading

Reading the Sunspot documentation is highly recommended. Sunspot::Rails exists to wrap Sunspot with a Rails-friendly API, but almost all of the functionality you use in Sunspot::Rails is implemented in Sunspot.

Posts about Sunspot on my blog are available at


Please submit bug reports to


  • Mat Brown (

  • Peer Allan (

  • Michael Moen (

  • Benjamin Krause (

  • Adam Salter (

  • Brandon Keepers (

  • Paul Canavese (

  • John Eberly (

  • Gert Thiel (


Sunspot::Rails is distributed under the MIT License, copyright © 2013 Mat Brown

You can’t perform that action at this time.