Sunspot Solr Search – Setting up in Rails App within 5 minutes
Here’s a 5 minute guide for getting started with Sunspot Solr Search in Ruby on Rails Application. By the end of this blog, you should have working full-text search in development environment, powered by a locally running Solr instance.
Fulltext search is never been used significantly and all the real power of the Solr search engine along with all the beauty of Ruby; Sunspot [https://github.com/sunspot/sunspot] exposes all of Solr’s most powerful search features using an APIs. Sunspot is a Ruby library for expressive, powerful interaction with the Solr search engine. Developers likes Sunspot Solr Search more because it means robust, flexible fulltext search with no boolean queries and no string programming.
Basic Installation of SunSpot:
Before installing gem , we need to install JRE(Java Runtime Environment) for solr configuration
$ sudo apt-get install openjdk-6-jre
Add the following line in to Gemfile:
Update bundle now:
$ bundle install
Run the following command to generate the config/sunspot.yml:
rails g sunspot_rails:install
In config/sunspot.yml we can add/change the default SOLR configurations based on environment.
development environment’s default port number is 8982.
After data index, we can see the indexed data list in
For example http://localhost:8982/solr.
And the “sunspot” setup is done.
Solr is a standalone HTTP server, but Sunspot comes packaged with a copy of it that’s already configured to work with Sunspot. If sunspot_solr was installed, start the packaged Solr distribution with:
bundle exec rake sunspot:solr:start
When this command is run for the first time in application instance, it will create a solr/ directory in Rails application root. This root folder contains Solr’s default configuration, as well as the actual data files for the Solr index.
SunSpot Solr Search Installation – Basic Trouble Shooting:
If you see: Errno::ECONNREFUSED (Connection refused – connect(2)) Then perhaps:
Solr server is not started properly, run command:
$ rake sunspot:solr:start
An error occurred in starting the solr server
$ rake sunspot:solr:run
to run the server in the foreground and check for errors.
If the same error occurs only in testing environment, but working fine in development environment then check for the environment and invoke the task appropriately:
$ RAILS_ENV=test rake sunspot:solr:run
SunSpot Solr Search – Application Model Configuration:
In application under development, we’ve got a Question model, with Description and Category fields. To set it up for keyword Sunspot Solr search, we use the searchable method:
class Question < ActiveRecord::Base searchable do text :Description, :default_boost => 2
text : Category
Note that the keyword “text” here means that we are creating a field that’s fulltext-searchable. The package will consider to be broken apart into individual keywords, and then those keywords will be matched against the words in keyword search queries. There are quite a few other field types, but text is the only one that is searched by keywords.
Then :default_boost parameter means that, unless otherwise specified, words matching the : Description field should be considered twice as relevant as words matching the : Category field.
Now that Sunspot knows how to index the Question model, we need to get the existing data into Solr. Any time a Question is created, updated, or destroyed, Sunspot::Rails will automatically make the change to the index; we only need to do a full reindex if we have added or changed a searchable definition for a model.
Adding Sunspot Solr Search to Rails application:
In rails application, Sunspot Solr Search can do a lot more than just a simple keyword search, but we are trying to setup in five minutes, we have focused only the fundamentals for the application setup now.
Let’s consider a situation where we have got a search form where a user types in some keywords, which submits a “:q” param to the QuestionsController#search method.
class QuestionsController < ApplicationController def search @search = Question.search(:include => [:comments]) do
Our @search variable is assigned an object of class Sunspot::Search. Here’s how we’ll work with it in the view:
– @search.each_hit_with_result do |title, question|
%h2= h question.title
%p= h truncate(question.description, :length => 100)
Thats it…. the quick setup of Sunspot Solr Search — awesome fulltext search in Rails application. Will write more about the search concepts in the coming weeks.