码迷,mamicode.com
首页 > 数据库 > 详细

用Lucene检索数据库

时间:2014-08-14 19:24:09      阅读:246      评论:0      收藏:0      [点我收藏+]

标签: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("中国");
       
      }

}

用Lucene检索数据库,布布扣,bubuko.com

用Lucene检索数据库

标签:Lucene   blog   http   java   os   io   文件   数据   

原文地址:http://www.cnblogs.com/apem/p/3912767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!