Lucene高亮显示
public static List search(String[] fields, String keyword)
throws IOException, InvalidTokenOffsetsException {
ArrayList list = new ArrayList();
IndexSearcher isearcher = null;
IKSimilarity iKSimilarity = new IKSimilarity();
Directory dir = new SimpleFSDirectory(new File(indexDir));
IndexReader indexReader = IndexReader.open(dir, true);
isearcher = new IndexSearcher(indexReader);
Analyzer analyzer = new IKAnalyzer();
isearcher.setSimilarity(iKSimilarity);
// QueryParser parser = new
// QueryParser(Version.LUCENE_30,field,analyzer);
Query query = IKQueryParser.parseMultiField(fields, keyword);
TopScoreDocCollector collector = TopScoreDocCollector.create(100, false);
isearcher.search(query, collector);
// TopDocs topDocs = isearcher.search(query , 5);
TopDocs topDocs = collector.topDocs();
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (int i = 0; i < scoreDocs.length; i++) {
Document doc = isearcher.doc(scoreDocs[i].doc);
/** 高亮显示的关键步骤 **/
SimpleHTMLFormatter simpleHtmlFormatter = new SimpleHTMLFormatter("<font color='red'>","</font>");
Highlighter highlighter = new Highlighter(simpleHtmlFormatter,new QueryScorer(query));
highlighter.setTextFragmenter(new SimpleFragmenter(50));
String goal= doc.get("goal");
String highlightGoal = "";
if(!goal.equals("")){
TokenStream tokenStream = analyzer.tokenStream("goal",new StringReader(goal));
highlightGoal = highlighter.getBestFragment(tokenStream, goal);
/** 注意 ***/
if(highlightGoal==null){
highlightGoal = goal;
}
}
/** 封装高亮信息 **/
Info info = new Info();
info.setNo(doc.getField("no").stringValue());
info.setWeight(isearcher.explain(query, scoreDocs[i].doc).getValue());
info.setGoal(highlightGoal);
list.add(info);
}
return list;
}
在做这个例子的过程中遇到了一个问题,出现在
if(highlightGoal==null){
highlightGoal = goal;
}
如果goal中没有关键词 词元,highlightGoal就会为null
分享到:
相关推荐
lucene 高亮显示. java 已测试
lucene高亮显示
java实现lucene高亮显示Html,直接测试就可以用;java实现的对html进行高亮显示,
NULL 博文链接:https://navylee.iteye.com/blog/740128
基于SpringBoot编写的一个Lucene测试Demo把匹配到的结果高亮摘要显示在前端jsp上
NULL 博文链接:https://sunlongan666.iteye.com/blog/580380
这个是自己写的lucene分页高亮程序虽然有些不足但是毕竟是自己研究半天总结出来的麻烦各位帮忙给点分哈哈祝大家新年快乐!
Lucene高亮显示的源码,这个源码太难找了,这里共享给大家,希望有所帮助!
lucene.NET 中文分词 高亮 lucene.NET 中文分词 高亮 lucene.NET 中文分词 高亮 lucene.NET 中文分词 高亮
利用IKAnalyzer结合LUCENE.4.9进行中文分词的高亮显示。
lucene-memory-3.0.2.jar,lucene高亮显示中不可少的jar包lucene-memory-*.jar
java实现高亮显示的jar包,lucene用的jar包,net.teamhot.lucene
项目中代码包含了lucene的增删改查,中文词分器、luke查看工具、高亮显示等,mysql数据库等,作为新手学习lucene完全够用了,如果想深入学习,需要对其中属性等深入研究,请自行百度。
该项目中包括创建索引,增删改查索引,以及关键字高亮显示实例~~对于初学者很有帮助,该项目是基于Lucene3.0
希望大家可以关注我的博客http://blog.csdn.net/wuyinggui10000/article/category/3173543
java全文索引,高亮显示,lucene-4.7.0工具类
NULL 博文链接:https://gaojunwei.iteye.com/blog/1842881
分词、盘古分词、搜索关键词高亮显示。包含了lucene的基本操作
lucene全文搜索ajax例子,集成高亮显示。多次搜索等功能了,解压就可以运行了
个人搜索引擎开发的三种高亮显示的方法小结