Tuesday, August 21, 2012

Configuring Drupal search_api_solr module

In my previous post I described in detail how to set up Solr.
In order to install Drupal search_api_solr we need to install the module using Drush or some other way.
After installation is complete, we need to move schema.xml and solrconfig.xml from module to solr.
We define Solr cores in solr.xml on the Solr root /var/solr in our case.
Sample configuration is:
<?xml version="1.0" encoding="UTF-8" ?>

<solr persistent="false">

  <!--
  adminPath: RequestHandler path to manage cores.  
    If 'null' (or absent), cores will not be manageable via request handler
  -->
  <cores adminPath="/admin/cores" defaultCoreName="core0">
    <core name="core0" instanceDir="core0" />
  </cores>
</solr>

Now we move schema.xml and solrconfig.xml from module to solr.
cp module_installation_folder/search_api_solr/schema.xml /var/solr/core0/conf/.
cp module_installation_folder/search_api_solr/solrconfig.xml /var/solr/core0/conf/.
If we check schema.xml we will see that we also need to define /var/solr/core0/conf/protwords.txt
&amp;
&lt;
&gt;
&#039;
&quot;
and we must download mapping-ISOLatin1Accent.txt to map non ascii chars to their ASCII equivalent.
In Drupal go to http://localhost/admin/config/search/search_api/server/solr_server/edit to tell Drupal where your Solr is.
You can test your installation by visiting http://locahost:8983/solr/ as we defined it in Tomcat context as described in previous post.
All we need now is to fill Solr with data using drush sapi-i command (thanks Gasper for help).

Installing Solr on Ubuntu

If you want to install Solr on your web server you will need to install some sort of Java application server (JBoss, Jetty, Tomcat ...) to contain the app, that is Solr.

Good place to start is of course the official documentation at Apache and I used this guide to help me get started.

I personally choose Tomcat as my container, because of previous experience.

The exact procedure is:
# install tomcat 
apt-get install tomcat6

# get Solr and extract it
cd /tmp
wget http://archive.apache.org/dist/lucene/solr/3.6.1/apache-solr-3.6.1.tgz
tar xzf apache-solr-3.6.1.tgz

#create folder for Solr and move installation to it 
mkdir -p /var/solr
cp apache-solr-3.6.1/dist/apache-solr-3.6.1.war /var/solr/solr.war
cp -R apache-solr-3.6.1/example/multicore/* /var/solr/
chown -R tomcat6 /var/solr/

#disable tomcat security
echo 'TOMCAT6_SECURITY=no' | sudo tee -a /etc/default/tomcat6

We have now installed Tomcat and Solr but Tomcat is not aware of Solr. We need to define the app context for Tomcat.

In Catalina/localhost define solr.xml (or use any name you like)

vim /etc/tomcat6/Catalina/localhost/solr.xml 
And put following data to it:
<Context path="/solr" docBase="/var/solr/solr.war"
   debug="0" privileged="true" allowLinking="true" crossContext="true">
  <!-- make symlinks work in Tomcat -->
  <Resources className="org.apache.naming.resources.FileDirContext" allowLinking="true" />

  <Environment name="solr/home" type="java.lang.String" value="/var/solr" override="true" />
</Context>
in this file we define that on url /solr (Context path) resides application on location /var/solr/solr.war (docBase). We also define environment variable for java runtime to use.

If you want to define port go to /etc/tomcat6/server.xml and look for Connector port

<Connector port="8983" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               URIEncoding="UTF-8"
               redirectPort="8443" />
Set it to value of your desire.

To make thing simple I compiled a simple shell script to do all of the above:

#!/bin/bash

echo "1. Installing tomcat6"
sudo apt-get install tomcat6

echo "2. change ownership of files"
sudo chown -R tomcat6 /var/solr/

echo "3.tomcat6 configuratin"
echo 'TOMCAT6_SECURITY=no' | sudo tee -a /etc/default/tomcat6
sudo sed -i 's/Connector port="8080"/Connector port="8983"/g' /etc/tomcat6/server.xml

sudo cat > /tmp/solr.xml  << NIZ
<Context path="/solr" docBase="/var/solr/solr.war"
   debug="0" privileged="true" allowLinking="true" crossContext="true">
  <!-- make symlinks work in Tomcat -->
  <Resources className="org.apache.naming.resources.FileDirContext" allowLinking="true" />

  <Environment name="solr/home" type="java.lang.String" value="/var/solr" override="true" />
</Context>
NIZ

sudo mv /tmp/solr.xml /etc/tomcat6/Catalina/localhost/solr.xml

echo "4. Restarting tomcat6"
sudo service tomcat6 restart