栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Elasticsearch7 elasticsearchRestTemplate.search 高亮

Elasticsearch7 elasticsearchRestTemplate.search 高亮

@Override
    public Page search(String keyword, Integer pageNum, Integer pageSize) {

        Pageable pageable = PageRequest.of(pageNum, pageSize);

        //设置需要高亮的字段
        HighlightBuilder.Field questionContentField = new HighlightBuilder.Field("questionContent")
                .preTags("")
                .postTags("");
        HighlightBuilder.Field optionField = new HighlightBuilder.Field("options")
                .preTags("")
                .postTags("");

        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(
            boolQuery()
                .should(boolQuery()
                        .must(queryStringQuery(keyword).defaultField("questionContent").boost(10)))
                .should(boolQuery()
                        .must(queryStringQuery(keyword).defaultField("options")))
        );

        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(functionScoreQueryBuilder)
            .withPageable(PageRequest.of(pageNum, pageSize)).withSort(SortBuilders.scoreSort())
                .withHighlightFields(questionContentField, optionField).build();

        LOGGER.info("DSL:{}", searchQuery.getQuery().toString());
        SearchHits searchHits = elasticsearchRestTemplate.search(searchQuery, EsAnswer.class);

        long totalHits = searchHits.getTotalHits();
        if(totalHits <=0){
            return new PageImpl<>(new ArrayList(),pageable,0);
        }

        List searchAnswerList = new ArrayList();
        for (SearchHit searchHit : searchHits) { // 获取搜索到的数据
            EsAnswer content = searchHit.getContent();
            // 处理高亮
            Map> highlightFields = searchHit.getHighlightFields();
            for (Map.Entry> stringHighlightFieldEntry : highlightFields.entrySet()) {
                String key = stringHighlightFieldEntry.getKey();
                if (StringUtils.equals(key, "questionContent")) {
                    List fragments = stringHighlightFieldEntry.getValue();
                    StringBuilder sb = new StringBuilder();
                    for (String fragment : fragments) {
                        sb.append(fragment);
                    }
                    content.setQuestionContent(sb.toString());
                }
                if (StringUtils.equals(key, "options")) {
                    List fragments = stringHighlightFieldEntry.getValue();
                    StringBuilder sb = new StringBuilder();
                    for (String fragment : fragments) {
                        sb.append(fragment);
                    }
                    content.setOptions(sb.toString());
                }
            }
            searchAnswerList.add(content);
        }

        return new PageImpl<>(searchAnswerList,pageable, totalHits);

    }

同时对questionContent和options字段搜索,不过questionContent的boost为10.

使用new HighlightBuilder.Field构建2个高亮字段,并使用.withHighlightFields(questionContentField, optionField)将其绑定到builder,获取搜索结果elasticsearchRestTemplate.search(),对结果进行高亮处理,其实就是替换文本。


思路来源于:
Elasticsearch基本操作_weixin_44131993的博客-CSDN博客

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/730810.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号