user2428568 user2428568 - 1 year ago 184
Java Question

can not create CoreContainer in solrj and embeddedsolrserver in java

This is my Code, I am very tired to create


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()) {

my solr.xml file is below.


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

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


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(
at org.apache.solr.client.solrj.SolrServer.query(
at com.hl.model.em.main(
Caused by: org.apache.solr.common.SolrException: No such core: collection1
at org.apache.solr.client.solrj.embedded.EmbeddedSolrServer.request(
at org.apache.solr.client.solrj.request.QueryRequest.process(
... 2 more

Answer Source

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);
EmbeddedSolrServer server = new EmbeddedSolrServer(container, "collection1");

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

QueryResponse queryResponse = server.query(solrParams);
for (SolrDocument document : queryResponse.getResults()) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download