try { //连接solr服务器 HttpSolrServer server=new HttpSolrServer("http://localhost:8080/solr"); //获得solr.xml配置好的cores作为默认,获得默认core的路径 String path=(String) CoreAdminRequest.getStatus("core0",server).getCoreStatus().get("core0").get("instanceDir"); //获得solrhome,也就是solr放置索引的主目录 String solrHome=path.substring(0,path.indexOf("core0")); //建立新core所在文件夹 File corePath=new File(solrHome+File.separator+coreName); if(!corePath.exists()){ corePath.mkdir(); } //建立新core下的conf文件夹 File confPath=new File(corePath.getAbsolutePath()+File.separator+"conf/"); if(!confPath.exists()){ confPath.mkdir(); } //将默认core下conf里的solrconfig.xml和schema.xml拷贝到新core的conf下。这步是必须的因为新建的core solr会去其conf文件夹下找这两个文件,如果没有就会报错,新core则不会创建成功 CoreContainer.fileCopy(new File(path+"conf/solrconfig.xml"), new File(confPath.getAbsolutePath()+File.separator+"solrconfig.xml")); CoreContainer.fileCopy(new File(path+"conf/schema.xml"), new File(confPath.getAbsolutePath()+File.separator+"schema.xml")); //创建新core,同时会把新core的信息添加到solr.xml里 CoreAdminRequest.createCore(coreName, coreName, server); } catch (IOException e) { e.printStackTrace(); } catch (SolrServerException e) { // TODO Auto-generated catch block e.printStackTrace(); }
比较简单,但是解决了我目前的问题,如果有人在研究,请多多指点。