Sunday, May 04, 2008

Installation and Configuration of LocalSolr.

A client of mine asked me to investigate localsolr which= extends Apache Solr server with the ability to perform text searches filtered on geographical distance from a given point specified by latitude and longitude. Here are the steps that I followed:

Download the Software

  1. Downland Ant from
  2. Download Tomcat from
  3. Download LocalLucene from
  4. Download LocalSolr from
  5. Download Solr-1.3-dev, via svn, to get the source code
    svn checkout
  6. Uncompress all of the software packages.

Create Environment Variables

  1. In Unix variants, update your .bash_login script to include the following. Of course, you'll need to set the variables correctly according to where you uncompressed the software. And don't forget to source the .bash_login file after you have changed it.
    export SUPPORT_DIR=/home/medined/support
    export LOCAL_LUCENE_HOME=$SUPPORT_DIR/locallucene-r1.5
    export LOCAL_SOLR_HOME=$SUPPORT_DIR/localsolr-r1.5
    export SOLR_HOME=$SUPPORT_DIR/apache-solr-1.3-dev
    export SOLR_CONFIG=/home/medined/.solr
    export TOMCAT_HOME=$SUPPORT_DIR/apache-tomcat-6.0.16
    export JAVA_OPTS="$JAVA_OPTS -Dsolr.solr.home=$SOLR_CONFIG"

Create Apache Solr War

  1. cd $SOLR_HOME
  2. ant dist

Configure Local Solr

  1. cp -R $SOLR_HOME/example/solr $SOLR_CONFIG
  2. cp $SOLR_HOME/libs/solr/apache-solr-1.3-dev.war $TOMCAT_HOME/webapps/solr.war mkdir $SOLR_CONFIG/lib
  3. cp $LOCAL_LUCENE_HOME/dist/locallucene.jar $SOLR_CONFIG/lib
  4. cp $LOCAL_SOLR_HOME/dist/localsolr.jar $SOLR_CONFIG/lib
  5. cp $LOCAL_LUCENE_HOME/lib/gt2-referencing-2.3.1.jar $SOLR_CONFIG/lib
  6. cp $LOCAL_LUCENE_HOME/lib/geoapi-nogenerics-2.1-M2.jar $SOLR_CONFIG/lib
  7. cp $LOCAL_LUCENE_HOME/lib/jsr108-0.01.jar $SOLR_CONFIG/lib
  8. Add the following to the end of $SOLR_CONFIG/conf/solrconfig.xml (just before the closing CONFIG tag:
        <factory name="standard" class="solr.ChainedUpdateProcessorFactory" default="true">
          <chain class="">
            <str name="latField">lat</str>
            <str name="lngField">lng</str>
            <int name="startTier">9</int>
            <int name="endTier">17</int>
          <chain class="solr.LogUpdateProcessorFactory" >
           <!-- <int name="maxNumToLog">100</int> -->
          <chain class="solr.RunUpdateProcessorFactory" />
     <requestHandler name="geo" class="">
        <!-- Custom latitude longitude fields, below are the defaults if not otherwise
        specified -->
         <str name="latField">lat</str>
         <str name="lngField">lng</str>
  9. Add the following to the fields tag of $SOLR_CONFIG/conf/schema.xml
    <field name="lat" type="sdouble" indexed="true" stored="true"/>
       <field name="lng" type="sdouble" indexed="true" stored="true"/>
       <dynamicField name="_local*" type="sdouble" indexed="true" stored="true"/>

Controlling Tomcat

  • $TOMCAT_HOME/bin/ - this command starts Tomcat.
  • tail -f $TOMCAT_HOME/logs/catalina.out - this command lets you watch Tomcat's output log.
  • $TOMCAT_HOME/bin/ - this command stops Tomcat.

If you're lucky enough to be using Unix then combine the first two commands onto one line:

$TOMCAT_HOME/bin/; tail -f $TOMCAT_HOME/logs/catalina.out

Importing Data Into Apache Solr

When LocalSolr is deployed into Tomcat, the default port is 8080. However, the Apache Solr import tools use a different hardcoded port. This causes me a but of angst until I realized that I could easily copy the SimplePostTool and change the port. So copy $SOLR_HOME/src/java/org/apache/solr/util/, change the port specified on line 46 (see below) and compile it.

public static final String DEFAULT_POST_URL = "http://localhost:8080/solr/update";

I create a data file that looked like this

   <field name="id">01</field>
   <field name="name">HOUSE01</field>
   <field name="lat">39.36</field>
   <field name="lng">-77.4027</field>
   <field name="text">zxy</field>
   <field name="id">02</field>
   <field name="name">HOUSE02</field>
   <field name="lat">38.36</field>
   <field name="lng">-77.4027</field>
   <field name="text">zxy</field>

Then I simply executed the SimplePostTool program passing it the name of the data file as the program argument.

Apache Solr Admin Screen

With Tomcat running, you should be able to connect with http://localhost:8080/solr/admin/

Local Solr GIS Query

With Tomcat running, you should be able to execute a GIS-based query by connecting to http://localhost:8080/solr/select?&qt=geo&lat=38.8700&long=-77.4027&q=zxy&radius=1. The q=zxy tells Apache Solr to return all documents. The lat and long parameters indicate the center of the circle to search using decimal degrees. While the radius parameter indicates the radius, in miles, of the circle.

Good Luck!