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.








Tuesday, March 17, 2015

How to setup Solr on Linux machine?

System Environment
Linux Version: 2.6.32-358.23.2.el6.x86_64
Solr Version: 5.0.0

Need to upgrade JDK to version 1.7.0.55 and above for Solr to work

1. Download JDK version 1.7.0_75 (jdk-7u75-linux-x64.tar.gz)
2. Copy jdk-7u75-linux-x64.tar.gz to "/usr/lib" directory
3. Run the command to untar the zip files
tar xzf jdk-7u75-linux-x64.tar.gz
4. Run  the command below to install the JDK
alternatives --install /usr/bin/java java /usr/lib/jdk1.7.0_75/bin/java 2
5. Run the command below to select the java command to use
alternatives --config java

There are 3 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
   2           /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java
   3           /usr/lib/jdk1.7.0_75/bin/java

Enter to keep the current selection[+], or type selection number: 3
6. Run the command below to install

alternatives --install /usr/bin/jar jar /usr/lib/jdk1.7.0_75/bin/jar 2
alternatives --install /usr/bin/javac javac /usr/lib/jdk1.7.0_75/bin/javac 2
alternatives --set jar /usr/lib/jdk1.7.0_75/bin/jar
alternatives --set javac /usr/lib/jdk1.7.0_75/bin/javac 

Solr Installation

1. Download Solr from Apache website. Note: "solr-5.0.0.tgz" has been downloaded
2. Copy solr-5.0.0.tgz to "/opt" directory
3. Run the command below to unzip the Solr installer
tar zxf solr-5.0.0.tgz
3. Run the command below to start the Solr instance. The default port is 8983
# cd /opt/solr-5.0.0/
# bin/solr start
4. To shutdown the Solr instance, run the command below.
# bin/solr stop

Open Firewall to allow port 8983

1. Run the command below to open port 8983 on the machine
# iptables -I INPUT -p tcp -m tcp --dport 8983 -j ACCEPT
#service iptables save
2. Type the URL (http://[server_address]:8983/) in any browser, Solr admin screen will be displayed.