Sunspot is a ruby driver for Solr and is pretty easy to integrate into rails using sunspot_rails.
Installation
First add it to your Gemfile:
1
|
|
Update your bundle:
1
|
|
Run this task to create your sunspot.yml
file:
1
|
|
To start the sunspot server run:
1
|
|
You probably don’t need the rails env variable set but I usually do it to be explicit. The first time you run this command,
it will create a solr/ directory
in your Rails root. This contains Solr’s configuration, as well as the actual data files
for the Solr index. You’ll probably want to add solr/data
to your .gitignore
.
Setting up your Models
There are two ways to setup your models for indexing. First way (and more appropriate for rails) is to use the ‘searchable’ macro. Suppose we have a model called Provider with first_name, middle_name, and last_name columns in the database. We can setup those fields to indexing with the following:
1 2 3 4 5 |
|
The second way is the following:
1 2 3 |
|
This code can be stored at the end of the model (outside of the class), or in an initializer, though it isn’t too pretty. For more information on setting up classes with search and indexing, check out this page
Don’t Forget to Reindex
To reindex, run the following rake task:
1
|
|
You can also reindex in the console by running:
1
|
|
Testing Provider search with RSpec
Normally I use before(:each)
blocks to set up my data, but setting up search data is a use case for using a before(:all)
block. With search, we’re not manipulating the data, so having test data setup before all search specs is not only
acceptable, but efficient. Below are specs for testing that a provider can be found by all parts of his/her name:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
One gotcha. Don’t forget to start your sunspot server in the test environment:
1
|
|