Tuesday, May 27, 2014

Sitecore and Solr: Configure DataImportHandler for External Data Extraction

These instructions are based on a Sitecore 7.0 installation of SOLR and will use the folder structures based on it with Jetti, SOLR 4.5, multiple cores, etc.  It will also assume the installation of SOLR is up and running.

1) Edit the solrconfig.xml file for the current core
Example: \example\solr\core\conf\

2) Add new request handler:

----------- 
<!-- DataImporter -->
  <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
    <lst name="defaults">
      <str name="config">data-config.xml</str>
    </lst>
  </requestHandler>
------------

3) Create new file "data-config.xml" in the same folder or wherever the path is as specified in the requestHandler

Example of file content:

<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource name="ds1"
            type="JdbcDataSource"
            driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"
            url="jdbc:sqlserver://server;databaseName=dbname"
            user="user"
            password="pwd"
            readOnly="true" />
    <document>
        <entity name="user"
            dataSource="ds1"
            query="select * from [dbo].[users]"
            >
            <field column="id" name="_id" />
            <field column="email" name="_email" />           
            <field column="first_name" name="_fname" />
            <field column="last_name" name="_lname" />       
        </entity>
    </document>
</dataConfig>

Datasource type is jdbcdatasource
Driver is com.microsoft.sqlserver.jdbc.SQLServerDriver
Url format is jdbc:sqlserver://server;databaseName=dbname
Also make sure column names do not have spaces or weird characters

4) Make sure the dataimport jar files are in place.  You can get the solr-dataimporthandler*.jar from the dist folder.

Copy the files into:
\example\solr-webapp\webapp\WEB-INF\lib\


5) Install SQL Server JDBC driver
http://msdn.microsoft.com/en-us/sqlserver/aa937724

Run the downloaded installer and you will be prompted to unzip the files to a location. 
Unzip to any location.

In the unzipped folder look for \sqljdbc_3.0\enu\sqljdbc4.jar and copy to:

\example\solr-webapp\webapp\WEB-INF\lib\


6) Verify that everything has worked by browsing to you SOLR admin URL and selecting the core with the dataimporter.  Click the dataimport tab and execute.  If all is well, your indexes would be created and you can perform queries as usual.


No comments:

Post a Comment