solr配置与solrj EmbeddedSolrServer使用篇

Solr的配置网上讲的很多,但是实施总是遇到困难,现总结如下

1.从官网下载solr core的源码包,将solrwar包放到tomcat的webapps文件夹下,其它web容器亦然.

    运行tomcat ,解压出来solr的文件夹.

2.Single core配置:以solr自带例子做讲解

2.1.新建solrhome目录:d:/test/solrcore/singlecore ,从solr源码的example包下复制solr包

        2.2.在tomcat 下的 webappss的solr包 WEB-INF目录下修改web.xml的 

<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.修改solrhome目录下的solr下的conf的solrconfig.xml文件

                <dataDir>D:/test/solrcore/data/singlecore</dataDir>

              此目录放置的是solr的data索引文件

        2.4.启动solr控制台,即可

 

3.multicore 配置:以solr core源码的multicore为例

        3.1. 新建solrhome目录:d:/test/solrcore/multicore从solr源码的example下复制multicore目录到在solrhome下

        3.2.  在tomcat 下的 webappss的solr包 WEB-INF目录下修改web.xml的 

                <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.  修改solrhome目录下的solr下的conf的solrconfig.xml文件            

                <dataDir>D:/test/solrcore/data/multicore</dataDir>
        3.3. 将core0和core1的索引文件放到 D:/test/solrcore/data/multicore目录下

        3.4. 启动solr控制台,可以看到二个core,安装完成

 

 

 

 

4.EmbeddedSolrServer使用

        4.1:singleCore使用:

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使用:

                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(); } } }  

你可能感兴趣的:(tomcat,exception,server,Solr,query,Class)