其他分享
首页 > 其他分享> > 8.9 SpringBoot集成ElasticSearch之高亮

8.9 SpringBoot集成ElasticSearch之高亮

作者:互联网

1.condition开发
在项目目录“/src/main/java/com/example/es/condition”的EmployeeCondition类中实现SortProvider接口,具体代码如下。

@Data
public class EmployeeCondition extends SampleEmployeeCondition implements RoutingProvider, ScoreFunctionProvider,SortProvider, HighlighterProvider {
    @Override
    public HighlighterBuilder highlighterBuilder() {
        return Highlighter.highlighter().addFields(Highlighter.field("job"));
    }
}

2.entity开发
在项目目录“/src/main/java/com/example/es”的EmployeeEntity类中实现HighlightSource接口,具体代码如下。

@Data
public class EmployeeEntity implements IdProvider, HighlightSource {
    private Map<String, List<String>> highlightSource;

    /**
     * eBatis回调该方法传入高亮结果
     * @param highlightSource 高亮结果
     */
    @Override
    public void setHighlightSource(Map<String, List<String>> highlightSource) {
        this.highlightSource = highlightSource;
    }
}

3.测试
启动项目,然后在postman中请求“http://localhost:8080/employee/queryEmployeePage”,成功后返回对应的信息。

[
    {
        "highlightSource": {
            "job": [
                "<em>Java engineer</em>"
            ]
        },
        "id": "10000002",
        "employeeId": "10000002",
        "name": "Stephen Curry",
        "age": 27,
        "birthday": "1995-08-06",
        "job": "Java engineer",
        "salary": 20000.0,
        "hobby": [
            "tennis",
            "football"
        ],
        "profile": {
            "nickName": "cupid",
            "avatar": "https://www.avatar.com/10000002.png",
            "grade": "golden"
        },
        "relative": [
            {
                "employeeId": "10000001"
            },
            {
                "employeeId": "10000003"
            }
        ]
    },
    {
        "highlightSource": {
            "job": [
                "<em>Java engineer</em>"
            ]
        },
        "id": "10000001",
        "employeeId": "10000001",
        "name": "James Harden",
        "age": 31,
        "birthday": "1991-01-01",
        "job": "Java engineer",
        "salary": 30000.0,
        "hobby": [
            "swimming",
            "running",
            "basketball",
            "football"
        ],
        "profile": {
            "nickName": "squirrel",
            "avatar": "https://www.avatar.com/10000001.png",
            "grade": "diamonds"
        },
        "relative": [
            {
                "employeeId": "10000002"
            },
            {
                "employeeId": "10000004"
            }
        ]
    }
]

标签:Java,SpringBoot,8.9,highlightSource,job,ElasticSearch,avatar,public,employeeId
来源: https://blog.csdn.net/Jgx1214/article/details/122327220