Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Deploying Apache Solr 4.10.3 with Tomcat and IK Analyzer on Windows and Linux

Tech Jul 25 1

Overview

Apache Solr is a high-performance, Lucene-based search platform written in Java. It exposes a rich HTTP/REST API for indexing and querying, runs inside any servlet container, and keeps its configuration externalized in a directory called solrhome. This guide walks through a complete setup on both Windows and Linux, adds Chinese tokenization with IK Analyzer, and finishes with a minimal Java client example.

  1. Download and Unpack

Grab the 4.10.3 distribution from http://lucene.apache.org/solr/. After unpacking you will see:

  • bin/ – start scripts
  • contrib/ – extra plugins
  • dist/solr-4.10.3.war and dependency JARs
  • example/ – ready-made Solr cores and Jetty launcher
  • docs/ – Javadocs
  1. Windows Installation with Tomcat 7

Prerequisites

  • JDK 1.7+
  • Tomcat 7.0.x (tested with 7.0.53)

Steps

  1. Copy dist/solr-4.10.3.war to $CATALINA_HOME/webapps/solr.war.

  2. Start Tomcat once to let the WAR explode, then remove the original solr.war.

  3. Copy every JAR from example/lib/ext into $CATALINA_HOME/webapps/solr/WEB-INF/lib.

  4. Create $CATALINA_HOME/webapps/solr/WEB-INF/classes and drop example/resources/log4j.properties inside.

  5. Create a solrhome directory anywhere (e.g. D:\solrhome) and copy the contants of example/solr into it.

  6. Edit $CATALINA_HOME/webapps/solr/WEB-INF/web.xml and add the JNDI entry: ``` solr/home D:/solrhome java.lang.String

  7. Restart Tomcat and open http://localhost:8080/solr.

  8. Linux Installation with Tomcat 7


# 1. Unpack Tomcat
tar -zxf apache-tomcat-7.0.47.tar.gz
mkdir /usr/local/solr
mv apache-tomcat-7.0.47 /usr/local/solr/tomcat

# 2. Upload and unzip Solr
tar -zxf solr-4.10.3.tgz

# 3. Deploy WAR
cp solr-4.10.3/dist/solr-4.10.3.war /usr/local/solr/tomcat/webapps/solr.war
# Start/stop Tomcat to explode, then
rm /usr/local/solr/tomcat/webapps/solr.war



# 4. Runtime libs
cp solr-4.10.3/example/lib/ext/* /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/

# 5. Create solrhome
cp -r solr-4.10.3/example/solr /usr/local/solr/solrhome

# 6. Point webapp to solrhome
vim /usr/local/solr/tomcat/webapps/solr/WEB-INF/web.xml
# Add the same <env-entry> as above with value /usr/local/solr/solrhome

# 7. Start
/usr/local/solr/tomcat/bin/startup.sh
  1. Addding IK Analyzer for Chinese Text

  1. Upload IKAnalyzer2012FF_u1.jar to $CATALINA_HOME/webapps/solr/WEB-INF/lib.

  2. Create $CATALINA_HOME/webapps/solr/WEB-INF/classes and copy:

    • IKAnalyzer.cfg.xml
    • ext_stopword.dic
    • mydict.dic

    Ensure all files are UTF-8 without BOM.

  3. Edit solrhome/collection1/conf/schema.xml and append: ``` <fieldType name="text_ik" class="solr.TextField"> </fieldType>

  4. Restart Tomcat.

  5. Defining Product Fields


Inside the same schema.xml, add the following before the closing </schema> tag:

<field name="item_title"        type="text_ik" indexed="true" stored="true"/>
<field name="item_sell_point"   type="text_ik" indexed="true" stored="true"/>
<field name="item_price"        type="long"    indexed="true" stored="true"/>
<field name="item_image"        type="string"  indexed="false" stored="true"/>
<field name="item_category_name" type="string"  indexed="true" stored="true"/>
<field name="item_desc"         type="text_ik" indexed="true" stored="false"/>

<field name="item_keywords"     type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="item_title"          dest="item_keywords"/>
<copyField source="item_sell_point"     dest="item_keywords"/>
<copyField source="item_category_name"    dest="item_keywords"/>
<copyField source="item_desc"           dest="item_keywords"/>
  1. Java Client Quick-Start

Add the SolrJ dependency (already bundled in dist/) and run:

import org.apache.solr.client.solrj.*;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.*;
import org.junit.Test;

public class SolrClientDemo {

    private static final String SOLR_URL = "http://192.168.1.123:8080/solr";

    @Test
    public void insertDoc() throws Exception {
        HttpSolrServer server = new HttpSolrServer(SOLR_URL);
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", "sku123");
        doc.addField("item_title", "高性能笔记本电脑");
        doc.addField("item_price", 5999);
        server.add(doc);
        server.commit();
    }

    @Test
    public void searchDocs() throws Exception {
        HttpSolrServer server = new HttpSolrServer(SOLR_URL);
        SolrQuery q = new SolrQuery("item_keywords:笔记本");
        q.setStart(0).setRows(10);
        QueryResponse resp = server.query(q);
        for (SolrDocument d : resp.getResults()) {
            System.out.println(d.get("id") + " - " + d.get("item_title"));
        }
    }

    @Test
    public void purgeIndex() throws Exception {
        HttpSolrServer server = new HttpSolrServer(SOLR_URL);
        servererver.deleteByQuery("*:*");
        server.commit();
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.