文档介绍:word
word
1 / 5
word
准备工作环境〔10分钟〕
. 2009-10-29 10:02  罗刚  电子工业  我要评论()
摘要:《自己动手写搜索引擎》第1章遍历搜索引擎技术,本摘要:《自己动手写搜索引擎》第1章遍历搜索引擎技术,本章从快速实现根本的搜索入手,然后深入展开分析搜索的根本技术。本节为大家介绍编写代码〔15分钟〕。
标签:搜索引擎  自己动手写搜索引擎
Oracle帮您准确洞察各个物流环节
  编写代码〔15分钟〕
搜索引擎的根底在于对全文索引库的管理,在Lucene中,通过IndexWriter来写入索引库。伪代码如下:
1.创建IndexWriter,准备写索引;
2.遍历要索引的路径;
3.优化索引。
下面是主要的实现代码:
public void go() throws Exception  {  
        long start = ();  
        if (verbose) {  
            ("Creating index in: " + indexDir);  
            //创建索引目录或者建立增量索引  
            if (incremental) ("- using incremental mode");  
        }  
        Index = new IndexWriter(new File(indexDir), new StandardAnalyzer(),  
            !incremental);//打开或创建索引库,indexDir是索引存放的路径  
         
        File dir = new File(sSourceDir);//待索引的文件存放的路径  
        indexDir(dir);//索引路径  
        ();//索引优化  
        ();//关闭索引库  
        if(verbose)  
        ("index plete in :"+(System. currentTime Millis() - start)/1000);  
} 
下面这段代码把文件内容加到索引库:
word
word
4 / 5
word
private void indexFile(File item) {  
        if (verbose) ("Adding FILE: " + item);   
        News news = loadFile(item);//把文件中的内容加载到news对象  
        if ( news!= null &&  != null) {  
            Document doc = new Document()