diff --git a/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java b/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java index e3e9bf794..10cf875cc 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java @@ -830,13 +830,21 @@ public class MappingElasticsearchConverter @Nullable public Object get(ElasticsearchPersistentProperty property) { + String fieldName = property.getFieldName(); + if (target instanceof Document) { // nested objects may have properties like 'id' which are recognized as isIdProperty() but they are not // Documents Document document = (Document) target; if (property.isIdProperty() && document.hasId()) { - return document.getId(); + Object id = null; + + // take the id property from the document source if available + if (!fieldName.contains(".")) { + id = target.get(fieldName); + } + return id != null ? id : document.getId(); } if (property.isVersionProperty() && document.hasVersion()) { @@ -849,8 +857,6 @@ public class MappingElasticsearchConverter return ((SearchDocument) target).getScore(); } - String fieldName = property.getFieldName(); - if (!fieldName.contains(".")) { return target.get(fieldName); }