org.elasticsearch.search.highlight.HighlightField Java Examples

The following examples show how to use org.elasticsearch.search.highlight.HighlightField. 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: 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 #5
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 #6
Source File: PercolateShardResponse.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public PercolateShardResponse(BytesRef[] matches, List<Map<String, HighlightField>> hls, long count, float[] scores, PercolateContext context, ShardId shardId) {
    super(shardId);
    this.matches = matches;
    this.hls = hls;
    this.count = count;
    this.scores = scores;
    this.percolatorTypeId = context.percolatorTypeId;
    this.requestedSize = context.size();
    QuerySearchResult result = context.queryResult();
    if (result != null) {
        if (result.aggregations() != null) {
            this.aggregations = (InternalAggregations) result.aggregations();
        }
        this.pipelineAggregators = result.pipelineAggregators();
    }
}
 
Example #7
Source File: PercolateResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor only for internal usage.
 */
public Match(Text index, Text id, float score, Map<String, HighlightField> hl) {
    this.id = id;
    this.score = score;
    this.index = index;
    this.hl = hl;
}
 
Example #8
Source File: PercolateShardResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(StreamOutput out) throws IOException {
    super.writeTo(out);
    out.writeByte(percolatorTypeId);
    out.writeVLong(requestedSize);
    out.writeVLong(count);
    out.writeVInt(matches.length);
    for (BytesRef match : matches) {
        out.writeBytesRef(match);
    }
    out.writeVLong(scores.length);
    for (float score : scores) {
        out.writeFloat(score);
    }
    out.writeVInt(hls.size());
    for (Map<String, HighlightField> hl : hls) {
        out.writeVInt(hl.size());
        for (Map.Entry<String, HighlightField> entry : hl.entrySet()) {
            out.writeString(entry.getKey());
            entry.getValue().writeTo(out);
        }
    }
    out.writeOptionalStreamable(aggregations);
    if (pipelineAggregators == null) {
        out.writeBoolean(false);
    } else {
        out.writeBoolean(true);
        out.writeVInt(pipelineAggregators.size());
        for (PipelineAggregator pipelineAggregator : pipelineAggregators) {
            out.writeBytesReference(pipelineAggregator.type().stream());
            pipelineAggregator.writeTo(out);
        }
    }
}
 
Example #9
Source File: PercolateShardResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    percolatorTypeId = in.readByte();
    requestedSize = in.readVInt();
    count = in.readVLong();
    matches = new BytesRef[in.readVInt()];
    for (int i = 0; i < matches.length; i++) {
        matches[i] = in.readBytesRef();
    }
    scores = new float[in.readVInt()];
    for (int i = 0; i < scores.length; i++) {
        scores[i] = in.readFloat();
    }
    int size = in.readVInt();
    for (int i = 0; i < size; i++) {
        int mSize = in.readVInt();
        Map<String, HighlightField> fields = new HashMap<>();
        for (int j = 0; j < mSize; j++) {
            fields.put(in.readString(), HighlightField.readHighlightField(in));
        }
        hls.add(fields);
    }
    aggregations = InternalAggregations.readOptionalAggregations(in);
    if (in.readBoolean()) {
        int pipelineAggregatorsSize = in.readVInt();
        List<SiblingPipelineAggregator> pipelineAggregators = new ArrayList<>(pipelineAggregatorsSize);
        for (int i = 0; i < pipelineAggregatorsSize; i++) {
            BytesReference type = in.readBytesReference();
            PipelineAggregator pipelineAggregator = PipelineAggregatorStreams.stream(type).readResult(in);
            pipelineAggregators.add((SiblingPipelineAggregator) pipelineAggregator);
        }
        this.pipelineAggregators = pipelineAggregators;
    }
}
 
Example #10
Source File: PercolateResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(StreamOutput out) throws IOException {
    out.writeText(id);
    out.writeText(index);
    out.writeFloat(score);
    if (hl != null) {
        out.writeVInt(hl.size());
        for (Map.Entry<String, HighlightField> entry : hl.entrySet()) {
            out.writeString(entry.getKey());
            entry.getValue().writeTo(out);
        }
    } else {
        out.writeVInt(0);
    }
}
 
Example #11
Source File: PercolateResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    id = in.readText();
    index = in.readText();
    score = in.readFloat();
    int size = in.readVInt();
    if (size > 0) {
        hl = new HashMap<>(size);
        for (int j = 0; j < size; j++) {
            hl.put(in.readString(), HighlightField.readHighlightField(in));
        }
    }
}
 
Example #12
Source File: InternalSearchHit.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, HighlightField> highlightFields() {
    if (highlightFields == null) {
        return ImmutableMap.of();
    }
    return this.highlightFields;
}
 
Example #13
Source File: PercolateResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * @return If highlighting was specified in the percolate request the this returns highlight snippets for each
 * matching field in the document being percolated based on this query otherwise <code>null</code> is returned.
 */
