Deploying Apache Solr 4.10.3 with Tomcat and IK Analyzer on Windows and Linux
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.
- Download and Unpack
Grab the 4.10.3 distribution from http://lucene.apache.org/solr/. After unpacking you will see:
bin/– start scriptscontrib/– extra pluginsdist/–solr-4.10.3.warand dependency JARsexample/– ready-made Solr cores and Jetty launcherdocs/– Javadocs
- Windows Installation with Tomcat 7
Prerequisites
- JDK 1.7+
- Tomcat 7.0.x (tested with 7.0.53)
Steps
-
Copy
dist/solr-4.10.3.warto$CATALINA_HOME/webapps/solr.war. -
Start Tomcat once to let the WAR explode, then remove the original
solr.war. -
Copy every JAR from
example/lib/extinto$CATALINA_HOME/webapps/solr/WEB-INF/lib. -
Create
$CATALINA_HOME/webapps/solr/WEB-INF/classesand dropexample/resources/log4j.propertiesinside. -
Create a
solrhomedirectory anywhere (e.g.D:\solrhome) and copy the contants ofexample/solrinto it. -
Edit
$CATALINA_HOME/webapps/solr/WEB-INF/web.xmland add the JNDI entry: ``` solr/home D:/solrhome java.lang.String -
Restart Tomcat and open
http://localhost:8080/solr. -
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
- Addding IK Analyzer for Chinese Text
-
Upload
IKAnalyzer2012FF_u1.jarto$CATALINA_HOME/webapps/solr/WEB-INF/lib. -
Create
$CATALINA_HOME/webapps/solr/WEB-INF/classesand copy:IKAnalyzer.cfg.xmlext_stopword.dicmydict.dic
Ensure all files are UTF-8 without BOM.
-
Edit
solrhome/collection1/conf/schema.xmland append: ``` <fieldType name="text_ik" class="solr.TextField"> </fieldType> -
Restart Tomcat.
-
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"/>
- 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();
}
}