Search code examples
spring-data-elasticsearch

MappingException(Attempt to add id) after upgrading to spring boot 2.2.X


After upgrading the spring boot to 2.2.4 (from 2.1.x), org.springframework.cloud:spring-cloud-dependencies to Hoxton.RELEASE and org.springframework.cloud:spring-cloud-stream-dependencies to Horsham.RELEASE.

Getting the following exception when trying to create index document.

Caused by: org.springframework.data.mapping.MappingException: Attempt to add id property private java.util.Map .CatalogIndex.document but already have property private java.lang.String .CatalogIndex.id registered as id. Check your mapping configuration!

Please find the entity class hierarchy. I have removed all the getter and setter for simplicity.


package mypackage.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Parent;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class CatalogIndex {

    private static final long serialVersionUID = 1L;

    @Id
    private String id;

    @Parent(type = "Initiative")
    private String initiativeId;

    private List<Map<String, Object>> typeHierarchy;

    private Map<String, Object> document;

    private List<Map<String, Object>> filters;
}

package mypackage.entity;

import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "cataloginitiative")
public class CatalogInitiativeIndex extends CatalogIndex {   }


Solution

  • Spring Data Elasticsearch, when inspecting the Entity class, tries to figure out which property of the class is to be used as the Id property. A property qualifies for this if one of the following is true:

    • the property is annotated with @Id
    • the property is named id
    • the property is named document

    So in your case you have the property id which has a matching name and the annotation, and the property document with a matching name.

    You have to rename your property document to somthing different.