Skip to content

Spring Data Elasticsearch mapping id named field inside _source

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 {

    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 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 = “”) 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?



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.

2 People found this is helpful