private static Optional<String> getHighlightedText(SearchHit hit) { return ofNullable(hit.getHighlightFields()) .flatMap(fields -> ofNullable(fields.get(FIELD_NAME))) .flatMap(field -> ofNullable(field.getFragments())) .flatMap(fragments -> stream(fragments).findFirst()) .map(Text::string); }
@Override public int hashCode() { return Objects.hash(id, type, nestedIdentity, version, source, fields, getHighlightFields(), Arrays.hashCode(matchedQueries), explanation, shard, innerHits, index, clusterAlias); }
@Override public boolean equals(Object obj) { if (obj == null || getClass() != obj.getClass()) { return false; } SearchHit other = (SearchHit) obj; return Objects.equals(id, other.id) && Objects.equals(type, other.type) && Objects.equals(nestedIdentity, other.nestedIdentity) && Objects.equals(version, other.version) && Objects.equals(source, other.source) && Objects.equals(fields, other.fields) && Objects.equals(getHighlightFields(), other.getHighlightFields()) && Arrays.equals(matchedQueries, other.matchedQueries) && Objects.equals(explanation, other.explanation) && Objects.equals(shard, other.shard) && Objects.equals(innerHits, other.innerHits) && Objects.equals(index, other.index) && Objects.equals(clusterAlias, other.clusterAlias); }
if (!map.containsKey("type")) map.put("type", hit.getType()); this.results.add(map); this.highlights.add(hit.getHighlightFields()); if (explain) { Explanation explanation = hit.getExplanation();
@Override public boolean isHighlighted() { return (hit.getHighlightFields() != null); }
@Override public int hashCode() { return Objects.hash(id, type, nestedIdentity, version, source, fields, getHighlightFields(), Arrays.hashCode(matchedQueries), explanation, shard, innerHits, index, clusterAlias); }
@Override public int hashCode() { return Objects.hash(id, type, nestedIdentity, version, source, fields, getHighlightFields(), Arrays.hashCode(matchedQueries), explanation, shard, innerHits, index, clusterAlias); }
esSearchHit.setSourceAsString(hit.getSourceAsString()); hit.getHighlightFields().forEach((key, value) -> { String[] texts = new String[value.fragments().length]; for (int i = 0; i < value.fragments().length; i++) {
esSearchHit.setSourceAsString(hit.getSourceAsString()); hit.getHighlightFields().forEach((key, value) -> { String[] texts = new String[value.fragments().length]; for (int i = 0; i < value.fragments().length; i++) {
esSearchHit.setSourceAsString(hit.getSourceAsString()); hit.getHighlightFields().forEach((key, value) -> { String[] texts = new String[value.fragments().length]; for (int i = 0; i < value.fragments().length; i++) {
private static Optional<String> getHighlightedText(SearchHit hit) { return ofNullable(hit.getHighlightFields()) .flatMap(fields -> ofNullable(fields.get(FIELD_NAME))) .flatMap(field -> ofNullable(field.getFragments())) .flatMap(fragments -> stream(fragments).findFirst()) .map(Text::string); }
private static void assertHighlight(SearchHit hit, String field, int fragment, Matcher<Integer> fragmentsMatcher, Matcher<String> matcher) { assertThat(hit.getHighlightFields(), hasKey(field)); assertThat(hit.getHighlightFields().get(field).fragments().length, fragmentsMatcher); assertThat(hit.getHighlightFields().get(field).fragments()[fragment].string(), matcher); }
@Override public boolean equals(Object obj) { if (obj == null || getClass() != obj.getClass()) { return false; } SearchHit other = (SearchHit) obj; return Objects.equals(id, other.id) && Objects.equals(type, other.type) && Objects.equals(nestedIdentity, other.nestedIdentity) && Objects.equals(version, other.version) && Objects.equals(source, other.source) && Objects.equals(fields, other.fields) && Objects.equals(getHighlightFields(), other.getHighlightFields()) && Arrays.equals(matchedQueries, other.matchedQueries) && Objects.equals(explanation, other.explanation) && Objects.equals(shard, other.shard) && Objects.equals(innerHits, other.innerHits) && Objects.equals(index, other.index) && Objects.equals(clusterAlias, other.clusterAlias); }
@Override public boolean equals(Object obj) { if (obj == null || getClass() != obj.getClass()) { return false; } SearchHit other = (SearchHit) obj; return Objects.equals(id, other.id) && Objects.equals(type, other.type) && Objects.equals(nestedIdentity, other.nestedIdentity) && Objects.equals(version, other.version) && Objects.equals(source, other.source) && Objects.equals(fields, other.fields) && Objects.equals(getHighlightFields(), other.getHighlightFields()) && Arrays.equals(matchedQueries, other.matchedQueries) && Objects.equals(explanation, other.explanation) && Objects.equals(shard, other.shard) && Objects.equals(innerHits, other.innerHits) && Objects.equals(index, other.index) && Objects.equals(clusterAlias, other.clusterAlias); }
public static void assertNotHighlighted(SearchResponse resp, int hit, String field) { assertNoFailures(resp); assertThat("not enough hits", resp.getHits().getHits().length, greaterThan(hit)); assertThat(resp.getHits().getHits()[hit].getHighlightFields(), not(hasKey(field))); }
@Override public Iterable<String> getSnippets(String property) { HighlightField highlightField = hit.getHighlightFields().get( ElasticsearchIndex.toPropertyFieldName(property)); if (highlightField == null) { return null; } return Iterables.transform(Arrays.asList(highlightField.getFragments()), new Function<Text, String>() { @Override public String apply(Text fragment) { return SearchFields.getSnippet(fragment.string()); } }); } }
private HighlightedSearchResult mapToHighlightedResult(SearchHit hit) { HttpArticle article = mapToHttpArticle(hit.getSource()); List<String> highlights = Lists.newArrayList(); for (Map.Entry<String, HighlightField> fields : hit.getHighlightFields().entrySet()) { for (Text text : fields.getValue().getFragments()) { highlights.add(text.toString()); } } return new HighlightedSearchResult(article, highlights); }
/** * 高亮结果集 特殊处理 * * @param searchResponse * @param highlightField */ private static List<Map<String, Object>> setSearchResponse(SearchResponse searchResponse, String highlightField) { List<Map<String, Object>> sourceList = new ArrayList<Map<String, Object>>(); StringBuffer stringBuffer = new StringBuffer(); for (SearchHit searchHit : searchResponse.getHits().getHits()) { searchHit.getSource().put("id", searchHit.getId()); if (StringUtils.isNotEmpty(highlightField)) { System.out.println("遍历 高亮结果集,覆盖 正常结果集" + searchHit.getSource()); Text[] text = searchHit.getHighlightFields().get(highlightField).getFragments(); if (text != null) { for (Text str : text) { stringBuffer.append(str.string()); } //遍历 高亮结果集,覆盖 正常结果集 searchHit.getSource().put(highlightField, stringBuffer.toString()); } } sourceList.add(searchHit.getSource()); } return sourceList; }
private void addHighlights(List<DocumentModel> docs) { for (SearchHit hit : getResponse().getHits()) { for (DocumentModel doc : docs) { String docId = doc.getRepositoryName() + doc.getId(); String hitId = getRepoForIndex(hit.getIndex()) + hit.getId(); if (docId.equals(hitId)) { // Add highlight if it exists Map<String, HighlightField> esHighlights = hit.getHighlightFields(); if (!esHighlights.isEmpty()) { Map<String, List<String>> fields = new HashMap<>(); for (Map.Entry<String, HighlightField> entry : esHighlights.entrySet()) { String field = entry.getKey(); List<String> list = new ArrayList<>(); for (Text fragment : entry.getValue().getFragments()) { list.add(fragment.toString()); } fields.put(field, list); } doc.putContextData(PageProvider.HIGHLIGHT_CTX_DATA, (Serializable) fields); } break; } } } }
/** * 条件查询高亮实现,使用指定的标识符号对返回结果进行高亮 * @throws Exception */ @Test public void searchHighlight()throws Exception{ SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo"); HighlightBuilder highlightBuilder=new HighlightBuilder(); highlightBuilder.preTags("<h2>"); highlightBuilder.postTags("</h2>"); highlightBuilder.field("title"); SearchResponse sr=srb.setQuery(QueryBuilders.matchQuery("title", "战")) .highlighter(highlightBuilder) .setFetchSource(new String[]{"title","price"}, null) .execute() .actionGet(); // 分页排序所有 SearchHits hits=sr.getHits(); for(SearchHit hit:hits){ System.out.println(hit.getSourceAsString()); System.out.println(hit.getHighlightFields()); } }