查询结果高亮显示

代码实现:

/**
* 高亮显示
* 1:通过关键词进行查询
* 2:对关键词进行高亮的显示
* 3: 高亮显示的内容替换原有内容
* */
@Test
public void searchHightLight() throws IOException {
   //1:通过关键词进行查询
   SearchRequestBuilder builder = transportClient.prepareSearch("sanguo").setTypes("dahan")
          .setQuery(QueryBuilders.fuzzyQuery("address", "北京"));
   //2:对查询出来的关键词设置高亮--关键词
   HighlightBuilder highlightBuilder = new HighlightBuilder();
   highlightBuilder.field("address");
   highlightBuilder.preTags("<em>");//前置元素
   highlightBuilder.postTags("</em>");//后置元素
   //将高亮设置信息放入查询条件中
   builder.highlighter(highlightBuilder);
   SearchResponse searchResponse = builder.get();
   SearchHits hits = searchResponse.getHits();
   Iterator<SearchHit> iterator = hits.iterator();
   ObjectMapper objectMapper = new ObjectMapper();
   while (iterator.hasNext()){
       SearchHit searchHitFields = iterator.next();
       //将高亮处理后的内容,替换原有的内容
       Map<String, HighlightField> highlightFields = searchHitFields.getHighlightFields();
       HighlightField content = highlightFields.get("address");
       //fragments就是高亮显示的片段
       Text[] fragments = content.fragments();
       StringBuffer sb = new StringBuffer();
       //拼接查询出来的高亮片段
       for (Text text:fragments){
           sb.append(text);
      }
       //将查询结果转化为对象--因为我们要进行替换,所以要转化成对象,然后直接将高亮片段替换原有对象内容
       Article article = objectMapper.readValue(searchHitFields.getSourceAsString() , Article.class);
       //开始进行高亮替换,但是要做一次校验,因为可能查询不到具体内容,那么就是没有高亮显示的,所以不要把空内容替换原有内容
       if(sb.toString() != null || !"".equals(sb.toString())){
           //替换
           article.setAddress(sb.toString());
      }
       System.out.println(article.toString());
  }
}

编写Article实体类:



/**
* Created by angel;
*/
public class Article {
   private String studentNo;
   private String name;
   private String male;
   private String age;
   private String birthday;
   private String classNo;
   private String address;
   private String isLeader;

   public void setAddress(String address) {
       this.address = address;
  }

   public void setAge(String age) {
       this.age = age;
  }

   public void setBirthday(String birthday) {
       this.birthday = birthday;
  }

   public void setClassNo(String classNo) {
       this.classNo = classNo;
  }

   public void setMale(String male) {
       this.male = male;
  }

   public void setName(String name) {
       this.name = name;
  }

   public void setStudentNo(String studentNo) {
       this.studentNo = studentNo;
  }

   public void setIsLeader(String isLeader) {
       this.isLeader = isLeader;
  }

   public String getAddress() {
       return address;
  }

   public String getAge() {
       return age;
  }

   public String getMale() {
       return male;
  }

   public String getBirthday() {
       return birthday;
  }

   public String getClassNo() {
       return classNo;
  }

   public String getIsLeader() {
       return isLeader;
  }

   public String getName() {
       return name;
  }

   public String getStudentNo() {
       return studentNo;
  }

   @Override
   public String toString() {
       return "[studentNO:"+studentNo+" name: +"+name+" age :"+age+" birthday: "+birthday+" classNo: "+classNo+" address: "+address+" isLeader: "+isLeader+"]";
  }
}

 

posted @ 2017-05-22 23:32  niutao  阅读(1071)  评论(0编辑  收藏  举报