Application scenarios
When using SOLR, you sometimes need to dynamically load the configuration file through the program, such as modifying solrconfig. xml and schema. xml. You need to re-load the core to re-load the configuration file.
In addition, dynamic core creation is also required for Index classification.
Sample Code
Import Java. io. file; import Java. io. ioexception; import Org. apache. SOLR. client. solrj. solrserverexception; import Org. apache. SOLR. client. solrj. impl. httpsolrserver; import Org. apache. SOLR. client. solrj. request. coreadminrequest; import Org. apache. SOLR. common. util. namedlist; import Org. apache. SOLR. util. fileutils; public class coreadminrequestdemo {public static final string solr_url = "http: // 172.1663.233: 89 83/SOLR "; public static final string default_core_name =" collection1 "; public static final string new_core_name =" Demo "; public static void main (string [] ARGs) {try {// createcore (new_core_name); reloadcore (default_core_name);} catch (ioexception e) {e. printstacktrace ();} catch (solrserverexception e) {e. printstacktrace () ;}}/***** @ Param corename * @ throws solrserverexception * @ throws ioexception */Public static void reloadcore (string corename) throws solrserverexception, ioexception {// connect to the SOLR server httpsolrserver Server = new httpsolrserver (solr_url); coreadminrequest. reloadcore (corename, server); system. out. println ("reload" + corename + "successful ");} /***** @ Param corename * @ throws solrserverexception * @ throws ioexception */public static void createcore (string corename) throws solrserverexception, Io Exception {// connect to the SOLR server httpsolrserver Server = new httpsolrserver (solr_url); // obtain SOLR. use the cores configured in XML as the default value to obtain the default core path namedlist <Object> List = coreadminrequest. getstatus (default_core_name, server ). getcorestatus (). get (default_core_name); string Path = (string) list. get ("instancedir"); // obtain solrhome, which is the main directory where SOLR is indexed. String solrhome = path. substring (0, path. indexof (default_core_name); // create a new core folder file Cor Epath = new file (solrhome + file. Separator + new_core_name); If (! Corepath. exists () {corepath. mkdir ();} // create the conf folder file confpath = new file (corepath. getabsolutepath () + file. separator + "CONF/"); If (! Confpath. exists () {confpath. mkdir ();} // copy solrconfig. xml and schema. XML in conf under the default core to conf of the new core. This step is required // because the new core SOLR will go to the conf folder to find these two files. If not, an error will be reported, and the new core will not be created successfully. fileutils. copyfile (new file (path + "CONF/solrconfig. XML "), new file (confpath. getabsolutepath () + file. separator + "solrconfig. XML "); fileutils. copyfile (new file (path + "CONF/schema. XML "), new file (confpath. getabsolutepath () + file. separator + "schema. XML "); // create a new core and add the information of the new core to SOLR. coreadminrequest. createcore (corename, corename, server );}}