关于通过solrj创建core心得

最近开发需要用到solr全文检索,由于有多种数据,希望把这些数据所见的索引进行分类查询,于是就了解了solr的multiCore。在网上找了很多资料没有找到我想要的例子,最终通过群里的一些朋友帮助熟悉了CoreAdminRequest这个类对我很有用,于是就根据提供的api最终实现了core的添加。以下是代码

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();
}

比较简单,但是解决了我目前的问题,如果有人在研究,请多多指点。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值