Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.
/ websolr-sunspot_rails Public archive

[Deprecated] The preferred way of integrating websolr and Ruby on Rails

License

Notifications You must be signed in to change notification settings

omc/websolr-sunspot_rails

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

This is the gem to install for the supported version of sunspot_rails on websolr. The version number of this gem will track the sunspot_rails gem version, with an additional number at the end for our patches. If you install websolr-sunspot_rails 0.11.4.2, that means you got the third (0, 1, 2) release of websolr support for sunspot_rails 0.11.4.

Installation

cd your_rails_app
sudo gem install websolr-sunspot_rails
config.gem "websolr-sunspot_rails"                # into config.environment.rb
echo websolr-sunspot_rails >> .gems               # if using Heroku
./script/generate sunspot                         # Installs config/sunspot.yml
echo "require 'sunspot/rails/tasks'" >> Rakefile  # Installs local development tasks

Usage

Starting a local development server

To start up a Solr instance for development, issue the following:

rake sunspot:solr:start

Using in production

You need to make sure that the WEBSOLR_URL environment variable is set correctly.

If you’re using Heroku, this should happen automatically. You can verify by running heroku config.

If you’re running in your own environment, set the environment variable as you normally would on a *nix system.

If you have to set the variable in Ruby, you should be able to put the following in an Rails initializer:

if RAILS_ENV == "production"
  ENV["WEBSOLR_URL"] = "http://index.websolr.com/solr/[your-api-key]"
  load "websolr-sunspot_rails.rb"
end

Setup

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) /, '')
    end
  end
end

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

Indexing

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...
  end
end

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
post.index
post.remove_from_index

Committing

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
post.index!
# this is the same as...
post.index
Sunspot.commit

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

Searching

Do it like this:

Post.search do
  with :blog_id, 1
  with(:updated_at).greater_than(Time.now - 2.weeks)
  order :sort_title, :asc
  paginate :page => 1, :per_page => 15
end

See the documentation for Sunspot.search 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:

Sunspot.search(Post, Comment) do
  with :blog_id, 1
  order :created_at, :asc
end

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
          ratings.average(:value)
        end
      end
    end
  end
end

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:

Post.reindex

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.

Testing Solr integration using RSpec

To disable the sunspot-solr integration for your active record models, add the following line to your spec_helper.rb

require ‘sunspot/spec/extension’

This will disable all automatic after_save/after_destroy solr-requests generated via the #searchable method. This will not disable/mock explicit calls in your code.

If you want to test the sunspot-solr integration with active record, you can reenable the after_save/after_destroy hooks by adding ‘integrate_sunspot’ in your examples.

describe Searches do
  integrate_sunspot

  before(:each) do
    @movie = Factory.create :movie
  end

  it "should find a movie" do
    Movie.search { keywords @movie.title }.first.should == @movie
  end
end

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.

About

[Deprecated] The preferred way of integrating websolr and Ruby on Rails

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages