Use solrj 4.9 to manage SOLR Core

Source: Internet
Author: User
Tags solr
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 );}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.