Lucene检索目录

Lucene检索索引
package com;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.wltea.analyzer.lucene.IKQueryParser;
import org.wltea.analyzer.lucene.IKSimilarity;

public class Search {
	public static void main(String[] args) throws Exception {
		String fieldName = "discript";
		String keyword = "人民";
		/*Query IkQuery = searchIKQuery(fieldName, keyword);
		executeQuery(fieldName, keyword,IkQuery);*/
		BooleanQuery booleanQuery = searchBooleanQuery(keyword);
		executeQuery(fieldName, booleanQuery);
	}

	public static BooleanQuery searchBooleanQuery( String keyword) throws Exception {
		BooleanQuery bq = new BooleanQuery();

		Analyzer analyzer = new IKAnalyzer();
		String[] fn = {"name"};
		// 增加权重
		Map<String, Float> filedmap = new HashMap();
		filedmap.put("name", 50f);

		MultiFieldQueryParser multField = new MultiFieldQueryParser(Version.LUCENE_30, fn, analyzer, filedmap);
		Query muquery = multField.parse(keyword);
		
		Query ikquery = IKQueryParser.parseMultiField(fn,keyword);
		
		Term term1 = new Term("discript",keyword);
		TermQuery tq = new TermQuery(term1);  
		bq.add(tq,BooleanClause.Occur.MUST);
		bq.add(muquery, BooleanClause.Occur.SHOULD);
		//bq.add(ikquery, BooleanClause.Occur.MUST);
        System.out.println(bq);
		return bq;
	}
    
	public static Query searchIKQuery(String fieldName, String keyword) throws IOException{
		// 使用IKQueryParser查询分析器构造Query对象
		Query query = IKQueryParser.parse(fieldName, keyword);
		
		return query;
	}
	public static IndexSearcher createSeacher() throws Exception {
		IndexSearcher isearcher = null;
		Directory directory = null;
		File file = new File("C:/commFile/lucence");
		directory = FSDirectory.open(file);
		// 实例化搜索器
		isearcher = new IndexSearcher(directory);

		// 在索引器中使用IKSimilarity相似度评估器
		isearcher.setSimilarity(new IKSimilarity());
		if (directory != null) {
			directory.close();
		}
		return isearcher;
	}

	public static void executeQuery(String fieldName,Query query)throws Exception {
		// 实例化IKAnalyzer分词器
		Analyzer analyzer = new IKAnalyzer();
		IndexSearcher isearcher = createSeacher();
		try {
			Sort sort = new Sort();
			// 按字段的排序(一个或多个)
			SortField[] f = { new SortField("name", SortField.STRING, false) };
			sort.setSort(f);
			// 搜索相似度最高的10条记录
			//TopDocs topDocs = isearcher.search(query, null, 10, sort);
			TopDocs topDocs = isearcher.search(query, 10);

			// 关键字高亮显示
			Formatter formatter = new SimpleHTMLFormatter("<font color=\"red\">", "</font>"); // 前缀和后缀
			Scorer scorer = new QueryScorer(query);
			Highlighter highlighter = new Highlighter(formatter, scorer);
			highlighter.setTextFragmenter(new SimpleFragmenter(200)); // 字长度
			System.out.println("命中文章的篇数:" + topDocs.totalHits);
			// 输出结果
			ScoreDoc[] scoreDocs = topDocs.scoreDocs;
			for (int i = 0; i < topDocs.totalHits; i++) {
				int docId =scoreDocs[i].doc;
				Document targetDoc = isearcher.doc(docId);   
				System.out.println("内容:" + targetDoc.get(fieldName)+"===="+targetDoc.get("name"));//输出相应字段的内容
				String hupName = highlighter.getBestFragment(analyzer,fieldName,targetDoc.get(fieldName));
				//System.out.println(hupName);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (analyzer != null) {
				analyzer.close();
			}
			if (isearcher != null) {
				isearcher.close();
			}

		}

	}


}