Solr for Drupal Developers, Part 3: Testing Solr locally

Posts in this series:

In earlier Solr for Drupal Developers posts, you learned about Apache Solr and it's history in and integration with Drupal. In this post, I'm going to walk you through a quick guide to getting Apache Solr running on your local workstation so you can test it out with a Drupal site you're working on.

The guide below is for those using Mac or Linux workstations, but if you're using Windows (or even if you run Mac or Linux), you can use Drupal VM instead, which optionally installs Apache Solr alongside Drupal.

As an aside, I am writing this series of blog posts from the perspective of a Drupal developer who has worked with large-scale, highly customized Solr search for Mercy (example), and with a variety of small-to-medium sites who are using Hosted Apache Solr, a service I've been running as part of Midwestern Mac since early 2011.

Installing Apache Solr in a Virtual Machine

Apache Solr can be run directly from any computer that has Java 1.7 or later, so technically you could run it on any modern Mac, Windows, or Linux workstation natively. But to keep your local workstation cleaner, and to save time and hassle (especially if you don't want to kludge your computer with a Java runtime!), this guide will show you how to set up an Apache Solr virtual machine using Vagrant, VirtualBox, and Ansible.

Let's get started:

  1. Clone the ansible-vagrant-examples project from GitHub (you can also download ansible-vagrant-examples directly).
  2. Change directory in Terminal to the /solr subdirectory, and follow the instructions in the Solr example's README for installing Vagrant, VirtualBox, and Ansible, then follow the rest of the instructions for building that example (e.g. vagrant up).
  3. At this point, if you visit in your browser, you should see the Apache Solr admin interface:
    Apache Solr Administration Dashboard - 4.10
  4. The next step is to point your local Drupal installation (assuming you have a Drupal site running locally) at this Solr instance and make sure it can connect. We're using the Apache Solr Search module in this example, but Search API Solr Search setup is similar.
    1. Visit /admin/config/search/apachesolr/settings, and click 'Add search environment'.
    2. Enter (this is the default search core that Apache Solr includes out of the box) for 'Solr server URL', check the checkbox to make this the default environment, add a description (e.g. 'Local Solr server'), and click 'Save':
      Drupal Apache Solr module search environment configuration form
    3. After saving the new environment, the next page should show the environment with a green-colored background. That means your Drupal site can connect to the Solr server.
  5. After Drupal is able to connect, you need to add the Drupal module's Solr configuration files to the search core you'll be using. This takes a few steps, but will ensure all your Drupal content is indexed by Solr correctly.
    1. Change directory in Terminal to the /solr directory (where you ran vagrant up earlier), and run vagrant ssh to log into the Solr VM.
    2. While logged into the VM, enter the following commands:
      1. curl | tar -xz (download the Apache Solr module into the current directory).
      2. sudo cp -r apachesolr/solr-conf/solr-4.x/* /var/solr/collection1/conf/ (copy the Apache Solr module configuration into the default Solr core).
      3. sudo chown -R solr:solr /var/solr/collection1/conf/* (fix permissions for the copied files).
      4. sudo service solr restart (restart Apache Solr so the configuration is updated).
    3. Once this is complete, go back to the Apache Solr search settings page (/admin/config/search/apachesolr/settings), and click on the 'Index' configuration in your local solr server row. You should see something like drupal-4.3-solr-4.x for the 'Schema', meaning the Drupal module schema.xml has been picked up successfully.

At this point, you should be able to index your site content into Apache Solr (scroll down and check some content types you want to index), and start playing around with Apache Solr search!

The best first steps are to look around in all the Apache Solr configuration pages, test indexing your entire site, then work on setting up search pages and maybe even install the Facet API module to configure some search facets. In very little time, you should be able to make your site search as user-friendly and speedy as Amazon, Newegg, etc.

Further Reading