Blog posts for Ruby, Ruby on rails and Linux.

Go back to all blogs

Installing sunspot solr in production for rails application

Kapil Raj Nakhwa2016-Jul- 3

In ruby on rails , The go to option for most of the developers is solr by sunspot.  https://github.com/sunspot/sunspot. They already do have an excellent readme and provide indepth instruction on using sunspot and in installing it in development environment. However, In production environment it is completely a different story. 

For those of you using heroku. It is quite easy as you can just add websolr add on https://devcenter.heroku.com/articles/websolr and you should be good to go. 

However, For those who are hosting the rails app in their own VPS and server boxes you will need to dig a bit deeper. As you probably now that as ruby developers we are overwhelmed when dealing with JVM and XML configurations. 

So i’ll try to make it a bit simpler. 

  • ssh into your machine 
  • sudo apt-add-repository -y ppa:webupd8team/java 
       (add following PPA for java 7 as java is a prerequisite . )
  • sudo apt-get update
  • sudo echo debconf shared/accepted-oracle-license-v1-1 select true | debconf-set-selections
  • sudo apt-get install -y oracle-java7-installer

Now are are ready to install Solr 

I am going with apache solr version of 5.4.0 . You can substitute proper versions in the commands below. 

cd /opt

wget http://archive.apache.org/dist/lucene/solr/5.4.0/solr-5.4.0.tgz

tar xzf solr-5.4.0.tgz solr-5.4.0/bin/install_solr_service.sh --strip-components=2

sudo ./install_solr_service.sh solr-5.4.0.tgz


Solr process 9073 running on port 8983

{

  "solr_home":"/var/solr/data/",

  "version":"5.2.1 1684708 - shalin - 2015-06-10 23:20:13",

  "startTime":"2015-09-18T15:32:05.933Z",

  "uptime":"0 days, 0 hours, 36 minutes, 13 seconds",

  "memory":"49.7 MB (%10.1) of 490.7 MB"}

Now we can create a collection core for our rails application 

su - solr -c "/opt/solr/bin/solr create -c solr_sunspot_my_core_example -n data_driven_schema_configs"

This would create a collection that is empty at /var/solr/data/solr_sunspot_my_core_example with a config/solrconfig.xml file. 


However, for our rails application to be able to make user of the solr configurations we would need to copy the configurations from sunspot .

so we can head over to

 /var/solr/data/solr_sunspot_my_core_example/conf

and remove existing solrconfig.xml file there


rm solrconfig.xml

 

get new copies of the solrconfig.xml and solr by executing following 

wget https://raw.githubusercontent.com/sunspot/sunspot/master/sunspot_solr/solr/solr/configsets/sunspot/conf/schema.xml

wget https://raw.githubusercontent.com/sunspot/sunspot/master/sunspot_solr/solr/solr/configsets/sunspot/conf/solrconfig.xml

Restart the solr service 

service solr restart

head to your rails application and edit config/sunspot.yml now to reflect our configuration paths 

production: 

solr: 

hostname: localhost 

port: 8983 

log_level: WARNING 

path: /solr/solr_sunspot_my_core_example

That should be it. 

You can reindex with following now in your rails application folder 

rake sunspot:reindex


Your search should be working now. 

Tags: ruby on rails,sunspot,solr,lucene,ruby,rails,search on ruby on rails

Go back to all blogs
Never miss a post on new ruby and rails tips