@Nullable
public Map<String, HighlightField> getHighlightFields() {
    return hl;
}
 
Example #14
Source File: InternalSearchHit.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, HighlightField> getHighlightFields() {
    return highlightFields();
}
 
Example #15
Source File: InternalSearchHit.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Map<String, HighlightField> internalHighlightFields() {
    return highlightFields;
}
 
Example #16
Source File: PercolateShardResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public PercolateShardResponse(BytesRef[] matches, List<Map<String, HighlightField>> hls, long count, PercolateContext context, ShardId shardId) {
    this(matches, hls, count, EMPTY_SCORES, context, shardId);
}
 
Example #17
Source File: PercolateShardResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public List<Map<String, HighlightField>> hls() {
    return hls;
}
 
Example #18
Source File: QueryCollector.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
List<Map<String, HighlightField>> hls() {
    return hls;
}
 
Example #19
Source File: QueryCollector.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
List<Map<String, HighlightField>> hls() {
    return hls;
}
 
Example #20
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 #21
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());
}
 
Example #22
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 #23
Source File: SearchServlet.java    From stash-codesearch-plugin with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private ImmutableMap<String, Object> searchHitToDataMap(
    SearchHit hit,
    Map<String, Repository> repoMap, // null iff no permission validation required
    int maxPreviewLines,
    int maxMatchLines,
    ImmutableSet<String> noHighlight) {
    ImmutableMap.Builder<String, Object> hitData = new ImmutableMap.Builder<String, Object>();
    Map<String, Object> hitSource = hit.getSource();

    String type = hit.getType();
    hitData.put("type", type);

    String project = getStringFromMap(hitSource, "project");
    hitData.put("project", project);

    String repository = getStringFromMap(hitSource, "repository");
    hitData.put("repository", repository);

    // Validate permissions & build hit data map
    String repoId = project + "^" + repository;
    Repository repoObject;
    if (repoMap == null) { // current user is system administrator
        repoObject = repositoryServiceManager.getRepositoryService().getBySlug(
            project, repository);
    } else { // must validate against allowed repositories for non-administrators
        repoObject = repoMap.get(repoId);
    }

    if (repoObject != null &&
        repoObject.getProject().getKey().equals(project) &&
        repoObject.getSlug().equals(repository)) {

        // Generate refs array
        ImmutableSortedSet<String> refSet;
        try {
            refSet = ImmutableSortedSet.copyOf((Iterable<String>) hitSource.get("refs"));
        } catch (Exception e) {
            log.warn("Invalid refs collection detected for element in {}/{}", project, repository, e);
            return null;
        }
        if (refSet.isEmpty()) {
            log.warn("Detected empty refs collection for element in {}/{}", project, repository);
            return null;
        }
        hitData.put("refs", refSet);

        // Human-readable labels
        hitData
            .put("projectname", repoObject.getProject().getName())
            .put("repositoryname", repoObject.getName());

        if (type.equals("commit")) {
            hitData
                .put("hash", getStringFromMap(hitSource, "hash"))
                .put("subject", getStringFromMap(hitSource, "subject"))
                .put("body", getStringFromMap(hitSource, "body"))
                .put("commitDate", getDateStringFromMap(hitSource, "commitdate"))
                .put("authorName", getStringFromMap(hitSource, "authorname"))
                .put("authorEmail", getStringFromMap(hitSource, "authoremail"));

        } else if (type.equals("file")) {
            HighlightField highlightField = hit.getHighlightFields().get("contents");
            String path = getStringFromMap(hitSource, "path");
            String primaryRef = "refs/heads/master";
            if (!refSet.contains(primaryRef)) {
                primaryRef = refSet.iterator().next();
            }
            String contents = getStringFromMap(hitSource, "contents");
            SourceSearch searchedContents = SourceSearch.search(
                contents, highlightField, 1, maxPreviewLines, maxMatchLines);
            String extension = getStringFromMap(hitSource, "extension");

            hitData
                .put("path", path)
                .put("blob", getStringFromMap(hitSource, "blob"))
                .put("primaryRef", primaryRef)
                .put("sourceLines", searchedContents.getJoinedLines())
                .put("sourceLineNums", searchedContents.getJoinedLineNums())
                .put("isPreview", searchedContents.isPreview())
                .put("shownLines", searchedContents.getLines().length)
                .put("excessLines", searchedContents.getExcess())
                .put("extension", extension)
                .put("noHighlight", noHighlight.contains(extension));
        }
    } else {
        return null;
    }

    return hitData.build();
}
 
Example #24
Source File: SearchHit.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * A map of highlighted fields.
 */
Map<String, HighlightField> getHighlightFields();
 
Example #25
Source File: SearchHit.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * A map of highlighted fields.
 */
Map<String, HighlightField> highlightFields();