用户名: 密 码:
您现在的位置:首页 >> SEO开发技巧 >> 内容

如何使用lucene2.9.0建立增量索引

时间:2009/11/19 10:20:24 点击:7091

  核心提示:用 lucene 建立索引不可能每次都重新开始建立,而是按照新增加的记录,一次次的递增建立索引的IndexWriter类,有三个参数 IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(),isEmpty);其中第三个参数是...
用 lucene 建立索引不可能每次都重新开始建立,而是按照新增加的记录,一次次的递增
建立索引的IndexWriter类,有三个参数
IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(),isEmpty);

其中第三个参数是bool型的,指定它可以确定是增量索引,还是重建索引.
对于从数据库中读取的记录,譬如要为文章建立索引,我们可以记录文章的id号,然后下次再次建立索引的时候读取存下的id号,从此id后往下继续增加索引,逻辑如下.

建立增量索引,主要代码如下
public void createIndex(String path)
{
      Statement myStatement
= null;
      String articleId
="0";
     
//读取文件,获得文章id号码,这里只存最后一篇索引的文章id
    try {
         FileReader fr
= new FileReader("**.txt");
         BufferedReader br
= new BufferedReader(fr);                 
         articleId
=br.readLine();
        
if(articleId==null||articleId=="")
         articleId
="0";
         br.close();
         fr.close();
       }
catch (IOException e) {
         System.out.println(
"error343!");
         e.printStackTrace();
       }
    
try {
        
//sql语句,根据id读取下面的内容
         String sqlText = "*****"+articleId;
         myStatement
= conn.createStatement();
         ResultSet rs
= myStatement.executeQuery(sqlText);
       
//写索引
        while (rs.next()) {
          Document doc
= new Document();
          doc.add(Field.Keyword(
"**", DateAdded));
          doc.add(Field.Keyword(
"**", articleid));
          doc.add(Field.Text(
"**", URL));    
          doc.add(Field.Text(
"**", Content));
          doc.add(Field.Text(
"**", Title));    
         
try{
             writer.addDocument(doc);
           }
          
catch(IOException e){
             e.printStackTrace();
          }
           
//将我索引的最后一篇文章的id写入文件
          try {
            FileWriter fw
= new FileWriter("**.txt");
            PrintWriter out
= new PrintWriter(fw);    
            out.close();
            fw.close();
            }
catch (IOException e) {
              e.printStackTrace();
            }
          }
             ind.Close();
             System.out.println(
"ok.end");
          }
         
catch (SQLException e){
             e.printStackTrace();
         }
        
finally {
            
//数据库关闭操作
         }        
     }

然后控制是都建立增量索引的时候根据能否都到id值来设置IndexWriter的第三个参数为true 或者是false

boolean isEmpty = true;
try {
     FileReader fr
= new FileReader("**.txt");
     BufferedReader br
= new BufferedReader(fr);                 
    
if(br.readLine()!= null) {
         isEmpty
= false;
      }
      br.close();
      fr.close();
     }
catch (IOException e) {
        e.printStackTrace();
   }

文章来源:http://www.xinxilong.com

作者:不详 来源:网络
相关评论
发表我的评论
  • 大名:
  • 内容:
  • 论坛群发大师(www.xinxilong.com) © 2008 版权所有 All Rights Resverved.
  • Email:4984672[at]qq.com 沪ICP备12025887号-1
  • Powered by 论坛群发大师