org.elasticsearch.search.fetch.subphase.highlight.HighlightField Java Examples

The following examples show how to use org.elasticsearch.search.fetch.subphase.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: SearchBuilder.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
/**
 * 返回JSON列表数据
 */
private List<JSONObject> getList(SearchHits searchHits) {
    List<JSONObject> list = new ArrayList<>();
    if (searchHits != null) {
        searchHits.forEach(item -> {
            JSONObject jsonObject = JSON.parseObject(item.getSourceAsString());
            jsonObject.put("id", item.getId());

            Map<String, HighlightField> highlightFields = item.getHighlightFields();
            if (highlightFields != null) {
                populateHighLightedFields(jsonObject, highlightFields);
            }
            list.add(jsonObject);
        });
    }
    return list;
}
 
Example #2
Source File: HighlightResultHelper.java    From mogu_blog_v2 with Apache License 2.0 6 votes vote down vote up
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
    List<T> results = new ArrayList<>();
    for (SearchHit hit : response.getHits()) {
        if (hit != null) {
            T result = null;
            if (StringUtils.hasText(hit.getSourceAsString())) {
                result = JSONObject.parseObject(hit.getSourceAsString(), clazz);
            }
            // 高亮查询
            for (HighlightField field : hit.getHighlightFields().values()) {
                try {
                    PropertyUtils.setProperty(result, field.getName(), concat(field.fragments()));
                } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                    log.error("设置高亮字段异常:{}", e.getMessage(), e);
                }
            }
            results.add(result);
        }
    }
    return new AggregatedPageImpl<T>(results, pageable, response.getHits().getTotalHits(), response.getAggregations(), response.getScrollId());
}
 
Example #3
Source File: HighlightResultHelper.java    From mogu_blog_v2 with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T mapSearchHit(SearchHit searchHit, Class<T> clazz) {
    List<T> results = new ArrayList<>();
    for (HighlightField field : searchHit.getHighlightFields().values()) {
        T result = null;
        if (StringUtils.hasText(searchHit.getSourceAsString())) {
            result = JSONObject.parseObject(searchHit.getSourceAsString(), clazz);
        }
        try {
            PropertyUtils.setProperty(result, field.getName(), concat(field.fragments()));
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            log.error("设置高亮字段异常:{}", e.getMessage(), e);
        }
        results.add(result);
    }
    return null;
}
 
Example #4
Source File: HighlightResultMapper.java    From code with Apache License 2.0 6 votes vote down vote up
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse searchResponse, Class<T> clazz, Pageable pageable) {
    SearchHits hits = searchResponse.getHits();
    List<T> result = new ArrayList<>((int) hits.getTotalHits());
    for (SearchHit searchHit : hits) {
        // 高亮 K,V
        Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
        // 使用反射比较通用一些,也可以直接 new 对象,把值set进去
        try {
            T document = objectMapper.readValue(searchHit.getSourceAsString(), clazz);
            for (Map.Entry<String, HighlightField> entry : highlightFields.entrySet()) {
                Field field = clazz.getField(entry.getKey());
                field.setAccessible(true);
                field.set(document, entry.getValue().fragments()[0].toString());
            }
            result.add(document);
        } catch (IllegalAccessException | IOException | NoSuchFieldException e) {
            e.printStackTrace();
        }

    }
    return new AggregatedPageImpl<>(result, pageable, hits.getTotalHits());
}
 
Example #5
Source File: SearchBuilder.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
/**
 * 组装高亮字符
 * @param result 目标对象
 * @param highlightFields 高亮配置
 */
private <T> void populateHighLightedFields(T result, Map<String, HighlightField> highlightFields) {
    for (HighlightField field : highlightFields.values()) {
        try {
            String name = field.getName();
            if (!name.endsWith(".keyword")) {
                PropertyUtils.setProperty(result, field.getName(), concat(field.fragments()));
            }
        } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
            throw new ElasticsearchException("failed to set highlighted value for field: " + field.getName()
                    + " with value: " + Arrays.toString(field.getFragments()), e);
        }
    }
}
 
Example #6
Source File: ResultMapperExt.java    From roncoo-education with MIT License 5 votes vote down vote up
private <T> void populateHighLightedFields(T result, Map<String, HighlightField> highlightFields) {
	for (HighlightField field : highlightFields.values()) {
		try {
			setProperties(result, field.getName(), concat(field.fragments()));
		} catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
			throw new ElasticsearchException("failed to set highlighted value for field: " + field.getName() + " with value: " + Arrays.toString(field.getFragments()), e);
		}
	}
}
 
