Spring Data Elasticsearch mapping id named field inside _source

huangapple go评论71阅读模式
英文:

Spring Data Elasticsearch mapping id named field inside _source

问题

我正在尝试使用Spring Data Elastic4.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.

这个问题有解决方法吗
英文:

I'm trying to map documents from ElasticSearch into java objects using Spring Data Elastic (4.0.2). My problem is the following: I have two id fields one is the _id for the document itself, and one id inside _source.

@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
    }
  }

If I put @Id on one and @Field(name = "id") on the other as shown in the example above, I get an exception which states that I can't have two Id fields:

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!

I was also trying with @Field(name = "_source.id") but it was not working neither:

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: null is not a Map.

Is there a solution for this problem?

答案1

得分: 0

不能使用 messageId 用于这个字段吗?问题在于在 Spring Data Elasticsearch 中,如果满足以下条件之一,属性会被视为实体的 id 属性:

  • 使用 @Id 进行注解
  • 属性命名为 id
  • 属性命名为 document

我们将在 4.1 版本中弃用使用属性名称的行为,并在 4.2 版本中最早将其移除。

因此,目前对于这个属性,不使用 id 将是前进的方式。

英文:

Can't you use messageId for this field? The problem is that in Spring Data Elasticsearch, a property is considered to the id property of the entity if one of the following is true:

  • it is annotated with @Id
  • it is named id
  • it is named document

We will deprecate the behaviour of using the name of the property in 4.1 and remove it earliest in 4.2.

So currently not using id for this property would be the way to go.

huangapple
  • 本文由 发表于 2020年7月28日 22:02:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63135943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定