首页 »编程综合 » lucene更新索引:lucene学习----创建索引 »正文
lucene更新索引:lucene学习----创建索引
来源: 发布时间:星期四, 2009年1月15日 浏览:30次 评论:0
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 LuceneIndex{ public void (String args) throws Exception{ try{ try{ Class.forName("org.gjt.mm.mysql.Driver"); }catch(java.lang.ClassNotFoundException ex){ .out.pr("ClassnotfoundExceptin"); .out.prln(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"); .out.prln("*****************************************"); //生成索引书写器 IndexWriter writer = IndexWriter("E:lucene est4index", ChineseAnalyzer,true); mergeFactor = 1000; writer.MergeFactor(mergeFactor); writer.MaxBufferedDocs(1000); //缓存Cache在内存中document数目超过它以后会写入到磁盘 默认:10 //建立索引 .out.prln("建立索引......"); Date start = Date; n=0; String id = ""; String title = ""; while (rs.next){ (rs.getString(1)!=null&&rs.getString(2)!=null){ //.out.pr("id:"+rs.getString(1)); //.out.prln("____email:"+rs.getString(2)); //生成文档对象 title = rs.getString(2); String content = rs.getString(3); // SimpleDateFormat sdf = SimpleDateFormat("yyyyMMdd"); // String createTime = org.apache.lucene.document.DateTools.date(rs.getDate(4), DateTools.Resolution.MILLISECOND ); id = rs.getString(1); //生成文档对象 Document doc = Document; doc.add( Field("title",title,Field.Store.YES,Field.Index.TOKENIZED)); doc.add( Field("content",content,Field.Store.YES,Field.Index.TOKENIZED)); doc.add( Field("id",id,Field.Store.YES,Field.Index.UN_TOKENIZED)); // doc.add( Field("createTime",createTime,Field.Store.YES,Field.Index.UN_TOKENIZED)); //将文档添加到索引种 writer.addDocument(doc); //索引优化 (n>mergeFactor){ writer.optimize; n=0; } //.out.prln("addDocument:"+title+"OK"); } } writer.optimize; Date end = Date; //关闭 writer.close; .out.prln("建立索引用时:"+(end.getTime - start.getTime)+"毫秒"); .out.prln("*****************************************"); con.close; }catch(SQLException ex1){ while (ex1!=null){ .out.prln("The exception of SQL"); .out.prln(ex1.getSQLState); .out.prln(ex1.getMessage); .out.prln(ex1.getErrorCode); ex1=ex1.getNextException; } } } } 注:这个类用到了Chinese解析器,故需要把lucene-analyzers-2.0.0.jar包导进来!
相关文章
读者评论
发表评论
|
|