Wednesday, March 18, 2015

How to setup Solr on Linux machine (1) ?

System Environment
Linux Version: 2.6.32-358.23.2.el6.x86_64
Solr Version: 5.0.0
Postgresql database version: 8.4

Configure a new Core

1. Copy the basic configuration from "/opt/solr-5.0.0/server/solr/configsets/basic_configs" directory to "/opt/solr-5.0.0/server/solr/configsets" directory.

# cd /opt/solr-5.0.0/server/solr/configsets
# cp -rf basic_configs ../
2. Rename the directory as new application name, for example, "myapp".

# cd ..
# mv basic_configs myapp
3. Open the server admin module "http://localhost:8983/solr/", fill in the information

name: myapp
instanceDir: myapp
dataDir: data
config: solrconfig.xml
schema: schema.xml


4. Click on "Add Core" button, new core will be added.

Index data from Postgresql database use data import handler

1. Download Posgresql database driver (postgresql-8.4-703.jdbc4.jar) and place it in "/opt/solr-5.0.0/contrib/dataimporthandler/lib" directory.

2. Make sure jar files as listed below exists in "/opt/solr-5.0.0/dist" directory.
- solr-dataimporthandler-5.0.0.jar
- solr-dataimporthandler-extras-5.0.0.jar

3. To index the data from "booktown" database "books" table, edit solrconfig.xml located at "/opt/solr-5.0.0/server/solr/myapp/conf" directory

<lib dir="/opt/solr-5.0.0/contrib/dataimporthandler/lib" regex=".*\.jar"/>
<lib dir="/opt/solr-5.0.0/dist/" regex="solr-dataimporthandler-.*\.jar"/>
...
...
...
<requesthandler class="org.apache.solr.handler.dataimport.DataImportHandler" name="/dataimport">
 <lst name="defaults">
  <str name="config">db-data-config.xml</str>
 </lst>
</requesthandler>

4.Create db-data-config.xml at  "/opt/solr-5.0.0/server/solr/myapp/conf" directory. Below are sample configuration file.

<dataConfig>
  <dataSource type="JdbcDataSource" 
              driver="org.postgresql.Driver"
              url="jdbc:postgresql://localhost:5432/booktown" 
              user="solr" 
              password="solr"/>
  <document>
    <entity name="books"  
      pk="id"
      query="select id, title from books"
      deltaImportQuery="SELECT id, title from books WHERE id='${dih.delta.id}'"
      >
       <field column="id" name="id"/>
       <field column="title" name="title"/>       
    </entity>
  </document>
</dataConfig>

5. Edit schema.xml located at "/opt/solr-5.0.0/server/solr/myapp/conf".

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 
<field name="title" type="text_general" indexed="true" stored="true"/>

6. Open the Solr browser, select "myapp" in the dropdown box. Click on "Data Import" and "Execute" button. Click on "Refresh status" button. Data from Postgresql will be indexed.


7. Click on "Query" menu follow by "Execute Query" button. The search will be displayed.








No comments: