lucene连接mysql查询

package com.lucene.demo;
/*
 * 连接Mysql查询
 * 
 */
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.TermVector;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.wltea.analyzer.lucene.IKSimilarity;

public class SearchLogic {

	private static Connection conn = null;
	private static Statement stmt = null;
	private static ResultSet rs = null;
	private String searchDir = "E:\\Test\\Index";     
    private static File indexFile = null;     
    private static Searcher searcher = null;     
    private static Analyzer analyzer = null; 
    
    
    /** 索引页面缓冲 */    
    private int maxBufferedDocs = 500;     
    
    
    
    /**   
     * 获取数据库数据   
     * @return ResultSet   
     * @throws Exception   
     */    
    public List<SearchBean> getResult(String queryStr) throws Exception {
		
    	List<SearchBean> result = null; 
    	conn = jdbcuitl.getConnection();
    	if(conn == null){
    		System.out.println("数据库连接失败!");
    	}
    	String sql = "select id, username, password, type from comment_user";
    	
    	try {
			stmt = conn.createStatement();
			
			rs = stmt.executeQuery(sql);
			
			this.createIndex(rs);
			
			TopDocs topdocs = this.search(queryStr);
			
			ScoreDoc[] scoredoc = topdocs.scoreDocs;
			
			result = this.addHits2List(scoredoc);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new Exception("数据库查询sql出错! sql : " + sql);   
		}finally {     
			
            if(rs != null) rs.close();     
            if(stmt != null) stmt.close();     
            if(conn != null) conn.close();     
        }            
    	
    	return result; 
    }
    
    //创建索引
    private void createIndex(ResultSet rs) throws Exception {     
        Directory directory = null;     
        IndexWriter indexWriter = null;     
         
        try {     
            indexFile = new File(searchDir);     
            if(!indexFile.exists()) {     
                indexFile.mkdir();     
            }     
            directory = FSDirectory.open(indexFile);     
            analyzer = new IKAnalyzer();     
              
            indexWriter = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);     
            indexWriter.setMaxBufferedDocs(maxBufferedDocs);     
            Document doc = null;     
            while(rs.next()) {     
                doc = new Document();     
                Field id = new Field("id", String.valueOf(rs.getInt("id")), Field.Store.YES, Field.Index.NOT_ANALYZED, TermVector.NO);     
                Field username = new Field("username", rs.getString("username") == null ? "" : rs.getString("username"), Field.Store.YES,Field.Index.ANALYZED, TermVector.NO);     
                doc.add(id);     
                doc.add(username);     
                indexWriter.addDocument(doc);     
            }     
                         
            indexWriter.optimize();     
            indexWriter.close();     
        } catch(Exception e) {     
            e.printStackTrace();     
        }      
    }     
    
    /**   
     * 搜索索引   
     * @param queryStr   
     * @return   
     * @throws Exception   
     */    
     private TopDocs search(String queryStr) throws Exception {            
         if(searcher == null) {     
             indexFile = new File(searchDir);     
             searcher = new IndexSearcher(FSDirectory.open(indexFile));       
         }     
         searcher.setSimilarity(new IKSimilarity());     
         QueryParser parser = new QueryParser(Version.LUCENE_30,"username",new IKAnalyzer());     
         Query query = parser.parse(queryStr);  
           
         TopDocs topDocs = searcher.search(query, searcher.maxDoc());     
         return topDocs;     
     }  
       
     /**   
     * 返回结果并添加到List中   
     * @param scoreDocs   
     * @return   
     * @throws Exception   
     */    
     private List<SearchBean> addHits2List(ScoreDoc[] scoreDocs ) throws Exception {     
         List<SearchBean> listBean = new ArrayList<SearchBean>();     
         SearchBean bean = null;     
         for(int i=0 ; i<scoreDocs.length; i++) {     
             int docId = scoreDocs[i].doc;     
             Document doc = searcher.doc(docId);     
             bean = new SearchBean();     
             bean.setId(doc.get("id"));     
             bean.setUsername(doc.get("username"));     
             listBean.add(bean);     
         }     
         return listBean;     
     }  
       
     public static void main(String[] args) {     
         SearchLogic logic = new SearchLogic();     
         try {     
             Long startTime = System.currentTimeMillis();     
             List<SearchBean> result = logic.getResult("as123");     
             int i = 0;     
             for(SearchBean bean : result) {     
                 if(i == 10)   
                     break;     
                 System.out.println("bean.name " + bean.getClass().getName() + " : bean.id " + bean.getId()+ " : bean.username " + bean.getUsername());   
                 i++;     
             }  
               
             System.out.println("searchBean.result.size : " + result.size());     
             Long endTime = System.currentTimeMillis();     
             System.out.println("查询所花费的时间为:" + (endTime-startTime)/1000);     
         } catch (Exception e) {   
             e.printStackTrace();     
             System.out.println(e.getMessage());     
         }     
     }     
 }

package com.lucene.demo;

import java.sql.DriverManager;

import java.sql.Connection;

public class jdbcuitl {

		private static Connection connection = null;
		private static final String url = "jdbc:mysql://localhost:3306/sht?useUnicode=true&characterEncoding=UTF-8";
		private static final String Driver = "com.mysql.jdbc.Driver";
		private static final String username = "root";
		private static final String password = "123456";
		
		public static Connection getConnection(){
			
			try {
				//加载驱动
				Class.forName(Driver);
				
				connection = DriverManager.getConnection(url,username,password);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			return connection;
		}
}



package com.lucene.demo;

public class SearchBean {  
	
	
    private String id;  
    private String username;  
    
    
    public String getId() {  
        return id;  
    }  
    public void setId(String id) {  
        this.id = id;  
    }  
    public String getUsername() {  
        return username;  
    }  
    public void setUsername(String username) {  
        this.username = username;  
    }  
}


你可能感兴趣的:(lucene连接mysql查询)