代码高亮显示是通过在搜索到的Term中把匹配了用户输入的关键字的周围加上一些标记来实现(比如,关键字是 "中华" 查到的一个Term是 "中华人民共和国",则把这个Term修改一下 把"中华" 两个字加粗,最终结果是这样的: <b>中华</b>人民共和国 )
下边是具体实现的代码片段:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 |
TopDocs tds = searcher.search(booleanQuery, 500 ); ScoreDoc[] sds = tds.scoreDocs; <strong>Formatter formatter = new
SimpleHTMLFormatter( "<span class=\"highlight\">" , "</span>" );</strong> <strong>Highlighter hl = new
Highlighter(formatter, new
QueryScorer(booleanQuery));</strong> for
(ScoreDoc sd : sds) { Document doc = searcher.doc(sd.doc); //title的高亮显示 TokenStream tokenScream_title = context.analyzer.tokenStream( "title" , new
StringReader(doc.get( "title" ))); String title = hl.getBestFragment(tokenScream_title, doc.get( "title" )); //若返回空表示doc.get("title")中不包含相应的关键字 if (title== null ){ title=doc.get( "title" ); } // System.out.println("title"+title); //summary的高亮显示 TokenStream tokenScream_summary = context.analyzer.tokenStream( "summary" , new
StringReader(doc.get( "summary" ))); String summary = hl.getBestFragment(tokenScream_summary, doc.get( "summary" )); if (summary== null ){ summary=doc.get( "summary" ); } // System.out.println("summary" + summary); |
代码片段,Lucene的高亮显示,布布扣,bubuko.com
原文:http://www.cnblogs.com/jiaoyiping/p/3761651.html