Java Code Examples for org.elasticsearch.search.highlight.HighlightField#fragments()

The following examples show how to use org.elasticsearch.search.highlight.HighlightField#fragments() . 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: XiaoEUKResultMapper.java    From youkefu with Apache License 2.0 7 votes vote down vote up
public <T> T mapEntity(String source , SearchHit hit , Class<T> clazz) {
	T t = mapEntity(source , clazz) ;
	
	Map<String, HighlightField> highlightFields = hit.getHighlightFields();
	HighlightField highlightNameField = highlightFields.get("title");
	HighlightField contentHightlightField = highlightFields.get("content");
	try {
		if(highlightNameField!=null&&highlightNameField.fragments()!=null){
			PropertyUtils.setProperty(t, "title" , highlightNameField.fragments()[0].string());
		}
		if(contentHightlightField!=null){
			PropertyUtils.setProperty(t, "content" , contentHightlightField.fragments()[0].string());
		}
		PropertyUtils.setProperty(t, "id" , hit.getId());
	} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
		e.printStackTrace();
	} 
	return t;
}
 
Example 2
Source File: UKResultMapper.java    From youkefu with Apache License 2.0 6 votes vote down vote up
public <T> T mapEntity(String source , SearchHit hit , Class<T> clazz) {
	T t = mapEntity(source , clazz) ;
	
	Map<String, HighlightField> highlightFields = hit.getHighlightFields();
	HighlightField highlightNameField = highlightFields.get("title");
	HighlightField contentHightlightField = highlightFields.get("content");
	try {
		if(highlightNameField!=null&&highlightNameField.fragments()!=null){
			PropertyUtils.setProperty(t, "title" , highlightNameField.fragments()[0].string());
		}
		if(contentHightlightField!=null){
			PropertyUtils.setProperty(t, "content" , contentHightlightField.fragments()[0].string());
		}
		PropertyUtils.setProperty(t, "id" , hit.getId());
	} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
		e.printStackTrace();
	} 
	return t;
}
 
Example 3
Source File: EKMResultMapper.java    From youkefu with Apache License 2.0 6 votes vote down vote up
public <T> T mapEntity(String source , SearchHit hit , Class<T> clazz) {
	T t = mapEntity(source , clazz) ;
	
	Map<String, HighlightField> highlightFields = hit.getHighlightFields();
	HighlightField highlightNameField = highlightFields.get("title");
	HighlightField contentHightlightField = highlightFields.get("content");
	try {
		if(highlightNameField!=null&&highlightNameField.fragments()!=null){
			PropertyUtils.setProperty(t, "title" , highlightNameField.fragments()[0].string());
		}
		if(contentHightlightField!=null && contentHightlightField.fragments()!=null && contentHightlightField.fragments().length >0 && !StringUtils.isBlank(contentHightlightField.fragments()[0].string())){
			PropertyUtils.setProperty(t, "summary" , contentHightlightField.fragments()[0].string());
		}
	} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
		e.printStackTrace();
	} 
	return t;
}
 
Example 4
Source File: EsQuery.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
/**
 * 对搜索命中结果做高亮
 *
 * @param json
 * @param hit
 * @param highLigthFieldName
 */
private void highLightResult (JSONObject json, SearchHit hit, String highLigthFieldName) {
    //获取对应的高亮域
    Map<String, HighlightField> result = hit.highlightFields();
    //从设定的高亮域中取得指定域
    HighlightField hlField = result.get(highLigthFieldName);
    if (Check.NuNObj(hlField)) {
        return;
    }
    //取得定义的高亮标签
    Text[] hlTexts = hlField.fragments();
    if (Check.NuNObject(hlTexts)) {
        return;
    }
    //为title串值增加自定义的高亮标签
    StringBuffer hlTextsFiled = new StringBuffer();
    for (Text text : hlTexts) {
        hlTextsFiled.append(text);
    }
    //如果高亮域内有fragments 反回的数据不为空字符串
    if (!Check.NuNStrStrict(hlTextsFiled.toString())) {
        json.put(highLigthFieldName, hlTextsFiled);
    }
}
 
Example 5
Source File: EsHighLight.java    From es-service-parent with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param hit
 * @param seach_fileds
 * @return
 */
public static Map<String, Object> getHighlight(SearchHit hit, List<String> seach_fileds) {
    Map<String, Object> result = new HashMap<String, Object>();
    Map<String, HighlightField> highlights = hit.highlightFields();
    for (String filed : seach_fileds) {
        HighlightField highlight = highlights.get(filed);
        if (null == highlight) {
            continue;
        }
        StringBuffer sb = new StringBuffer();
        Text[] fragments = highlight.fragments();
        for (Text fragment : fragments) {
            sb.append(fragment);
        }
        result.put(filed + "_HIGH", sb.toString());
    }
    return result;

}
 
Example 6
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 7
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());
	}
}
  }
 
Example 8
Source File: ResultHighlightMapper.java    From klask-io with GNU General Public License v3.0 4 votes vote down vote up
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
    List<File> result = new ArrayList<>();
    long totalHits = response.getHits().getTotalHits();
    for (SearchHit searchHit : response.getHits()) {
        if (response.getHits().getHits().length <= 0) {
            return null;
        }

        //System.out.println(response.toString());

        String summaryWithHighlight = null;
        String pathWithHighlight = null;
        HighlightField highlightFieldContent = searchHit.getHighlightFields().get("content");
        HighlightField highlightFieldPath = searchHit.getHighlightFields().get("path");
        if (highlightFieldContent != null) {
            summaryWithHighlight = Arrays.stream(highlightFieldContent.fragments())
                .map(text -> EncodingUtil.convertToUTF8(text.toString()))
                .collect(Collectors.joining("\n[...]\n"));
        }
        if (highlightFieldPath != null && highlightFieldPath.fragments() != null) {
            pathWithHighlight = EncodingUtil.unEscapeString(highlightFieldPath.fragments()[0].toString());
        }
        File oneFile = new File(
            (String) searchHit.getSource().get("id"),
            (String) searchHit.getSource().get("name"),
            (String) searchHit.getSource().get("extension"),
            pathWithHighlight != null ? pathWithHighlight : (String) searchHit.getSource().get("path"),
            (String) searchHit.getSource().get("project"),
            summaryWithHighlight,
            (String) searchHit.getSource().get("version"),
            //conversion en string puis en long, très bizarre, à l'origine, il était préférable de réaliser :
            //(Long) searchHit.getSource().get("size")
            //mais cela jette un classCastException Integer to Long
            Long.valueOf(searchHit.getSource().get("size").toString())
        );
        oneFile.setScore(searchHit.getScore());
        result.add(oneFile);
    }
    return new AggregatedPageImpl<>((List<T>) result, pageable, totalHits, response.getAggregations());
}