gpt4 book ai didi

Java elasticsearch spring-data 设置不同的索引字段名称

转载 作者:行者123 更新时间:2023-11-30 03:06:35 25 4
gpt4 key购买 nike

我正在将 elasticsearch java-api 与 spring-data 结合使用,并且在索引文档时遇到问题。我想要一个不同的索引字段名称。这意味着与 java 代码中的不一样:

域对象:

@Document(indexName = "testindex", type = "message")
public class MessageObject {

@Id
private String unid;

@FieldNameInElasticIndex(value = "javaMessage") // I want anything like that
private String message;

private String secondMessage;

private String thirdMessage;

...

getters & setters

...

}

界面:

public interface MessageObjectRepository extends ElasticsearchRepository<MessageObject, Long> {


}

服务:

@Component
public class MessageService {

@Autowired
private MessageObjectRepository repository;


public void addRegistrationObject(MessageObject msg) {

repository.save(msg);
}
}

所以....

是否可以更改名称,使索引看起来像:

"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "testindex",
"_type": "message",
"_id": "00113B325ED357B7C1257E2D001D5B4B",
"_score": 1,
"_source": {
"unid": "00113B325ED357B7C1257E2D001D5B4B",
"javaMessage": "Hello", // <--- this is what I want (javaMessage)
"secondMessage": null,
"thirdMessage": "Third",

而不是

"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "testindex",
"_type": "message",
"_id": "00113B325ED357B7C1257E2D001D5B4B",
"_score": 1,
"_source": {
"unid": "00113B325ED357B7C1257E2D001D5B4B",
"message": "Hello", // <--- this is NOT what I want (java name: message)
"secondMessage": null,
"thirdMessage": "Third",

最佳答案

这个想法只是在字段上使用@JsonProperty注释并给出您想要在JSON序列化过程中使用的名称:

@JsonProperty("javaMessage")
private String message;

关于Java elasticsearch spring-data 设置不同的索引字段名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34617168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com