无法在java中的solrj和embeddedsolrserver中创建CoreContainer

时间:2014-04-03 11:48:58

标签: java solr lucene solrj

这是我的代码,我很难创建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

3 个答案:

答案 0 :(得分:0)

似乎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);
}

答案 1 :(得分:0)

还应在创建EmbeddedSolrServer实例之前创建核心集合1,尝试这样的事情(未使用您的代码进行测试):

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

答案 2 :(得分:0)

这段代码应该有助于从头开始创建一个新的核心。

private static final String SOLR_HOME = "target/solr-home/";
private static final String SOLR_CONFIGSETS = "configsets/";

private static EmbeddedSolrServer getEmbeddedSolrServer(String coreName, String coreConfig) throws IOException, SolrServerException {
    String cwd = Paths.get("").toAbsolutePath().toString();
    // The directory where the data will be stored
    final Path solrHome = Path.of(cwd, SOLR_HOME).toAbsolutePath();
    // The directory where find the solr schema and configuration
    final Path solrConfigsets = Path.of(cwd, SOLR_CONFIGSETS).toAbsolutePath();
    createDir(solrHome);

    final NodeConfig config = new NodeConfig.NodeConfigBuilder(coreConfig, solrHome)
            .setConfigSetBaseDirectory(solrConfigsets.toString())
            .build();
    final EmbeddedSolrServer embeddedSolrServer = new EmbeddedSolrServer(config, coreName);
    // Build an admin request to create a new core named coreName from the configuration coreConfig
    CoreAdminRequest.Create createRequest = new CoreAdminRequest.Create();
    createRequest.setCoreName(coreName);
    createRequest.setConfigSet(coreConfig);
    embeddedSolrServer.request(createRequest);

    return embeddedSolrServer;
}