How to Setup Solr on Rails with Sunspot

by Nathaniel Rowe on August 24, 2016

It's inevitable that as you develop applications, clients and users will want searching added. Completely inevitable. What happens when you need to extend beyond searching fields that are saved on your models? There's also the ever present need to keep things scalable. Having a solution that grows with you and isn't tied 100% to your schema can not only impress, but save you time in the long run. Check out Solr, the incredibly powerful search solution from Apache. In this article, I'll show you how to set it up and how to do a basic search.

Set It Up

Check out the Sunspot gem and read the docs. The following instructions are for setting this up on a development machine that runs Foreman (I highly recommend using this if you don't already).

Gemfile.rb

gem 'sunspot_rails'

group :development, :test
  gem 'sunspot_solr'
end
Procfile.dev

solr: bundle exec rake sunspot:solr:run

The Sunspot Solr gem saves you from having to install Solr separately, which gets you up and running quicker. Adding the above to your dev Procfile starts up Solr with your app. Just a few more easy steps and you're off to the races.

Model Changes

Given a Car model, the following will allow for some basic searching to try out Solr:

class Car < ActiveRecord::Base
# id                        :integer          not null, primary key
# mileage                   :integer          not null, default: 0
# make                      :string
# model                     :string

  searchable do
    string :make
    string :model
    integer :mileage
  end
end

That's the minimal setup to search the :make, :model, :mileage attributes on Car. After you add that, run the following from a console to reindex your Cars. If you don't, Solr won't have any search documents created for Car and you won't get any search results returned.

Car.solr_index

Any time you make changes to the searchable block, you'll need to reindex if you want your changes to be picked up. Like any properly designed caching system any time you save or update a car, there's a sunspot callback which updates the cache.

The Final Product

At this point, your cache is warmed up and the cars are ready to be searched.

A basic search

search = Car.solr_search do
  with(:make, "Chevrolet")
end

search.results.each {|result| puts r.model}

This example returns a search which will pull all results for Chevrolets and then prints out all the models. Add more with lines to further refine it. As a side note, sunspot returns paginated searches with only 30 results at a time; don't wonder WTF is happening if there should be more results, check the next page.

Some helpful methods to get you started:
search.total #count of the total results
search.total_pages #count of how many pages of results
search.results #the actual page of results

This is far from comprehensive, but it's a great start to get you up and running with Solr. A big technology that's got a shallow learning curve. Perfect for jumping in to a dedicated search solution. There will be more posts to follow with advanced ways to use this tech.

Ruby On Rails Performance

Let's Get In Touch!


Our best work gets done when we can work face-to-face with you.

770-317-4866