`
banditjava
  • 浏览: 158238 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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


希望这个补救措施能够进一步帮助到大家,我可不希望我发表的文章误人子弟,嘿嘿!

分享到:
评论
4 楼 nhy520 2009-05-23  
怎么执行脚本啊大哥.我不会shell编程
3 楼 yawl 2009-01-09  
http://wiki.apache.org/nutch/IntranetRecrawl

最前面的注解是我加的。今年初使用过nutch一段,发现这个recrawl的并不像宣称的那样工作。
2 楼 ianwong 2008-12-18  
你确信merge-out产生的是增量数据??
1 楼 ianwong 2008-12-17  
合并后 查询服务器启动不起来了哦

相关推荐

Global site tag (gtag.js) - Google Analytics