SOLR configurations are described on the Internet, but implementation is always difficult. The following is a summary:
1.Download the SOLR core source code package from the official website, put the solrwar package under the webapps folder of Tomcat, and other Web containers.
Run Tomcat and extract the SOLR folder.
2.Single Core configuration: Explains with the SOLR example
2.1.Create the solrhome Directory: D:/test/solrcore/singlecore. Copy the SOLR package from the example package of SOLR source code.
2.2.Modify
<env-entry-name>solr/home</env-entry-name>
<env-entry-value> D:/test/solrcore/singlecore</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
2.3.Modify the solrconfig. xml file of conf under SOLR in the solrhome directory
<dataDir>D:/test/solrcore/data/singlecore</dataDir>
This directory contains SOLR data index files.
2.4.Start the SOLR console.
3.Multicore configuration: Take the SOLR core source code multicore as an Example
3.1.Create the solrhome Directory: D:/test/solrcore/multicore. Copy the multicore directory from example of SOLR source code to solrhome.
3.2.Modify
<env-entry-name>solr/home</env-entry-name>
<env-entry-value> D:/test/solrcore/multicore</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
3.2.Modify the solrconfig. xml file of conf under SOLR in the solrhome directory
<Datadir> D:/test/solrcore/data/multicore </datadir>
3.3.Put the index files of core0 and core1 in the d:/test/solrcore/data/multicore directory.
3.4.Start the SOLR console and you can see two cores. The installation is complete.
4.Use embeddedsolrserver
4.1.Singlecore usage:
package com.taobao.terminator.allen.SolrjTest;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.core.CoreContainer;
import org.junit.Test;
public class EmbedSolrServerSingleCoreTest {
private static CoreContainer.Initializer initializer = null;
private static CoreContainer coreContainer = null;
private static EmbeddedSolrServer server = null;
static {
try {
System.setProperty("solr.solr.home", "D://test//solrcore//core0");
initializer = new CoreContainer.Initializer();
coreContainer = initializer.initialize();
server = new EmbeddedSolrServer(coreContainer, "");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void query() throws Exception {
try {
SolrQuery q = new SolrQuery();
q.setQuery("*:*");
q.setStart(0);
q.setRows(20);
SolrDocumentList list = server.query(q).getResults();
System.out.println(list.getNumFound());
} catch (Exception e) {
e.printStackTrace();
} finally {
coreContainer.shutdown();
}
}
@Test
public void deleteAllDoc() throws Exception {
try {
server.deleteByQuery("*:*");
server.commit();
query();
} catch (Exception e) {
e.printStackTrace();
} finally {
coreContainer.shutdown();
}
}
}
4.2.Multicore usage:
package com.taobao.terminator.allen.SolrjTest;
import java.io.File;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.core.CoreContainer.Initializer;
public class EmbedSolrServerMultiCoreTest {
private static CoreContainer.Initializer initializer = null;
private static CoreContainer coreContainer = null;
private static EmbeddedSolrServer server = null;
static {
try {
System.setProperty("solr.solr.home", "D://test//solrcore//core1");
initializer = new CoreContainer.Initializer();
coreContainer = initializer.initialize();
server = new EmbeddedSolrServer(coreContainer, "");
} catch (Exception e) {
e.printStackTrace();
}
}
public void query() throws Exception {
try {
File f = new File( "D:/test/solrcore/multicore", "solr.xml" );
coreContainer = new Initializer().initialize();
coreContainer.load("D:/test/multicore", f);
coreContainer.setPersistent(true);
server = new EmbeddedSolrServer(coreContainer, "core1");
SolrQuery q = new SolrQuery();
q.setQuery("*:*");
System.out.println(server.query(q).getResults().getNumFound());
} catch (Exception e) {
e.printStackTrace();
} finally {
coreContainer.shutdown();
}
}
}
(Source: Go to http://blog.csdn.net/flyingpig4/article/details/6366414basic for more information)