artur-rodrigues.com

Taking Solr 5 and Sunspot Rails to Production

by

For a ruby developer, having to deal with the JVM and .xml files can be burdensome - the “convention over configuration” motto doesn’t seem to apply. Nonetheless, Solr remains one of the most powerful options for full text search. The ruby community developed the very convenient gem sunspot and even integrated a Solr distribution within the gem for usage in development and testing environments.

The matter in question arises when we need to take our app to production, as the embedded Solr isn’t suited for the task. The tutorials and guides found on the internet are either outdated or involve too many components making automation a pain. For this guide, I’m using Ubuntu 14.04 and tried to be as succinct as possible.

To make things easy, ssh into the machine and become root:

sudo su -

Java is a prerequisite, so let’s go ahead and install version 7 through webupd8’s PPA:

apt-add-repository -y ppa:webupd8team/java
apt-get update
echo debconf shared/accepted-oracle-license-v1-1 select true | debconf-set-selections
apt-get install -y oracle-java7-installer

We are ready to install Solr. We’ll use version 5.2.1 and follow the instructions from its wiki, which use a bash scripts that creates all the necessary directories (albeit non conventional) and init scripts:

wget http://www.us.apache.org/dist/lucene/solr/5.2.1/solr-5.2.1.tgz
tar xzf solr-5.2.1.tgz solr-5.2.1/bin/install_solr_service.sh --strip-components=2
bash ./install_solr_service.sh solr-5.2.1.tgz
service solr status
Found 1 Solr nodes:

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"}

Next, let’s create a collection for the Rails application:

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

Notice that we’ve named it solr_sunspot_example, this creates an empty collection at /var/solr/data/solr_sunspot_example with a config/solrconfig.xml file. We need to overwrite it with the one from sunspot, together with the schema.xml. To get those specific files, head over to the source code and copy them to /var/solr/data/solr_sunspot_example/conf in the production server. Once that is done, restart Solr:

service solr restart

Finally, edit the config/sunspot.yml file in your project to match the new setup:

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

That’s it! The Rails app should be able to connect to Solr. If your database was already populated, run rake sunspot:reindex.

If still feels like too much, I’ve automated this process using Ansible. I’ve also prepared a sample Rails application, which demonstrates all steps of this setup simply by running vagrant up.

References