Compass对搜索关键字实现高亮显示
例如搜"广东",实现后输出结果如下:<font color="red"><b>广</b></font><font color="red"><b>东</b></font>科学中心新老主任顺利交接.
实现步骤:
1、applicationContext-compass.xml中添加高亮配置
2、查询关键代码如下://...@Overridepublic PagerModel searchByQueryString(String trends_title) {List trendsList = new ArrayList();Compass compass = compassTemplate.getCompass();CompassSession session = compass.openSession();// 从第几条记录开始int offset = SystemContext.getOffset();// 每页显示几条数据int pageSize = SystemContext.getPagesize();CompassTransaction tx = session.beginLocalTransaction();CompassQuery compassQuery = session.queryBuilder().queryString("trends_title:"+trends_title).toQuery();CompassHits hits = compassQuery.hits();// 分页储存数据for (int i = offset; i < offset + pageSize && i < hits.length(); i++) {Trends p = (Trends)hits.data(i);String trends_title_highlighter = hits.highlighter(i).fragment("trends_title");p.setTrends_title(trends_title_highlighter);trendsList.add(p);}tx.commit();PagerModel pm = new PagerModel();pm.setDatas(trendsList);pm.setTotal(hits.length());return pm;}//...