Java Code Examples for org.elasticsearch.action.search.SearchRequestBuilder#addHighlightedField()

The following examples show how to use org.elasticsearch.action.search.SearchRequestBuilder#addHighlightedField() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EsHighLight.java    From es-service-parent with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param srb
 * @param highlightFields
 */
public static void setHighLight(SearchRequestBuilder srb, EsHighlightFields highlightFields) {
    if (highlightFields == null || highlightFields.getFields() == null) {
        return;
    }
    srb.setHighlighterPreTags(highlightFields.getPreTags());
    srb.setHighlighterPostTags(highlightFields.getPostTags());
    srb.setHighlighterRequireFieldMatch(highlightFields.getRequireFieldMatch());
    for (String word : highlightFields.getFields()) {
        srb.addHighlightedField(word, highlightFields.getFragmentSize(),
                highlightFields.getNumOfFragments());
    }
}
 
Example 2
Source File: SearchServiceImpl.java    From emotional_analysis with Apache License 2.0 4 votes vote down vote up
@Override
public PageBean<Map<String,Object>> getSearchResult(int page, int rows,String content) throws Exception {

	PageBean<Map<String, Object>> searchListPageBean = new PageBean<Map<String, Object>>();
	BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
	boolBuilder.must(QueryBuilders.matchQuery("content", content));
	PageHelper.startPage(page, rows);
	SearchRequestBuilder searchRequestBuilder = ESClientUtils.getClient().prepareSearch("hotcomments")
			.setTypes("logs").setSearchType(SearchType.DFS_QUERY_THEN_FETCH) 
			.setQuery(boolBuilder).setFrom(page).setSize(3);
	long totalHits = ESClientUtils.getClient().prepareSearch("hotcomments").setTypes("logs")
			.setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(boolBuilder).get().getHits().getTotalHits();
	searchRequestBuilder.setHighlighterPostTags("</span>");
	searchRequestBuilder.setHighlighterPreTags("<span style=\"color:red\">");
	searchRequestBuilder.addHighlightedField("content");
	// 执行结果
	SearchResponse response = searchRequestBuilder.get();
	// 接受结果
	List<Map<String, Object>> result = new ArrayList<>();
	// 遍历结果
	for (SearchHit hit : response.getHits()) {
		Map<String, Object> source = hit.getSource();
		String songName = null;
		String songUrl = null;
		Set<Entry<String, Object>> entrySet = source.entrySet();
		for (Entry<String, Object> entry : entrySet) {
			if (entry.getKey().equals("songId")) {
				Integer songId = (Integer) entry.getValue();
				songName = SearchUtils.getSongNameById(songId);
				songUrl = SearchUtils.getSongUrlById(songId);
			}
		}
		source.put("songName", songName);
		source.put("songUrl", songUrl);
		// 处理高亮片段
		Map<String, HighlightField> highlightFields = hit.getHighlightFields();
		HighlightField nameField = highlightFields.get("content");
		if (nameField != null) {
			Text[] fragments = nameField.fragments();
			String nameTmp = "";
			for (Text text : fragments) {
				nameTmp += text;
			}
			// 将高亮片段组装到结果中去
			source.put("content", nameTmp);
		}
		result.add(source);
	}
	long totalPage = (totalHits + 3 - 1) / 3;
	if(page > totalPage){
		page = 1;
	}
	searchListPageBean.setPage(page);
	searchListPageBean.setTotalPage(totalPage);
	searchListPageBean.setList(result);
	return searchListPageBean;
}
 
Example 3
Source File: ESClientTest.java    From emotional_analysis with Apache License 2.0 4 votes vote down vote up
@Test
  public void test5(){
      BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();  
      //使用should实现或者查询  
      boolBuilder.should(QueryBuilders.matchQuery("content","爱我"));  
      //c查询  
      SearchRequestBuilder searchRequestBuilder = this.client.prepareSearch("hotcomments")  
              .setTypes("logs")  
              .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) //设置查询类型:1.SearchType.DFS_QUERY_THEN_FETCH 精确查询; 2.SearchType.SCAN 扫描查询,无序  
              .setQuery(boolBuilder)  
              .setSize(10);  
 
      //设置高亮显示  
      searchRequestBuilder.setHighlighterPostTags("</span>");
      searchRequestBuilder.setHighlighterPreTags("<span style=\"color:red\">");
      searchRequestBuilder.addHighlightedField("content"); 
      //执行结果  
      SearchResponse response = searchRequestBuilder.get();  
      //接受结果  
      List<Map<String,Object>> result = new ArrayList<>();  
      //遍历结果  
      for(SearchHit hit:response.getHits()){  
          Map<String, Object> source = hit.getSource();  
          //处理高亮片段  
          Map<String, HighlightField> highlightFields = hit.getHighlightFields();  
          HighlightField nameField = highlightFields.get("content");  
          if(nameField!=null){  
              Text[] fragments = nameField.fragments();  
              String nameTmp ="";  
              for(Text text:fragments){  
                  nameTmp+=text;  
              }  
              //将高亮片段组装到结果中去  
              source.put("content",nameTmp);
          }  
          result.add(source);  
      }  
      for (Map<String, Object> map : result) {
	Set<Entry<String, Object>> entrySet = map.entrySet();
	for (Entry<String, Object> entry : entrySet) {
		System.out.println(entry.getKey()+":::"+entry.getValue());
	}
}
  }