Example #7
Source File: ElasticsearchDocumentScore.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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()),
			(Text fragment) -> SearchFields.getSnippet(fragment.string()));
}
 
Example #8
Source File: URLTokenizerIntegrationTest.java    From elasticsearch-analysis-url with Apache License 2.0 5 votes vote down vote up
@Test
public void testHighlight() throws Exception {
    final String field = "url_highlight_test";
    Map<String, String> docContent = new HashMap<>();
    final String url = "http://www.foo.bar.com:8080/baz/bat?bob=blah";
    docContent.put(field, url);
    client().prepareIndex(INDEX, TYPE).setSource(docContent).get();
    refresh(INDEX);

    SearchResponse response = client().prepareSearch(INDEX).setQuery(QueryBuilders.matchQuery(field, "www.foo.bar.com:8080"))
            .highlighter(new HighlightBuilder().preTags("<b>").postTags("</b>").field("*").forceSource(true))
            .get();

    SearchHit[] hits = response.getHits().getHits();
    assertThat(hits.length, equalTo(1));

    SearchHit hit = hits[0];
    Map<String, Object> source = hit.getSource();
    assertThat(source.size(), equalTo(1));
    assertThat(source, hasKey(field));
    assertThat("URL was stored correctly", source.get(field), equalTo(url));
    assertThat(hit.highlightFields(), hasKey(field));
    HighlightField highlightField = hit.highlightFields().get(field);
    Text[] fragments = highlightField.getFragments();
    assertThat(fragments.length, equalTo(1));
    Text fragment = fragments[0];
    assertThat("URL was highlighted correctly", fragment.string(), equalTo("http://<b>www.foo.bar.com</b>:<b>8080</b>/baz/bat?bob=blah"));
}
 
Example #9
Source File: QueryTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void highlightPreTagsAndPostTags() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
    String query = String.format("select /*! HIGHLIGHT(phrase,pre_tags : ['<b>'], post_tags : ['</b>']  ) */ " +
            "* from %s/phrase " +
            "where phrase like 'fox' " +
            "order by _score", TEST_INDEX_PHRASE);
    SearchHits hits = query(query);
    for (SearchHit hit : hits){
        HighlightField phrase = hit.getHighlightFields().get("phrase");
        String highlightPhrase = phrase.getFragments()[0].string();
        Assert.assertTrue(highlightPhrase.contains("<b>fox</b>"));
    }

}
 
Example #10
Source File: SearchService.java    From jakduk-api with MIT License 5 votes vote down vote up
private Map<String, List<String>> getHighlight(Set<Map.Entry<String, HighlightField>> entrySet) {
	Map<String, List<String>> highlight = new HashMap<>();

	for (Map.Entry<String, HighlightField> highlightField : entrySet) {
		List<String> fragments = new ArrayList<>();
		for (Text text : highlightField.getValue().fragments()) {
			fragments.add(text.string());
		}
		highlight.put(highlightField.getKey(), fragments);
	}

	return highlight;
}
 
Example #11
Source File: SearchServiceInternalImpl.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Maps the information from Elasticsearch for a single {@link SearchResultItem}
 * @param source the fields returned by Elasticsearch
 * @param highlights the highlights returned by Elasticsearch
 * @return the search item object
 */
//TODO: Implement previewUrl for supported types
protected SearchResultItem processSearchHit(Map<String, Object> source, Map<String, HighlightField> highlights) {
    SearchResultItem item = new SearchResultItem();
    item.setPath((String) source.get(pathFieldName));
    item.setName((String) source.get(internalNameFieldName));
    item.setLastModified(Instant.parse((String) source.get(lastEditFieldName)));
    item.setLastModifier(source.get(lastEditorFieldName).toString());
    item.setSize(Long.parseLong(source.get(sizeFieldName).toString()));
    item.setType(getItemType(source));
    item.setMimeType(getMimeType(source));
    item.setSnippets(getItemSnippets(highlights));
    return item;
}
 
Example #12
Source File: SearchServiceInternalImpl.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Maps the Elasticsearch highlighting to simple text snippets
 * @param highlights the highlighting to map
 * @return the list of snippets
 */
protected List<String> getItemSnippets(Map<String, HighlightField> highlights) {
    if(MapUtils.isNotEmpty(highlights)) {
        List<String> snippets = new LinkedList<>();
        highlights.values().forEach(highlight -> {
            for(Text text : highlight.getFragments()) {
                snippets.add(text.string());
            }
        });
        return snippets;
    }
    return null;
}
 
