lucene學習—-建立索引

來源:互聯網
上載者:User

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包導進來!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.