lucene学习之如何创建索引?
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包导进来!
本文地址:http://www.45fan.com/a/question/70147.html