用Lucene檢索資料庫

來源:互聯網
上載者:User

標籤:Lucene   blog   http   java   os   io   檔案   資料   

http://blog.sina.com.cn/s/blog_82ac67c101012r9z.html

package com.javabean;

import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.QueryParser.Operator;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

public class lucene
{
 public void createIndexFile() {    
         IndexWriter indexWriter=null;    
          try {    
             // 需要的分詞器    
             Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);    
             // 建立的是哪個版本的IndexWriterConfig    
             IndexWriterConfig indexWriterConfig = new IndexWriterConfig(    
                     Version.LUCENE_36, analyzer);    
              // 建立系統檔案-----    
              Directory directory = new SimpleFSDirectory(new File("D:\\tomcat-6.0.35\\webapps\\jsp_flex\\index"));    
              indexWriter = new IndexWriter(directory,indexWriterConfig);
              //訪問資料庫拿資料
              DBConnect db = new DBConnect();
        String sql = "select * from book";
        ResultSet rs = db.select(sql);
        while(rs.next())
        {
              //indexWriter添加索引    
              Document doc=new Document();    
              //文本中新增內容      
              doc.add(new Field("ISBN",rs.getString(1).toString(),Store.YES,Index.ANALYZED));    
              doc.add(new Field("bookname",rs.getString(2).toString(),Store.YES,Index.ANALYZED));    
              doc.add(new Field("author",rs.getString(3).toString(),Store.YES,Index.ANALYZED));   
              doc.add(new Field("leibie",rs.getString(6).toString(),Store.YES,Index.ANALYZED));
              doc.add(new Field("jianjie",rs.getString(10).toString(),Store.YES,Index.ANALYZED));
              //添加到索引中去   
              indexWriter.addDocument(doc);
        }
                     
          } catch (IOException e) {       
              e.printStackTrace();    
          } catch (SQLException e) {
     e.printStackTrace();
    }finally{    
              if(indexWriter!=null){    
                  try {  
                   indexWriter.commit();
                      indexWriter.close();    
                  }  catch (IOException e) {        
                      e.printStackTrace();    
                  }    
              }    
          }    
      }    
     
      public void seacher(String queryString) throws IOException
      {
       FSDirectory fs;
       try {   
        fs =  FSDirectory.open(new File("D:\\tomcat-6.0.35\\webapps\\jsp_flex\\index"));  
        IndexReader ir = IndexReader.open(fs);  
        long start  = System.currentTimeMillis();  
        IndexSearcher search = new IndexSearcher(ir);  
        String key = queryString;  
        String fieds[] = new String[]{"ISBN","bookname","author","leibie","jianjie"};  
        Analyzer ik = new StandardAnalyzer(Version.LUCENE_36);   
        MultiFieldQueryParser m = new MultiFieldQueryParser(Version.LUCENE_36, fieds, ik);  
        m.setDefaultOperator(Operator.AND);  
        Query query = m.parse(key) ;  
           ScoreDoc[] hits = search.search(query, null, 1000).scoreDocs;   
        System.out.println("共命中"+hits.length+"條記錄");     
        for(ScoreDoc scoreDoc:hits)  
        {  
            Document doc= search.doc(scoreDoc.doc);  
            System.out.println(scoreDoc.score + "\t ISBN:"+doc.get("ISBN")+"\t bookname:"+doc.get("bookname")+"\t leibie:"+doc.get("leibie"));  
        }             
           System.out.println("執行時間:"+(System.currentTimeMillis()-start)+"毫秒");  
         }
       catch (IOException e)
       {  
         e.printStackTrace();  
       } catch (ParseException e) {
        e.printStackTrace();  
          }    
       
       
      }

public static void main(String[] args) throws IOException
      {    
       lucene test = new lucene();
      // test.createIndexFile();
       test.seacher("中國");
       
      }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.