在Java中solrj和embeddedsolrserver不能创建CoreContainer(ca

2019-10-19 17:58发布

这是我的代码,我很累创建CoreContainer

File home = new File( "C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/solr" );
//home directory     
File f = new File(home, "solr.xml" );

CoreContainer container = new CoreContainer();

container.load( "C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/solr", f );    

EmbeddedSolrServer server = new EmbeddedSolrServer( container, "collection1" );    

SolrQuery query = new SolrQuery("*:*");
QueryResponse rsp = server.query(query);
SolrDocumentList docs = rsp.getResults();
Iterator<SolrDocument> i = docs.iterator();

while (i.hasNext()) {
   System.out.println(i.next().toString());
}

我solr.xml文件如下。

<solr>

  <solrcloud>
    <str name="host">${host:}</str>
    <int name="hostPort">${jetty.port:9000}</int>
    <str name="hostContext">${hostContext:solr}</str>
    <int name="zkClientTimeout">${zkClientTimeout:30000}</int>
    <bool name="genericCoreNodeNames">${genericCoreNodeNames:true}</bool>
  </solrcloud>

  <shardHandlerFactory name="shardHandlerFactory"
    class="HttpShardHandlerFactory">
    <int name="socketTimeout">${socketTimeout:0}</int>
    <int name="connTimeout">${connTimeout:0}</int>
  </shardHandlerFactory>

</solr>

solrconfig.xml中的文件是一样的默认

当我运行此我得到SolrServerException等,也不会产生EmbeddedSolrServer

INFO: New CoreContainer: solrHome=solr/ instance=6751353
Apr 03, 2014 4:46:05 PM org.apache.solr.core.SolrResourceLoader <init>
INFO: Solr home set to 'C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/solr\'
org.apache.solr.client.solrj.SolrServerException: Error executing query
    at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:95)
    at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:118)
    at com.hl.model.em.main(em.java:101)
Caused by: org.apache.solr.common.SolrException: No such core: collection1
    at org.apache.solr.client.solrj.embedded.EmbeddedSolrServer.request(EmbeddedSolrServer.java:104)
    at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:89)
    ... 2 more

Answer 1:

看来,Solr的容器没有初始化。 与此代码尝试初始化容器:

String solrDir = "C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/solr/new_core/";

//this solr Directory is home and specified core.

//solrParams.set("qt", "/dataimport");
//solrParams.set("command", "full-import");

//above two line for importing data one time.

CoreContainer container = new CoreContainer(solrDir);
container.load();
EmbeddedSolrServer server = new EmbeddedSolrServer(container, "collection1");

ModifiableSolrParams solrParams = new ModifiableSolrParams();
solrParams.add(CommonParams.Q, "*:*");

QueryResponse queryResponse = server.query(solrParams);
for (SolrDocument document : queryResponse.getResults()) {
  System.out.println(document);
}


Answer 2:

核心collection1也应创建之前创建EmbeddedSolrServer例如,尝试这样的事情(不与你的代码测试):

String dataDirStr = "./data";
Properties props = new Properties();
props.setProperty(CoreDescriptor.CORE_DATADIR, dataDirStr);
CoreDescriptor descr = new CoreDescriptor(container, "collection1", home, props);
SolrCore core = container.create(descr);


文章来源: can not create CoreContainer in solrj and embeddedsolrserver in java