爱学习的蹭蹭 2017-08-24 04:09 采纳率: 0%
浏览 1620
已结题

lucene搜索后,前端HTML页面没有高亮显示

** 问题描述:**
项目是前后端分离,后端Java开发,前端纯HTML显示,查询出结果返回给前端后,前端还是原文输出
** 前端结果:**
图片说明
** 后端部分代码**

/**
 * 设置关键字高亮
 * @param query 查询对象
 * @param list 设置高亮的内容列表
 * @param fields 字段名
 */
public List<T> keyWordsHighLight(BooleanQuery query, List<T> list, String... fields){
    Analyzer analyzer = new IKAnalyzer();
    /**自定义标注高亮文本标签*/ 
    SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<font color='red'>","</font>");   
    Highlighter highlighter = new Highlighter(formatter, new QueryScorer(query)); 
    highlighter.setTextFragmenter(new SimpleFragmenter(130)); 

    for(T entity : list){ 
        try {
            for (String field : fields){
                if(ObjectUtil.isNotEmpty(field) && !"tag".equals(field)){
                    String text = ObjectUtil.replaceHtml((String)Reflections.invokeGetter(entity, field));
                    String desciption = highlighter.getBestFragment(analyzer,field,text);
                    if(desciption!=null){
                        Reflections.invokeSetter(entity, fields[0], desciption);
                        break;
                    }
                    Reflections.invokeSetter(entity, fields[0], ObjectUtil.abbr(text, 130));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidTokenOffsetsException e) {
            e.printStackTrace();
        } 
    }
    return list;
}
  • 写回答

3条回答 默认 最新

  • cloudyzhao 2017-08-24 05:13
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?