Spring _source 中的数据 Elasticsearch 映射 id 命名字段
Spring Data Elasticsearch mapping id named field inside _source
我正在尝试使用 Spring Data Elastic (4.0.2) 将文档从 ElasticSearch 映射到 java 对象。我的问题如下:我有两个 id 字段,一个是文档本身的 _id,另一个是 _source 中的 id。
@Document(indexName = "logger-logs-*", createIndex = false)
public class LogMessage {
@Id
private String _id;
@Field(name = "id")
private int messageId;
}
{
"_index" : "logger-logs-2020-03-01",
"_type" : "logger-logs",
"_id" : "xyz8iUCJdBd2Vs=",
"_score" : 1.0,
"_source" : {
"timestamp" : 1583103045441,
"level" : "info",
"levelNumber" : 3,
"id" : 10891
}
}
如果我将 @Id 放在一个上,将 @Field(name = "id") 放在另一个上,如上例所示,我会得到一个异常,指出我不能有两个 Id 字段:
nested exception is org.springframework.data.mapping.MappingException: Attempt to add property private int messageId but already have property private java.lang.String _id registered as id. Check your mapping configuration!
我也尝试使用 @Field(name = "_source.id") 但它也不起作用:
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: null is not a Map.
这个问题有解决办法吗?
这个字段不能使用messageId吗?问题在于,在 Spring Data Elasticsearch 中,如果满足以下任一条件,则将 属性 视为实体的 ID 属性:
- 注释为
@Id
- 它被命名为id
- 它被命名为文档
我们将在 4.1 中弃用使用 属性 名称的行为,并最早在 4.2 中将其删除。
所以目前不使用 id 属性 是可行的方法。
我正在尝试使用 Spring Data Elastic (4.0.2) 将文档从 ElasticSearch 映射到 java 对象。我的问题如下:我有两个 id 字段,一个是文档本身的 _id,另一个是 _source 中的 id。
@Document(indexName = "logger-logs-*", createIndex = false)
public class LogMessage {
@Id
private String _id;
@Field(name = "id")
private int messageId;
}
{
"_index" : "logger-logs-2020-03-01",
"_type" : "logger-logs",
"_id" : "xyz8iUCJdBd2Vs=",
"_score" : 1.0,
"_source" : {
"timestamp" : 1583103045441,
"level" : "info",
"levelNumber" : 3,
"id" : 10891
}
}
如果我将 @Id 放在一个上,将 @Field(name = "id") 放在另一个上,如上例所示,我会得到一个异常,指出我不能有两个 Id 字段:
nested exception is org.springframework.data.mapping.MappingException: Attempt to add property private int messageId but already have property private java.lang.String _id registered as id. Check your mapping configuration!
我也尝试使用 @Field(name = "_source.id") 但它也不起作用:
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: null is not a Map.
这个问题有解决办法吗?
这个字段不能使用messageId吗?问题在于,在 Spring Data Elasticsearch 中,如果满足以下任一条件,则将 属性 视为实体的 ID 属性:
- 注释为
@Id
- 它被命名为id
- 它被命名为文档
我们将在 4.1 中弃用使用 属性 名称的行为,并最早在 4.2 中将其删除。
所以目前不使用 id 属性 是可行的方法。