public void searchContent(String type,String keyword){ // 根据指定的检索内容类型type,以及检索关键字keyword进行检索操作
try {
IndexSearcher searcher = new IndexSearcher(this.indexPath); // 根据指定路径,构造一个IndexSearcher检索器
Term term = new Term(type,keyword); // 创建词条
Query query = new TermQuery(term); // 创建查询
Date startTime = new Date();
TermDocs termDocs = searcher.getIndexReader().termDocs(term); // 执行检索操作
while(termDocs.next()){ // 遍历输出根据指定词条检索的结果信息
System.out.println("搜索的该关键字【"+keyword+"】在文件\n"+searcher.getIndexReader().document(termDocs.doc()));
System.out.println("中,出现过 "+termDocs.freq()+" 次");
}
Date finishTime = new Date();
long timeOfSearch = finishTime.getTime() - startTime.getTime(); // 计算检索花费时间
System.out.println("本次搜索所用的时间为 "+timeOfSearch+" ms");
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
lucene search multiply method
最新推荐文章于 2024-08-21 11:12:41 发布