Skip to content

Instantly share code, notes, and snippets.

@maxivak
Last active October 3, 2021 15:04
Show Gist options
  • Save maxivak/e1211b67d33add72a4c4 to your computer and use it in GitHub Desktop.
Save maxivak/e1211b67d33add72a4c4 to your computer and use it in GitHub Desktop.
using Solr search with Rails app

Using Solr search engine with Sunspot gem in Rails app

Solr search engine

http://lucene.apache.org/solr/

Sunspot

Sunspot - Sunspot is a Ruby library for interaction with the Solr search engine. Sunspot is built on top of the RSolr library, which provides a low-level interface for Solr interaction;

Integrate Solr with Rails app

add gems sunspot_rails:

Gemfile:

gem 'sunspot_rails'

Generate configuration file

rails g sunspot_rails:install

This will create YML file /config/sunspot.yml. Leave default settings.

Run Solr server

Run Solr in development

Use sunspot_solr gem if you want to run Solr in development.

Sunspot::Solr is a packaged distribution of Solr for use with the Sunspot and Sunspot::Rails gems.

Sunspot embeds Solr inside the gem so there's no need to install it separately. This means that it works straight out of the box which makes it far more convenient to use in development.

gem 'sunspot_rails'
gem 'sunspot_solr'

Run Solr server:

bundle exec rake sunspot:solr:start

OR to start in foreground:
sunspot:solr:run


run server in the foreground:

rake sunspot:solr:run

access server from browser:

http://localhost:8982/solr/#/

Run Solr in production

config/sunspot.yml:

production:
  solr:
    hostname: localhost
    port: 8983
    log_level: WARNING
    path: /solr/production

remove folder solr:

cd app/path

rm -rf solr

Start Solr server:

RAILS_ENV=production rake sunspot:solr:start 

It will create directories in solr/:

data/production
test/data
development/data
default/data

Edit solr/solr.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<solr persistent="false">
  <cores adminPath="/admin/cores" host="${host:}" hostPort="${jetty.port:}">
    <core name="production" instanceDir="." dataDir="production/data"/>

  </cores>
</solr>

Remove unnecessary folders:

cd solr

rm -rf default test

Create folders for 'production' data:

mv development production
cd data
rmdir production
ln -s ../production/data production

Now we are ready to start Solr server:

# start in background
RAILS_ENV=production bundle exec  rake sunspot:solr:start


# run in foreground:
RAILS_ENV=production bundle exec  rake sunspot:solr:run


# to stop server:
RAILS_ENV=production bundle exec rake sunspot:solr:stop

# reindex
RAILS_ENV=production bundle exec rake sunspot:solr:reindex

The solution was found here:

Search model

Add to your model:

class Product < ActiveRecord::Base
    
    # solr
    searchable do
      text :name, :description
      time    :created_at

    end
  end
  

controller:

# app/controllers/search_controller.rb

class SearchController < AccountBaseController
  def index
    q = params[:q]
    @q = q
    
    unless q.blank?
      @res = Product.search do
        fulltext q
      end
    end
  end
end

view:

# app/views/search/index.html.haml

= simple_form_for :search, :method=>:get do |f|
  = text_field_tag 'q', @q, :style=>"width: 240px;"
  = f.submit 'Search'


- if !@res
  %hr
  Not found


- else
  Found #{@res.total}
  %hr
  - @res.results.each do |item|
    = link_to item.name, product_path(item)
  %br

Find examples of search in sunspot gem - https://github.com/sunspot/sunspot.

Read more about using Solr in Rails:

Reindex

Anytime a model is created, updated, or destroyed, Sunspot in Rails will automatically update data in its index (as a part of the save callbacks). You only need to do a full reindex if you added or changed a searchable definition for a model.

Reindex using rake:

bundle exec rake sunspot:reindex

# for a specific  model
bundle exec rake sunspot:reindex[500,Post]

# to skip the prompt asking you if you want to proceed with the reindexing:
bundle exec rake sunspot:reindex[,,true]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment