DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Install And Test SOLR+Sunspot In Your Rails+RSpec Project

Alexey Tarasevich user avatar by
Alexey Tarasevich
·
Jun. 22, 11 · · Code Snippet
Like (0)
Save
Tweet
1.75K Views

Join the DZone community and get the full member experience.

Join For Free
SOLR is Lucene-based full text search server.
Sunspot is a Ruby gem that serves as adapter for SOLR.

IMHO this combination creates much better developer experience comparing to the alternatives that I've considered before.
Ferret just does not install on Windows. Its official site did not work yesterday. And I've seen that people report that it's instable.
Sphinx+Thinking Sphinx is quite robust alternative. Though it requires regular reindexing. It works only with MySQL & PostgreSQL databases. It installation process requires some manual steps.
ActAsSolrReloaded is not that actively maintained as Sunspot. It lacks documentation, tutorials etc.

Sunspot does not suffer from all those disadvantages. The only problem is configuring automated testing. We'll fix this problem below.
So let's start.

Adding SOLR+Sunspot to Rails project is trivial:

Add to Gemfile:

gem 'sunspot_rails', '~> 1.2.1' 


Add to .gitignore:

solr/data


Run:

bundle install
rails generate sunspot_rails:install

You might review config/sunspot.yml file.

Add to your model class (e.g. Post title:text user:references):

  searchable do
    text :title

    integer :user_id, :references => User
  end
 

Now you ready to test it:

$ rake sunspot:reindex
$ rake sunspot:solr:start
$ rails c

> Post.solr_search { keywords "something" }.results
> Post.solr_search { keywords "some title" ; with :user_id, 1 }.results


Most problematic part here is to create automated tests. 
See also http://blog.kabisa.nl/2010/02/03/running-cucumber-features-with-sunspot_rails/.

Create file spec/support/sunspot.rb:

require 'sunspot/rails/spec_helper'
require 'net/http'

try_server = proc do |uri|
  begin
    Net::HTTP.get_response uri
  rescue Errno::ECONNREFUSED
  end
end

start_server = proc do |timeout|

  server = Sunspot::Rails::Server.new
  uri = URI.parse("http://0.0.0.0:#{server.port}/solr/")

  try_server[uri] or begin

    server.start
    at_exit { server.stop }

    timeout.times.any? do
      sleep 1
      try_server[uri]
    end
  end
end

original_session = nil            # always nil between specs
sunspot_server = nil              # one server shared by all specs

if defined? Spork
  Spork.prefork do
    sunspot_server = start_server[60] if Spork.using_spork?
  end
end

RSpec.configure do |config|

  config.before(:each) do
    if example.metadata[:solr]    # it "...", solr: true do ... to have real SOLR
      sunspot_server ||= start_server[60] || raise("SOLR connection timeout")

    else
      original_session = Sunspot.session
      Sunspot.session = Sunspot::Rails::StubSessionProxy.new(original_session)
    end
  end

  config.after(:each) do
    if example.metadata[:solr]
      Sunspot.remove_all!

    else
      Sunspot.session = original_session
    end
    original_session = nil
  end
end


Now you can add to your specs:

    it "Sunspot works", :solr => true do
      e = Post.create!(:text => "xxx")
      Sunspot.commit
      results = Entry.solr_search { keywords "xxx" }.results
      results.size.should == 1
    end 


Pay attension to :solr => true. We've used it in support/sunspot.rb, to distinguish the test cases with real SOLR Server from those with a stubbed one.

Also pay attention to Sunspot.commit. It's required for your changes to SOLR index to be applied immediately. But default SOLR applies changes quite quickly, but with some delay. What is unacceptable for an automated test.

Also it works well with Spork.
Testing

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Legacy Modernization and Hybrid Cloud with Kafka in Healthcare
  • Classification of Neural Networks in TensorFlow
  • How to Modify Java Command-Line Arguments
  • SQL CTE: How to Master It in One Sitting With Easy Examples

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo