- SolrUtil
增加queryHighlight 方法public static void queryHighlight(String keywords) throws SolrServerException, IOException { SolrQuery q = new SolrQuery(); //开始页数 q.setStart(0); //每页显示条数 q.setRows(10); // 设置查询关键字 q.setQuery(keywords); // 开启高亮 q.setHighlight(true); // 高亮字段 q.addHighlightField("name"); // 高亮单词的前缀 q.setHighlightSimplePre("<span style='color:red'>"); // 高亮单词的后缀 q.setHighlightSimplePost("</span>"); //摘要最长100个字符 q.setHighlightFragsize(100); //查询 QueryResponse query = client.query(q); //获取高亮字段name相应结果 NamedList<Object> response = query.getResponse(); NamedList<?> highlighting = (NamedList<?>) response.get("highlighting"); for (int i = 0; i < highlighting.size(); i++) { System.out.println(highlighting.getName(i) + ":" + highlighting.getVal(i)); } //获取查询结果 SolrDocumentList results = query.getResults(); for (SolrDocument result : results) { System.out.println(result.toString()); } }
- TestSolr4j
调用queryHighlight 方法public class TestSolr4j { public static void main(String[] args) throws SolrServerException, IOException { //高亮查询查询 SolrUtil.queryHighlight("name:手机"); } }
Solr-SolrJ 高亮显示
最新推荐文章于 2019-06-30 21:13:19 发布