用lucene为数据库搜索建立增量索引

用 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();
  }
            
  writer 
=   new  IndexWriter(Directory,  new  StandardAnalyzer(),isEmpty);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值