Enterprise Java

Setting up and playing with Apache Solr on Tomcat

A while back a had a little time to play with Solr, and was instantly blown away by the performance we could achieve on some of our bigger datasets.
Here is some of my initial setup and configuration learnings to maybe help someone get it up and running a little faster.
Starting with setting both up on windows.

Download and extract Apache Tomcat and Solr and copy into your working folders.

Tomcat Setup

If you want tomcat as a service install it using the following:
bin\service.bat install
Edit the tomcat users under conf.:

<role rolename="admin"/>
<role rolename="manager-gui"/>
<user username="tomcat" password="tomcat" roles="admin,manager-gui"/>

If you are going to query Solr using international characters (>127) using HTTP-GET, you must configure Tomcat to conform to the URI standard by accepting percent-encoded UTF-8. Add: URIEncoding=’UTF-8′

<connector port="8080" protocol="HTTP/1.1"
URIEncoding="UTF-8" />

to the conf/server.xml

Copy the contents of the example\solr your solr home directory D:\Java\apache-solr-3.6.0\home
create the code fragment on $CATALINA_HOME/conf/Catalina/localhost/solr.xml pointing to your solr home.

<?xml version="1.0" encoding="UTF-8"?>
<context docBase="D:\Java\apache-tomcat-7.0.27\webapps\solr.war" debug="0" crossContext="true" >
    <environment name="solr/home" type="java.lang.String" value="D:\Java\apache-solr-3.6.0\home" override="true" />

Startup tomcat, login, deploy the solr.war.

Solr Setup

It should be available at http://localhost:8080/solr/admin/ To create a quick test using SolrJ the creates and reads data: Grab the following Maven Libs:


JUnit test:

package za.co.discovery.ecs.solr.test;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
public class TestSolr {
    private SolrServer server;
     * setup.
    public void setup() {
        server = new HttpSolrServer("http://localhost:8080/solr/");
        try {
        } catch (SolrServerException e) {
        } catch (IOException e) {
     * Test Adding.
     * @throws MalformedURLException error
    public void testAdding() throws MalformedURLException {
        try {
            final SolrInputDocument doc1 = new SolrInputDocument();
            doc1.addField("id", "id1", 1.0f);
            doc1.addField("name", "doc1", 1.0f);
            doc1.addField("price", 10);
            final SolrInputDocument doc2 = new SolrInputDocument();
            doc2.addField("id", "id2", 1.0f);
            doc2.addField("name", "doc2", 1.0f);
            doc2.addField("price", 20);
            final Collection<solrinputdocument> docs = new ArrayList<solrinputdocument>();
            final SolrQuery query = new SolrQuery();
            query.addSortField("price", SolrQuery.ORDER.asc);
            final QueryResponse rsp = server.query(query);
            final SolrDocumentList solrDocumentList = rsp.getResults();
            for (final SolrDocument doc : solrDocumentList) {
                final String name = (String) doc.getFieldValue("name");
                final String id = (String) doc.getFieldValue("id"); //id is the uniqueKey field
                System.out.println("Name:" + name + " id:" + id);
        } catch (SolrServerException e) {
        } catch (IOException e) {

Adding data directly from the DB

Firstly you need to add the relevant DB libs to the add classpath. Then create data-config.xml as below, if you require custom fields, those can be specified under the fieldstag in the schema.xml shown below the dataconfig.xml

 <datasource name="jdbc" driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1525:DB" user="user" password="pass"/>
    <document name="products">
        <entity name="item" query="select * from demo">
            <field column="ID" name="id" />
            <field column="DEMO" name="demo" />
            <entity name="feature" query="select description from feature where item_id='${item.ID}'">
                <field name="features" column="description" />
            <entity name="item_category" query="select CATEGORY_ID from item_category where item_id='${item.ID}'">
                <entity name="category" query="select description from category where id = '${item_category.CATEGORY_ID}'">
                    <field column="description" name="cat" />

A custom field in the schema.xml:

  <field name="DEMO" type="string" indexed="true" stored="true" required="true" /> 

Add in the solrconfig.xml make sure to point the the data-config.xml, the handler has to be registered in the solrconfig.xml as follows

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

Once that is all setup a full import can be done with the following:

Then you should be good to go with some lightning fast data retrieval.

Reference: Setting up and playing with Apache Solr on Tomcat from our JCG partner Brian Du Preez at the Zen in the art of IT blog.

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
9 years ago

Please send video demo if possible ” Setting up and playing with Apache Solr on Tomcat ” and the procedure of this how can use it maven project.

Back to top button