8.8 SpringBoot集成ElasticSearch之指定返回字段

2023-11-05

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

@Data
public class EmployeeCondition extends SampleEmployeeCondition implements RoutingProvider, ScoreFunctionProvider, SourceProvider {
    private static final String[] INCLUDE_FIELDS = {"employeeId", "name","age","job","salary","hobby"};
    private static final String[] EXCLUDE_FIELDS = {"birthday","profile","relative"};
    /**
     * 获取包含的字段列表
     *
     * @return 字段列表
     */
    @Override
    public String[] getIncludeFields() {
        return INCLUDE_FIELDS;
    }

    /**
     * 获取排除的字段列表
     *
     * @return 字段列表
     */
    @Override
    public String[] getExcludeFields() {
        return EXCLUDE_FIELDS;
    }
}

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

[
    {
        "id": null,
        "employeeId": "10000002",
        "name": "Stephen Curry",
        "age": 27,
        "birthday": null,
        "job": "Java engineer",
        "salary": 20000.0,
        "hobby": [
            "tennis",
            "football"
        ],
        "profile": null,
        "relative": null
    },
    {
        "id": null,
        "employeeId": "10000001",
        "name": "James Harden",
        "age": 31,
        "birthday": null,
        "job": "Java engineer",
        "salary": 30000.0,
        "hobby": [
            "swimming",
            "running",
            "basketball",
            "football"
        ],
        "profile": null,
        "relative": null
    }
]

2.实体类实现方式
(1).entity开发
在项目目录“/src/main/java/com/example/es”的EmployeeEntity类中注释掉不需要展示的字段,具体代码如下。

@Data
public class EmployeeEntity implements IdProvider{
    @JsonProperty("id")
    private String id;

    @JsonProperty("employeeId")
    private String employeeId;

    @JsonProperty("name")
    private String name;

    @JsonProperty("age")
    private Integer age;

//    @JsonProperty("birthday")
//    private String birthday;

    @JsonProperty("job")
    private String job;

    @JsonProperty("salary")
    private Float salary;

    @JsonProperty("hobby")
    private List<String> hobby;

//    @JsonProperty("profile")
//    private Profile profile;

//    @JsonProperty("relative")
//    private List<Relative> relativeList;

    @Override
    public String getId() {
        return id;
    }
}

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

[
    {
        "id": "10000002",
        "employeeId": "10000002",
        "name": "Stephen Curry",
        "age": 27,
        "job": "Java engineer",
        "salary": 20000.0,
        "hobby": [
            "tennis",
            "football"
        ]
    },
    {
        "id": "10000001",
        "employeeId": "10000001",
        "name": "James Harden",
        "age": 31,
        "job": "Java engineer",
        "salary": 30000.0,
        "hobby": [
            "swimming",
            "running",
            "basketball",
            "football"
        ]
    }
]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

8.8 SpringBoot集成ElasticSearch之指定返回字段 的相关文章

随机推荐