user2428568 user2428568 - 4 months ago 51
Java Question

can not create CoreContainer in solrj and embeddedsolrserver in java

This is my Code, I am very tired to create

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


my solr.xml file is below.

<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 file is same as default

When I'm run this I get SolrServerException like, and also not create 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

It seems that solr container was not initialized. Try with this code to initialize the container:

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