Thursday, 27 February 2014

Install Apache Solr 4.2 on Ubuntu 12.04 with Tomcat 7 and Sunspot


Install Tomcat
sudo apt-get install tomcat7 tomcat7-admin

Setup tomcat manager
sudo vim /etc/tomcat7/tomcat-users.xm
<tomcat-users>
    <role rolename="manager"/>
    <user username="tomcat" password="yourSecret" roles="manager"/>
</tomcat-users> 
Restart Tomcat
sudo service tomcat7 restart
By default Tomcat run on port 8080 therefore go to: http://youripaddress:8080/manager/index and it will prompt for tomcat username and password, set in tomcat-users.xml

Install Solr

Download solr 4.2.0  from the Apache archives, as sunspot support 4.2.0 version of solr up till now. 
wget http://archive.apache.org/dist/lucene/solr/4.2.0/solr-4.2.0.tgz

Extract the solr-4.2.0
tar -zxvf solr-4.2.0.tgz
Move or copy solr to /usr/share/solr
sudo cp solr-4.2.0 /usr/share/solr/
Now we have to copy solr.xml from example/webapps to example/solr
sudo cp /usr/share/solr/example/webapps/solr.war /usr/share/solr/example/solr/solr.war

Now again go to http://youripaddress:8080/manager/index and check that solr tab on left side will be green, now more options will be appear to reload, stop and undeploy 

Integrating Solr with Sunspot
till today(27 Feb, 2014) sunspot is shipped with solr 4.2, default schema.xml file of 4.2 version of solr is not compatible with sunspot latest version 2.1.0 and sunspot_rails version 2.1.0

therefore we will take a backup of original schema.xml file on server and create schema.xml file which is shipped with sunspot_rails to server

taking backup
sudo cp /usr/share/solr/example/solr/collection1/conf/schema.xml /usr/share/solr/example/solr/collection1/conf/schema.xml-orig

removing schema.xml
sudo rm /usr/share/solr/example/solr/collection1/conf/schema.xml

now copy schema.xml content from https://github.com/sunspot/sunspot/blob/master/sunspot_solr/solr/solr/conf/schema.xml
create new schema.xml file and paste the content in it
sudo nano /usr/share/solr/example/solr/collection1/conf/schema.xml
ctrl + shift + v
save changes and restart Tomcat
sudo service tomcat7 restart
Now we have done with it, reindex your data and move on :)

Summary

We can work with latest supported versions of sunspot by following the same pattern just download the latest supported version of solr shipped with sunspot and use the schema.xml file shipped with it