Monday, January 16, 2012

Apache Solr Install Notes


I started working on full text searching solution for our map application. No surprise, Apache Solr is the selected solution and here are some quick notes for you to get started with it.

Part 1 - Basic Setup

1. Download Solr from: http://www.apache.org/dyn/closer.cgi/lucene/solr/
2. Unzip to: C:\Program Files\Apache Software Foundation\Solr (ensure that you rename the solr download to Solr)
3. Stop Tomcat
4. Copy C:\Program Files\Apache Software Foundation\Solr\dist\apache-solr-1.4.1.war to: C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps
5 Rename: C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\apache-solr-1.4.1.war to Solr.war
6. Open: C:\Program Files\Apache Software Foundation\Tomcat 6.0\bin\tomcat6w.exe goto Java tab and add Java options: -Dsolr.solr.home=C:\Program Files\Apache Software Foundation\Solr\example\solr
7. Start Tomcat and go to: http://localhost:8080/Solr/admin/

Part 2 - Configuring and running the DataImportHandler 

1. Added the following to file: C:\Program Files\Apache Software Foundation\Solr\example\solr\conf\solrconfig.xml
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
    <lst name="defaults">
      <str name="config">data-config.xml</str>
    </lst>
</requestHandler>
2. Downloaded sqljdbc.jar and sqljdbc4.jar into C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\solr\WEB-INF\lib
3.Create data-config.xml in C:\Program Files\Apache Software Foundation\Solr\example\solr\conf\
   Add to data-config.xml: 
<dataConfig>
 <dataSource type="JdbcDataSource"
  driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"
  url="jdbc:sqlserver://localhost\\sqlserver08;databaseName=Business Objects"
  user="sa" password="sqlserver08" />
 <document>
  <entity name="address"
   query="select OBJECTID,HOUSENO,StreetName,Suburb from Address">
   <field column="OBJECTID" name="id" />
   <field column="HOUSENO" name="house_number" />
   <field column="StreetName" name="street" />
   <field column="Suburb" name="Suburb" />
  </entity>
 </document>
</dataConfig> 

4. Added to schema.xml (C:\Program Files\Apache Software Foundation\Solr\example\solr\conf) in:
   <field name="house_number" type="string" indexed="true" stored="true"/>
   <field name="street" type="string" indexed="true" stored="true"/>
   <field name="Suburb" type="string" indexed="true" stored="true"/>
5. Run: http://localhost:8080/Solr/dataimport?command=full-import 
    You can use the interface at :  http://localhost:8080/Solr/admin/dataimport.jsp?handler=/dataimport
6. Test: http://localhost:8080/Solr/select?q=TYTUS or use http://localhost:8080/Solr/admin/form.jsp

Part 3 - Querying 

http://localhost:8088/solr/db/select/?q=house_number:41&version=2.2&start=0&rows=50&indent=on

Faceting:  http://localhost:8080/Solr/select?indent=on&version=2.2&q=house_number:41&facet=true&facet.field=Suburb

You can facet on multiple fields by appending &facet.field=<field name> The key to faceting is giving all your documents a category or type.