lucene学习----创建索引

本文介绍了一个使用Java和Lucene构建全文搜索索引的实际案例。通过从MySQL数据库中读取文章数据,并利用ChineseAnalyzer进行中文分词处理,最终将数据存储为Lucene索引文件。文中详细展示了如何配置数据库连接、创建索引、设置文档字段等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package src;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;

public class LuceneIndex{
 public static void main(String args[]) throws Exception{
  
  
  try{
   try{
    Class.forName("org.gjt.mm.mysql.Driver");
   }catch(java.lang.ClassNotFoundException ex){
    System.out.print("ClassnotfoundExceptin");
    System.out.println(ex.getMessage());
   }
   //数据库连接
   Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb?user=root&password=123&useUnicode=true&characterEncoding=GBK&allowMultiQueries=true");

   Statement qs = con.createStatement();
   //ResultSet rs = qs.executeQuery("select id,email,name from user where id<320");
   ResultSet rs = qs.executeQuery("select id,title,content,createTime from grouparticle where id<59");
   System.out.println("*****************************************");
   //生成索引书写器
   IndexWriter writer = new IndexWriter("E://lucene//test4//index", new ChineseAnalyzer(),true);
  
  
   
   int mergeFactor = 1000;
   writer.setMergeFactor(mergeFactor);
   writer.setMaxBufferedDocs(1000);  //缓存在内存中的document数目,超过它以后会写入到磁盘 默认:10
   //建立索引
   System.out.println("建立索引......");
   Date start = new Date();
   int n=0;
   String id = "";
   String title = "";
   while (rs.next()){
    if(rs.getString(1)!=null&&rs.getString(2)!=null){
     //System.out.print("id:"+rs.getString(1));
     //System.out.println("____email:"+rs.getString(2));
     //生成文档对象
     title = rs.getString(2);
     String content = rs.getString(3);
   //  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");       
     
   //  String createTime = org.apache.lucene.document.DateTools.dateToString(rs.getDate(4), DateTools.Resolution.MILLISECOND );
     
     id = rs.getString(1);
     //生成文档对象
     Document doc = new Document();
     doc.add(new Field("title",title,Field.Store.YES,Field.Index.TOKENIZED));
     doc.add(new Field("content",content,Field.Store.YES,Field.Index.TOKENIZED));
     doc.add(new Field("id",id,Field.Store.YES,Field.Index.UN_TOKENIZED));
  //   doc.add(new Field("createTime",createTime,Field.Store.YES,Field.Index.UN_TOKENIZED));
     //将文档添加到索引种
     writer.addDocument(doc);
     
     //索引的优化
     if(n>mergeFactor){
      writer.optimize();
      n=0;
     }
     //System.out.println("addDocument:"+title+"OK");    
    }
   }
   writer.optimize();
   Date end = new Date();
   //关闭
   writer.close();
   System.out.println("建立索引用时:"+(end.getTime() - start.getTime())+"毫秒");
   System.out.println("*****************************************");
   con.close();
  }catch(SQLException ex1){
   while (ex1!=null){
    System.out.println("The exception of SQL");
    System.out.println(ex1.getSQLState());
    System.out.println(ex1.getMessage());
    System.out.println(ex1.getErrorCode());
    ex1=ex1.getNextException();
   }
  }
 }

 

注:这个类用到了Chinese解析器,故需要把lucene-analyzers-2.0.0.jar包导进来!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值