专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »编程综合 » lucene更新索引:lucene 递归索引 »正文

lucene更新索引:lucene 递归索引

来源: 发布时间:星期四, 2009年1月15日 浏览:22次 评论:0
  //上个例子中没有递归,这次加上,于是目录子目录也可以被索引了.所用到个思路方法: IndexDirectory.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package yqr.search;
import com.leezjs.ChineseSegmenter.ChineseSegmenter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Date;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
/**
*
* @author yangqr
*/
public IndexDir {
  Date start;
  Date end;
  String seconds="";
  public IndexDir(String src,String index) {
    //开始建立索引
    try {
      Analyzer analyzer2 = WhitespaceAnalyzer;
      Directory directory = FSDirectory.getDirectory(index);
      IndexWriter iwriter = IndexWriter(directory, analyzer2, true);
      iwriter.MaxFieldLength(25000);
      try {
        start= Date;
        File d = File(src);
        IndexDirectory(d,iwriter);
        end= Date;
        seconds=(end.getTime-start.getTime)+"";
      } catch (Exception ex) {
        ex.prStackTrace;
      }
      iwriter.close;
    //建立索引结束
    } catch (Exception e) {
    }
  }
  public void IndexDirectory(File dir,IndexWriter iwriter) throws Exception {
     (dir.isDirectory) {
      File files = dir.listFiles;
      for ( i = 0; i < files.length; i) {
        File cur = files[i];
         (cur.isDirectory) {
          IndexDirectory(cur,iwriter);
        } (cur.isFile) {
          .out.prln("正在索引:" + cur.getAbsolutePath);
          FileReader fr = FileReader(cur);
          BufferedReader br = BufferedReader(fr);
          String file_content = "";
          String line = br.readLine;
          file_content line;
          while (line != null) {
            line = br.readLine;
            file_content line;
          }
          Document doc = Document;
          ChineseSegmenter cs = ChineseSegmenter.getGBSegmenter;
          String keywords = cs.segmentLine(file_content, " ");
          doc.add( Field("content", file_content, Field.Store.YES,
              Field.Index.TOKENIZED));
          doc.add( Field("keyword", keywords, Field.Store.YES, Field.Index.TOKENIZED));
          doc.add( Field("FileName", files[i].getAbsolutePath, Field.Store.YES, Field.Index.TOKENIZED));
          iwriter.addDocument(doc);
          iwriter.optimize;
        }
      }
    }
  }
  public void (String args) {
      IndexDir id= IndexDir("F:/sou/index_src","f:/sou/testindex");
      String total=id.seconds;
      .out.prln("共用了 "+total + " 毫秒");
  }
}


0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: