Lucene增加高亮显示后结果更高明显了,但是返回结果的速度比较慢.原因是Lucene做每一篇文档的相关关键词的高亮显示时,在运行时执行了很多遍的分词操作,降低了性能.
TermVector保存Token.getPositionIncrement() 和Token.startOffset() 以及Token.endOffset() 信息。利用Lucene中新增加的Token信息的保存结果以后,就不需要为了高亮显示而在运行时解析每篇文档。通过Field方法控制是否保存该信息
public void searcher() throws IOException{
IndexReader reader = IndexReader.open(directory);
IndexSearcher searcher = new IndexSearcher(directory);
TermQuery query = new TermQuery(new Term("subject","java"));
Hits hits = searcher.search(query);
//高亮显示设置
SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color='red'>","</font>");
Highlighter highlighter =new Highlighter(simpleHTMLFormatter,new QueryScorer(query));
// 这个100是指定关键字字符串的context的长度,你可以自己设定,因为不可能返回整篇正文内容
highlighter.setTextFragmenter(new SimpleFragmenter(100));
for(int i = 0; i < hits.length(); i++){
Document doc = hits.doc(i);
TermPositionVector termFreqVector = (TermPositionVector)reader.getTermFreqVector(hits.id(i), "subject");
TokenStream tokenStream = TokenSources.getTokenStream(termFreqVector);
String result = highlighter.getBestFragment(tokenStream, doc.get("subject"));
System.out.println(doc.get("title"));
System.out.println(result);
}
}
搜索结果:
java
<font color='red'>java</font>一门编程语言
分享到:
相关推荐
lucene 高亮显示. java 已测试
lucene高亮显示
基于SpringBoot编写的一个Lucene测试Demo把匹配到的结果高亮摘要显示在前端jsp上
lucene.NET 中文分词 高亮 lucene.NET 中文分词 高亮 lucene.NET 中文分词 高亮 lucene.NET 中文分词 高亮
NULL 博文链接:https://iamyida.iteye.com/blog/2201196
利用IKAnalyzer结合LUCENE.4.9进行中文分词的高亮显示。
NULL 博文链接:https://sunlongan666.iteye.com/blog/580380
lucene3.5高亮
NULL 博文链接:https://navylee.iteye.com/blog/740128
java实现lucene高亮显示Html,直接测试就可以用;java实现的对html进行高亮显示,
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
此资源配合《android+lucene实现全文检索并高亮关键字》使用http://download.csdn.net/detail/reality_jie/6335113
使用lucene技术,实现android的本地文档资源的全文检索功能,并实现关键字高亮
java实现高亮显示的jar包,lucene用的jar包,net.teamhot.lucene
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
这个是自己写的lucene分页高亮程序虽然有些不足但是毕竟是自己研究半天总结出来的麻烦各位帮忙给点分哈哈祝大家新年快乐!
Lucene.Net.dll Highlighter.Net.dll
lucene jar 2.4.0使用jar包加高亮包.外加commons-logging-1.0.4包
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...