springboot 使用 Spring Data Elasticsearch 高亮显示

说明

可以像百度那样,高亮显示匹配的关键字,如下图:

代码

package com.std.ctrl;


import com.std.es.entity.Toutiao;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/hightlight")
public class HighlightCtrl {

    @Autowired
    private ElasticsearchRestTemplate template;

    /**
     * 高亮显示
     */
    @RequestMapping("/test")
    public void test() {
        //定义高亮字段
        HighlightBuilder.Field highlightField = new HighlightBuilder.Field("title");

        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
                //查询条件
                .withQuery(QueryBuilders.matchPhraseQuery("title","java"))
                //分页
                .withPageable(PageRequest.of(0, 5))
                //排序
                .withSort(SortBuilders.fieldSort("like").order(SortOrder.DESC))
                //高亮字段显示
                .withHighlightFields(highlightField)
                .build();
        SearchHits<Toutiao> shs = template.search(nativeSearchQuery, Toutiao.class);
        List<SearchHit<Toutiao>> list=shs.getSearchHits();
        for(SearchHit<Toutiao> item:list){
            //打印高亮字段
            List<String> highlightlist=item.getHighlightField("title");
            for(String highlight:highlightlist){
                System.out.println(highlight);
            }

            Toutiao toutiao=item.getContent();
            System.out.println(toutiao);
            System.out.println("--------");
        }
    }

    /**
     * 高亮显示,且定义标签
     */
    @RequestMapping("/test2")
    public void test2() {
        //定义高亮字段
        HighlightBuilder.Field highlightField = new HighlightBuilder.Field("title");
        //设置高亮标签
        highlightField.preTags("<b>").postTags("</b>");

        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
                //查询条件
                .withQuery(QueryBuilders.matchPhraseQuery("title","java"))
                //分页
                .withPageable(PageRequest.of(0, 5))
                //排序
                .withSort(SortBuilders.fieldSort("like").order(SortOrder.DESC))
                //高亮字段显示
                .withHighlightFields(highlightField)
                .build();
        SearchHits<Toutiao> shs = template.search(nativeSearchQuery, Toutiao.class);
        List<SearchHit<Toutiao>> list=shs.getSearchHits();
        for(SearchHit<Toutiao> item:list){
            //打印高亮字段
            List<String> highlightlist=item.getHighlightField("title");
            for(String highlight:highlightlist){
                System.out.println(highlight);
            }

            Toutiao toutiao=item.getContent();
            System.out.println(toutiao);
            System.out.println("--------");
        }
    }

    /**
     * 多个查询条件,可以设置多个高亮字段
     */
    @RequestMapping("/test3")
    public void test3() {
        //定义高亮字段
        HighlightBuilder.Field titleField = new HighlightBuilder.Field("title");
        //设置高亮标签
//        titleField.preTags("<b>").postTags("</b>");

        HighlightBuilder.Field contentField = new HighlightBuilder.Field("content");

        MatchPhraseQueryBuilder titleQuery = QueryBuilders.matchPhraseQuery("title", "java");
        MatchPhraseQueryBuilder contentQuery = QueryBuilders.matchPhraseQuery("content", "java");

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .must(titleQuery)
                .must(contentQuery);

        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
                //查询条件
                .withQuery(boolQueryBuilder)
                //分页
                .withPageable(PageRequest.of(0, 5))
                //排序
                .withSort(SortBuilders.fieldSort("like").order(SortOrder.DESC))
                //高亮字段显示
                .withHighlightFields(titleField,contentField)
                .build();

        SearchHits<Toutiao> shs = template.search(nativeSearchQuery, Toutiao.class);
        List<SearchHit<Toutiao>> list=shs.getSearchHits();
        for(SearchHit<Toutiao> item:list){
            //打印高亮字段
            List<String> titleList=item.getHighlightField("title");
            for(String highlight:titleList){
                System.out.println(highlight);
            }

            List<String> contentList=item.getHighlightField("content");
            for(String highlight:contentList){
                System.out.println(highlight);
            }

            Toutiao toutiao=item.getContent();
            System.out.println(toutiao);
            System.out.println("--------");
        }
    }
}

原文出处:https://malaoshi.top/show_1IX32awQIzTZ.html