Adeithzya’s Weblog

Using Apache SOLR with Spring Framework

Posted in Uncategorized by adeithzya on August 25, 2011

This documentation is about the steps required to use Apache SOLR search engine with Spring framework in Windows OS. I was assigned to use SOLR search engine in my previous project, and I would like to keep that knowledge here. It is intended solely for my self as a reference, if I ever needed to perform such task in the future, then here they are:

#1. Setup Apache SOLR

To setup SOLR, first you need to download the software here¬† the version as the time when I’m writing this is 3.3, then after finished with the download process, extract the .zip file into a folder e.g. “C:\solr” resulting in the following folder structure:
SOLR Folder Structure

Browse to “C:\solr\apache-solr-3.3.0\example\solr”, this folder should have 3 sub folder and 2 file like this:
SOLR Template file

Copy the entire content of it into a new folder e.g. “C:\my_solr_app”, this copy serve as a template and later it can be configured if needed by editing the solr.xml file. Then browse to “C:\solr\apache-solr-3.3.0\dist” and copy file “apache-solr-3.3.0.war” into the same folder of “C:\my_solr_app”. Here are the final content of this folder:
SOLR All required files

I will be using Apache Tomcat as the web server for SOLR, hence browse to Tomcat configuration directory, in my case¬† it’s “C:\apache-tomcat-6.0.32\conf\Catalina\localhost” add new .xml file “my-solr-app.xml” (it can be any name). Here are the content of the .xml file:

<?xml version="1.0" encoding="utf-8"?>
<Context docBase="C:/my_solr_app/apache-solr-3.3.0.war" debug="0" crossContext="true">
<Environment name="solr/home" type="java.lang.String" value="C:/my_solr_app" override="true"/>
</Context>

Start Apache Tomcat and open a browser to the “http://localhost:8080/my-solr-app/&#8221;, you may need to change the port number configured for Tomcat web server. If you see the welcome screen, then you’ve just finished Apache SOLR setup.

#2. Spring Framework configuration

Browse to “C:\solr\apache-solr-3.3.0\dist” and copy file “apache-solr-solrj-3.3.0.jar” into the project classpath. You may also need to copy or download the dependency .jar file. Then Create a new bean inside Spring configuration file like this:

<bean id="solrServer" class="org.apache.solr.client.solrj.impl.CommonsHttpSolrServer">
<constructor-arg value="${solr.serverUrl}"/>
<property name="connectionTimeout" value="${solr.connectionTimeout}"/>
<property name="defaultMaxConnectionsPerHost" value="${solr.defaultMaxConnectionsPerHost}"/>
<property name="maxTotalConnections" value="${solr.maxTotalConnections}"/>
</bean>

#3. Using “solrServer” Spring bean in Service layer

Create org.apache.solr.client.solrj.SolrServer object as one of the member variable in the Service, using spring IOC mechanism. Use the following code snipet to interact with SOLR:

Query:
SolrQuery query = new SolrQuery();
query.setQuery(search);
QueryResponse qr = solrServer.query(query);
return qr.getBeans(SearchItem.class);


Post Item:
solrServer.addBean(item);
solrServer.commit();


Delete Item:
solrServer.deleteById(id);
solrServer.commit();

Refer to Solrj documentation here for more detail on how to interact with SOLR.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: