TopDocs2Page
把查询结果转换为SolrDocument
Document2IndexResult.transition(sd,keyword);
把document转化为实体,同事采用高亮效果
ContentMapper questionMapper = SpringContextTool.getApplicationContext().getBean(ContentMapper.class);
String questionContent=questionMapper.findContentById(entityId);
//高亮效果 text为全部内容
public static String highligher(String keyword,String text) {
text=RemoveHtml.delHTMLTag(text);
try {
if(keyword==null||"".equals(keyword.trim())) return subStr(text);
QueryParser parser = new QueryParser(LuceneContext.getInstance().getVersion(), "content",LuceneContext.getInstance().getAnalyzer());
Query query = parser.parse(keyword);
QueryScorer scorer=new QueryScorer(query);
//定义标注高亮文本的标签
SimpleHTMLFormatter formatter=new SimpleHTMLFormatter("<font class='lighter'>","</font>");
Highlighter highlighter=new Highlighter(formatter,scorer);
Fragmenter fragmenter=new SimpleFragmenter(20);//减少片断的默认大小
highlighter.setTextFragmenter(fragmenter);
//原始文本语汇单元化
TokenStream tokenstream=LuceneContext.getInstance().getAnalyzer().tokenStream("content",new StringReader(text));
String ht = highlighter.getBestFragments(tokenstream,text,2,"......");
if(ht==null||ht.equals("")) {
return subStr(text);
}else{
return ht.trim();
}
} catch (Exception e) {
e.printStackTrace();
}
return text;
}