Reputation: 31
I am new in solr and i want to create a new core in solr by using java code and i don't want create it by terminal and GUI of solr, this is code that i am using and i am using 6.2.1 version of solr, please help me . thanx in advance.
coreName="metademo";
String solrDir = "/home/manish/Downloads/solr-6.2.1/server/solr/";
String baseSolrUrl ="http://localhost:8983/solr/";
CoreAdminRequest.Create create = new CoreAdminRequest.Create();
create.setCoreName("metademo");
create.setInstanceDir(solrDir +File.separator );
SolrClient client2=new HttpSolrClient.Builder(baseSolrUrl).build();
create.setDataDir(solrDir + File.separator + coreName + File.separator + "data");
HttpSolrServer solrServer1 = new HttpSolrServer(solrDir,client);
CoreAdminRequest.createCore(coreName, solrDir, client2);
create.createCore(coreName, solrDir, client2);
System.out.println("Created core with name: " + coreName);
Upvotes: 2
Views: 2182
Reputation: 166
The following code snippet works with Solr 8.5.2:
String core = "test";
CoreAdminRequest.Create createRequest = new CoreAdminRequest.Create();
createRequest.setCoreName(core);
createRequest.setInstanceDir("./" + core);
createRequest.setConfigSet("_default");
createRequest.process(solrClient);
The call to setConfigSet()
is necessary, so that the server can know how to initialize configurations for the new core based on the specified configset. Otherwise, some exception message like "Unable to create core [test] Caused by: Can't find resource 'solrconfig.xml' in classpath" would be thrown.
Upvotes: 0
Reputation: 1334
First of all, you have to create the core folder in the solr directory (in your case: /home/manish/Downloads/solr-6.2.1/server/solr/metademo
).
This folder has to have the same name that you'll use in your java code.
Then inside of this new core directory (in your case named "metademo") copy from /.../solr-6.2.1/server/solr/configsets/basic_configs
, the so called /conf
directory.
Once copied, inside the /.../solr-6.2.1/server/solr/metademo/conf
folder, you have to change the name of managed-schema
file in schema.xml
.
I try this:
String coreName = "metademo";
String solrDir = "/.../solr-6.2.1/server/solr/metademo";
String baseSolrUrl = "http://localhost:8983/solr/";
SolrClient client = new HttpSolrClient(baseSolrUrl);
CoreAdminRequest.Create createRequest = new CoreAdminRequest.Create();
createRequest.setCoreName(coreName);
createRequest.setInstanceDir(solrDir);
createRequest.process(client);
and works. Without previous operation your code can only throw exceptions.
Upvotes: 2