Example #13
Source File: ElasticsearchServiceImpl.java    From MyCommunity with Apache License 2.0 4 votes vote down vote up
public Page<DiscussPost> searchDiscussPost(String keyword, int current, int limit) {
    SearchQuery searchQuery = new NativeSearchQueryBuilder()
            .withQuery(QueryBuilders.multiMatchQuery(keyword, "title", "content"))
            .withSort(SortBuilders.fieldSort("type").order(SortOrder.DESC))
            .withSort(SortBuilders.fieldSort("score").order(SortOrder.DESC))
            .withSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC))
            .withPageable(PageRequest.of(current, limit))
            .withHighlightFields(
                    new HighlightBuilder.Field("title").preTags("<em>").postTags("</em>"),
                    new HighlightBuilder.Field("content").preTags("<em>").postTags("</em>")
            ).build();

    return elasticTemplate.queryForPage(searchQuery, DiscussPost.class, new SearchResultMapper() {
        @Override
        public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> aClass, Pageable pageable) {
            SearchHits hits = response.getHits();
            if (hits.getTotalHits() <= 0) {
                return null;
            }

            List<DiscussPost> list = new ArrayList<>();
            for (SearchHit hit : hits) {
                DiscussPost post = new DiscussPost();

                String id = hit.getSourceAsMap().get("id").toString();
                post.setId(Integer.valueOf(id));

                String userId = hit.getSourceAsMap().get("userId").toString();
                post.setUserId(Integer.valueOf(userId));

                String title = hit.getSourceAsMap().get("title").toString();
                post.setTitle(title);

                String content = hit.getSourceAsMap().get("content").toString();
                post.setContent(content);

                String status = hit.getSourceAsMap().get("status").toString();
                post.setStatus(Integer.valueOf(status));

                String createTime = hit.getSourceAsMap().get("createTime").toString();
                post.setCreateTime(new Date(Long.valueOf(createTime)));

                String commentCount = hit.getSourceAsMap().get("commentCount").toString();
                post.setCommentCount(Integer.valueOf(commentCount));

                // 处理高亮显示的结果
                HighlightField titleField = hit.getHighlightFields().get("title");
                if (titleField != null) {
                    post.setTitle(titleField.getFragments()[0].toString());
                }

                HighlightField contentField = hit.getHighlightFields().get("content");
                if (contentField != null) {
                    post.setContent(contentField.getFragments()[0].toString());
                }

                list.add(post);
            }

            return new AggregatedPageImpl(list, pageable,
                    hits.getTotalHits(), response.getAggregations(), response.getScrollId());
        }
    });
}
 
Example #14
Source File: PostServiceImpl.java    From SENS with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Page<Post> findPostsByEs(Map<String, Object> criteria, Page<Post> page) {

    //search request
    SearchRequest searchRequest = new SearchRequest("blog");

    //search builder
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    BoolQueryBuilder booleanQueryBuilder = QueryBuilders.boolQuery();
    for (String key : criteria.keySet()) {
        booleanQueryBuilder.must(QueryBuilders.matchQuery(key, criteria.get(key)));
    }

    sourceBuilder.query(booleanQueryBuilder);
    sourceBuilder.from(Integer.parseInt(String.valueOf((page.getCurrent() - 1) * page.getSize())));
    sourceBuilder.size(Integer.parseInt(String.valueOf(page.getSize())));
    sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

    //sort
    sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));

    //highlight
    HighlightBuilder highlightBuilder = new HighlightBuilder();
    HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("postTitle");
    highlightTitle.preTags("<span class=\"highlight\">");
    highlightTitle.postTags("</span>");
    highlightBuilder.field(highlightTitle);
    sourceBuilder.highlighter(highlightBuilder);

    // add builder into request
    searchRequest.indices("blog");
    searchRequest.source(sourceBuilder);

    //response
    SearchResponse searchResponse = null;
    List<Post> postList = new ArrayList<>();
    try {
        searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        //search hits
        SearchHits hits = searchResponse.getHits();
        long totalHits = hits.getTotalHits();

        SearchHit[] searchHits = hits.getHits();
        page.setTotal((int) totalHits);
        for (SearchHit hit : searchHits) {
            String str = hit.getSourceAsString();
            Post esPost = JSONObject.parseObject(str, Post.class);
            //高亮
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField highlight = highlightFields.get("postTitle");
            if (highlight != null) {
                Text[] fragments = highlight.fragments();
                String fragmentString = fragments[0].string();
                esPost.setPostTitle(fragmentString);
            }
            postList.add(esPost);
        }
    } catch (Exception e) {
        e.printStackTrace();
        log.error("ES未开启: {}", e);
    }
    return page.setRecords(postList);
}