关于通过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();
		}

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

猜你喜欢

转载自gxq926.iteye.com/blog/1756502