Lucene3.0 对数据库数据进行搜索

package com.orifound.aiim.web.util;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

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.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.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 * 
 * @描述 对数据库数据进行搜索
 */
public class LuceneUtil {

	// 保存路径
	private static String INDEX_DIR = "D:\\Lucene\\index";
	private static Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
	private static Directory directory = null;

	/**
	 * 建立索引
	 * @param args
	 */
	public static void index() throws Exception{
		directory = FSDirectory.open(new File(INDEX_DIR));
		File indexFile = new File(INDEX_DIR);
		if (!indexFile.exists()) {
			indexFile.mkdirs();
		}
		
		//if (new File(INDEX_DIR).listFiles().length <= 0) {
			IndexWriter indexWriter = new IndexWriter(directory, analyzer,IndexWriter.MaxFieldLength.LIMITED);

			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			Connection connection = DriverManager.getConnection("jdbc:sqlserver://bbs:1433;DatabaseName=AIIM_DB;userName=sa;password=123456");
			PreparedStatement statement = connection.prepareStatement("select * from ArchivesInfoAttachedFileFullTextInfo");
			ResultSet resultSet = statement.executeQuery();
			while (resultSet.next()) {
				Document document = new Document();
				document.add(new Field("ID", resultSet.getString("ID"),Field.Store.YES, Field.Index.ANALYZED));
				document.add(new Field("FullText", resultSet.getString("FullText"), Field.Store.YES, Field.Index.ANALYZED));
				indexWriter.addDocument(document);
			}
			
			indexWriter.optimize();// 优化
			indexWriter.close();
			resultSet.close();
			statement.close();
			connection.close();
		//}
	}
	
	/**
	 * 关键字查询
	 * @param str
	 * @throws Exception
	 */
	public static void search(String str) throws Exception{
		directory = FSDirectory.open(new File(INDEX_DIR));
		IndexSearcher indexSearcher = new IndexSearcher(directory, true);
		
		String fieldname = "FullText";
		QueryParser queryParser = new QueryParser(Version.LUCENE_30,fieldname, analyzer);
		Query query = queryParser.parse(str);
		TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.create(100, true);
		indexSearcher.search(query, topScoreDocCollector);
		ScoreDoc[] scoreDocs = topScoreDocCollector.topDocs().scoreDocs;
		for (int i = 0; i < scoreDocs.length; i++) {
			Document doc = indexSearcher.doc(scoreDocs[i].doc);
			System.out.println(doc.get("ID"));
			System.out.println("------------------------------------------------");
		}
	}
	
	
	public static void main(String[] args) {	
		try {
			//LuceneUtil.index();
			LuceneUtil.search("成绩");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(java,apache,sql,jdbc,Lucene)