直接上代码~~
这是逻辑代码 :
FastVectorHighlighter fvh = new FastVectorHighlighter(false,false);
Analyzer a = new MMSegAnalyzer();
IndexSearcher searcher = new IndexSearcher(IndexReader.open(FileIndexUtil.getDirectory()));
MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, new String[]{"title","content"}, a);
Query query = parser.parse(name);
FieldQuery fq = fvh.getFieldQuery(query);
TopDocs tds = searcher.search(query, 20);
for(ScoreDoc sd : tds.scoreDocs) {
String highTitle = fvh.getBestFragment(fq, searcher.getIndexReader(), sd.doc, "title", 100);
System.out.println(highTitle);
System.out.println("-------------------------");
String highContent = fvh.getBestFragment(fq, searcher.getIndexReader(), sd.doc, "content",100);
System.out.println(highContent);
System.out.println("**********************************************************************");
}
searcher.close();
这是测试代码:
public void testSearcherByFastHighlighter(){
SearchUtil su = new SearchUtil();
su.searcherByFastHighlighter("开");
}
不知道为什么结果一直返回null
------解决方案--------------------
高亮显示要加 标签自己标红呀,使用SimpleHTMLFormatter 在关键字前后替换一下
SimpleHTMLFormatter simpleHtmlFormatter = new SimpleHTMLFormatter("【", "】");
http://blog.csdn.net/chenghui0317/article/details/10281311