Nutch开源搜索引擎增量索引recrawl的终极解决办法(续)

十一要放假了,先祝广大同学们节日快乐!

在之前的一篇文章中,我给出了Nutch的recrawl的解决办法。说实话,当时已经觉得可以应对recrawl的问题,但是我自己在测试过程中发现,在index的merge时,并没有完全成功。本文就是针对上一篇文章给出解决办法。

具体的原因是在merge完成后,会在index目录下面生成一个merge-output目录,这是由于临时目录newindexs和index执行完毕后产生的,这就增量索引的结果。

在shell中的命令是:/nutch/search/bin/nutch merge crawl10/index crawl10/newindexes

我原先的index目录中_0.fdt原来就是6292105 Byte,执行完后并没有增加,而在merge-output中的_0.fdt大小是1636852 Byte。说明这两块索引并没有合并在一起。

[nutch@linux1 crawl10-new]$ ll index/
total 76228
-rw-r--r-- 1 nutch users 6292105 Sep 28 17:02 _0.fdt
-rw-r--r-- 1 nutch users 236848 Sep 28 17:02 _0.fdx
-rw-r--r-- 1 nutch users 81 Sep 28 17:02 _0.fnm
-rw-r--r-- 1 nutch users 22120872 Sep 28 17:02 _0.frq
-rw-r--r-- 1 nutch users 177640 Sep 28 17:02 _0.nrm
-rw-r--r-- 1 nutch users 48174123 Sep 28 17:02 _0.prx
-rw-r--r-- 1 nutch users 12794 Sep 28 17:02 _0.tii
-rw-r--r-- 1 nutch users 902086 Sep 28 17:02 _0.tis
drwxr-xr-x 2 nutch users 4096 Sep 28 17:02 merge-output
-rw-r--r-- 1 nutch users 41 Sep 28 17:02 segments_2
-rw-r--r-- 1 nutch users 20 Sep 28 17:02 segments.gen

[nutch@linux1 crawl10-new]$ ll index/merge-output/
total 20520
-rw-r--r-- 1 nutch users 1636852 Sep 28 18:53 _0.fdt
-rw-r--r-- 1 nutch users 61608 Sep 28 18:53 _0.fdx
-rw-r--r-- 1 nutch users 81 Sep 28 18:53 _0.fnm
-rw-r--r-- 1 nutch users 5782861 Sep 28 18:53 _0.frq
-rw-r--r-- 1 nutch users 46210 Sep 28 18:53 _0.nrm
-rw-r--r-- 1 nutch users 13004666 Sep 28 18:53 _0.prx
-rw-r--r-- 1 nutch users 5406 Sep 28 18:53 _0.tii
-rw-r--r-- 1 nutch users 405471 Sep 28 18:53 _0.tis
-rw-r--r-- 1 nutch users 41 Sep 28 18:53 segments_2
-rw-r--r-- 1 nutch users 20 Sep 28 18:53 segments.gen


不知道nutch是否有这种合并的功能,呵呵,我没有找到,于是就自己写了一个类MergeLuceneIndex来解决这个问题。具体的思路就是用lucene的索引合并功能来处理这个问题,将merge-output下面的索引文件合并到index下面,执行后_1.fdt大小变成7928957 Byte,说明合并成功,重启tomcat后,就能看到增量的索引的内容了。


[nutch@linux1 crawl10-new]$ ll index
total 96360
-rw-r--r-- 1 root root 7928957 Sep 28 18:53 _1.fdt
-rw-r--r-- 1 root root 298456 Sep 28 18:53 _1.fdx
-rw-r--r-- 1 root root 81 Sep 28 18:53 _1.fnm
-rw-r--r-- 1 root root 27954608 Sep 28 18:53 _1.frq
-rw-r--r-- 1 root root 223846 Sep 28 18:53 _1.nrm
-rw-r--r-- 1 root root 61178789 Sep 28 18:53 _1.prx
-rw-r--r-- 1 root root 13078 Sep 28 18:53 _1.tii
-rw-r--r-- 1 root root 922972 Sep 28 18:53 _1.tis
drwxr-xr-x 2 nutch users 4096 Sep 28 18:53 merge-output
-rw-r--r-- 1 root root 41 Sep 28 18:53 segments_3
-rw-r--r-- 1 root root 20 Sep 28 18:53 segments.gen

合并索引是lucene的基本功能,很好很强大。由于我选paoding做中文分词,所以需要配置好paoding,再运行下面这个类。

//注意:nutch默认是用分拆式index,需要将compound设成false
indexWriter.setUseCompoundFile(false);

共享一下代码!
package org.apache.nutch.tool;
import java.io.File;

import net.paoding.analysis.analyzer.PaodingAnalyzer;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
/**
* 这个类是为了处理nutch的本地index用的,合并merge-out与原始的index文件
* @author kevin
*
*/
public class MergeLuceneIndex {
/**
* @author about merge lucene Index by kevin
* 将小索引文件合并到大的索引文件中去
* @param from 将要合并到to文件的文件
* @param to 将from文件合并到该文件
* @param sa
*/
private static void mergeIndex(File from, File to,Analyzer analyzer) {
IndexWriter indexWriter = null;
try {
System.out.println("Merge start! ");
indexWriter = new IndexWriter(to, analyzer, false);
indexWriter.setUseCompoundFile(false);//注意:nutch默认是用分拆式index,需要将compound设成false
indexWriter.setMergeFactor(100000);
indexWriter.setMaxFieldLength(Integer.MAX_VALUE);
indexWriter.setMaxBufferedDocs(Integer.MAX_VALUE);
indexWriter.setMaxMergeDocs(Integer.MAX_VALUE);
FSDirectory[] fs = { FSDirectory.getDirectory(from, false) };
indexWriter.addIndexes(fs);
indexWriter.optimize();
indexWriter.close();
System.out.println("Merge finished!");
} catch (Exception e) {
System.out.println("Merge error!");
e.printStackTrace();
} finally {
try {
if (indexWriter != null)
indexWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

public static void main(String[] arg){
//File from = new File("/nutch/local/crawl10-new/index/merge-output");
//File to = new File("/nutch/local/crawl10-new/index");
File from = new File(arg[0]);
File to = new File(arg[1]);
mergeIndex(from,to,new PaodingAnalyzer());
}
}


[color=darkred][size=large]希望这个补救措施能够进一步帮助到大家,我可不希望我发表的文章误人子弟,嘿嘿![/size][/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值