www.pudn.com > lucene+mysql+eclipe.rar > TestSearch.java


package com.lucene; 
 
import java.io.IOException; 
import java.sql.Connection; 
import java.sql.SQLException; 
import java.util.Date; 
import java.util.List; 
 
import org.apache.lucene.queryParser.ParseException; 
import org.apache.lucene.search.IndexSearcher; 
import org.apache.lucene.search.Searcher; 
 
/*  
 *@author 申华锋 E-mail:leonshine@qq.com 
 *@version 创建时间:Mar 9, 2008 12:12:45 AM 
 *@description 测试 
 */ 
public class TestSearch { 
	public final static String indexDir = "D:\\TestLucene";	 
 
	public static List lucene(String[] q, int startIndex, int endIndex) throws IOException, ParseException, 
			SQLException { 
		//index();// 做索引 
		Searcher searcher = null; 
		List lst = null; 
		try { 
			searcher = new IndexSearcher(indexDir); 
			// String[] q = { "apache", "三星", "ibmd", "micronnnst" }; 
			//long start = new Date().getTime(); 
			lst = SearcherFile.search(searcher, q, startIndex, endIndex); 
			//long end = new Date().getTime(); 
			 
			//System.out.println("花费时间:" + (double) (end - start) / 1000 + "秒"); 
			 
		} catch (Exception e) { 
			e.printStackTrace(); 
		} finally { 
			if (searcher != null) 
				searcher.close(); 
		} 
		return lst; 
	} 
 
	/*public static void main(String[] args) throws IOException, ParseException, SQLException { 
		index();//做索引 
		Searcher searcher=null; 
		try{ 
		searcher = new IndexSearcher(indexDir); 
		String[] q = { "apache", "11", "11", "1" }; 
		search(searcher, q);//搜索 
		}catch(Exception e){ 
		e.printStackTrace(); 
		}finally{ 
		if(searcher!=null) 
		searcher.close(); 
		} 
		}*/ 
 
 
	public static void index() throws SQLException { 
		Connection conn = null; 
		try { 
			conn = DBConnection.getConnection(); 
			Product[] list = Product.loadProducts(); 
			IndexerFile.indexFile(indexDir, list); 
		} catch (Exception e) { 
			e.printStackTrace(); 
		} finally { 
			if (conn != null) { 
				conn.close(); 
			} 
		} 
	} 
}