日期:2014-05-19 浏览次数:20823 次
public LuceneDomain searchIndex(String searchStr) throws Exception{
File indexDir = new File(PropertiesUtil.getPropertyValue(searchDirKEY));
String[] fields=new String[]{"id","source","title","context","url"};
//索引目录
Directory dir=FSDirectory.open(indexDir);
//根据索引目录创建读索引对象
IndexReader reader = IndexReader.open(dir);
//搜索对象创建
IndexSearcher searcher = new IndexSearcher(reader);
//IKAnalyzer中文分词
Analyzer analyzer = new IKAnalyzer();
//创建查询解析对象
//QueryParser parser = new QueryParser(Version.LUCENE_36,"context", analyzer);
QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_36,fields, analyzer);
parser.setDefaultOperator(QueryParser.AND_OPERATOR);
//根据域和目标搜索文本创建查询器
//Query query = parser.parse(searchStr);
Query query =IKQueryParser.parseMultiField(fields, searchStr);
System.out.println("Searching for: " + query.toString("context"));
//对结果进行相似度打分排序
TopScoreDocCollector collector = TopScoreDocCollector.create(maxBufferedDocs,false);
searcher.search(query, collector);
//获取结果
ScoreDoc[] hits = collector.topDocs().scoreDocs;
int numTotalHits = collector.getTotalHits();
LuceneDomain lucene=new LuceneDomain();
lucene.setTotalNum(numTotalHits);
lucene.setSearchText(searchStr);
List<SearchDomain> searchList=new ArrayList<SearchDomain>();
//显示搜索结果
SearchDomain search=null;
for (int i = 0; i < hits.length; i++) {
search=new SearchDomain();
Docum