每日学习笔记(20)

1,Solr合并索引数据有两种方法,第一种是1.4版本中引入的,通过CoreAdminHandler来实现,示例如下:

http://localhost:8983/solr/admin/cores?action=mergeindexes&core=core0&indexDir=/opt/solr/core1/data/index&indexDir=/opt/solr/core2/data/index

上述命令会将core1core2的索引合并到core0中去,这里最值得注意的一点是:一旦合并完成,必须在core0上调用commit操作,否则索引数据的变化对于searchers来说是暂时不可见的,只有等到下次core0重新装载起来时才可见。

实现代码如下:

/** * 合并索引数据 * @param otherIndexPathList 其他待合并的索引数据路径 * @param coreName 合并的目标solr核名称 * */ private void mergeIndexData(List<String> otherIndexPathList, String coreName) { if (null != otherIndexPathList && otherIndexPathList.size() > 0) { HttpClient client = new HttpClient(); client.setConnectionTimeout(20000); client.setTimeout(20000); client.setHttpConnectionFactoryTimeout(20000); StringBuffer sb = new StringBuffer(); for (int i = 0; i < otherIndexPathList.size(); ++i) { sb.append("&indexDir=" + otherIndexPathList.get(i) + "/data/index"); } String mergeIndexCMD = "http://" + Constants.LOCAL_ADDRESS + ":" + this.port + "/admin/cores?action=mergeindexes&core="+ coreName; if (sb.length() > 0) { mergeIndexCMD += sb.toString(); } HttpMethod method = new GetMethod(mergeIndexCMD); method.getParams().setContentCharset("GBK"); method.getParams().setHttpElementCharset("GBK"); method.getParams().setCredentialCharset("GBK"); // execute the method. try { if (client.executeMethod(method) == 200) { String response = method.getResponseBodyAsString(); if (logger.isInfoEnabled()) { logger.info("merge result" + response); } } } catch (Exception e) { logger.error("合并其他索引数据失败 " + coreName + ",索引目录: " + otherIndexPathList, e); } //commit操作让合并后的索引对搜索生效 StreamingUpdateSolrServer httpSolrServer = null; httpSolrServer = getSolrServer(Constants.LOCAL_ADDRESS, this.port, coreName); try { httpSolrServer.commit(); } catch (Exception e) { } } }

第二种方法是Solr3.3中引入的,也是通过CoreAdminHandler来实现,示例如下:

http://localhost:8983/solr/admin/cores?action=mergeindexes&core=core0&srcCore=core1&srcCore=core2

同第一种方法一样,一旦合并完成,必须在core0上调用commit操作,否则索引数据的变化对于searchers来说是暂时不可见的,只有等到下次core0重新装载起来时才可见。

使用”srcCore”和”indexDir”这两种方法的区别:

1)使用”indexDir”参数,你可以合并不是与Solr核相关联的索引数据,比如通过Lucene直接创建的索引

2)使用”indexDir”参数,你必须注意索引数据不是直接写入的,这就意味着如果它是一个solr核的索引,必须要关闭IndexWriter,这样才能触发一个commit命令。

3)“indexDir”必须指向solr核所在的主机上的磁盘路径,这就限制比较多了,而相反,你可以只给srcCore一个solr核的名称,而不关心它的实际索引路径在哪。

4)使用”srcCore”,你必须确保即使源索引数据同时存在写操作的时候,合并后的索引页不会损坏。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值