diff --git a/src/main/asciidoc/reference/elasticsearch-object-mapping.adoc b/src/main/asciidoc/reference/elasticsearch-object-mapping.adoc index bc119f108..95babfbd2 100644 --- a/src/main/asciidoc/reference/elasticsearch-object-mapping.adoc +++ b/src/main/asciidoc/reference/elasticsearch-object-mapping.adoc @@ -29,7 +29,7 @@ See <> * `@Id`: Applied at the field level to mark the field used for identity purpose. -* `@Transient`: By default all fields are mapped to the document when it is stored or retrieved, this annotation excludes the field. +* `@Transient`, `@ReadOnlyProperty`, `@WriteOnlyProperty`: see the following section <> for detailed information. * `@PersistenceConstructor`: Marks a given constructor - even a package protected one - to use when instantiating the object from the database. Constructor arguments are mapped by name to the key values in the retrieved Document. * `@Field`: Applied at the field level and defines properties of the field, most of the attributes map to the respective https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html[Elasticsearch Mapping] definitions (the following list is not complete, check the annotation Javadoc for a complete reference): @@ -49,6 +49,25 @@ In difference to a registered Spring `Converter` this only converts the annotate The mapping metadata infrastructure is defined in a separate spring-data-commons project that is technology agnostic. +[[elasticsearch.mapping.meta-model.annotations.read-write]] +==== Controlling which properties are written to and read from Elasticsearch + +This section details the annotations that define if the value of a property is written to or +read from Elasticsearch. + +`@Transient`: A property annotated with this annotation will not be written to the mapping, it's value will not be +sent to Elasticsearch and when documents are returned from Elasticsearch, this property will not be set in the +resulting entity. + +`@ReadOnlyProperty`: A property with this annotaiton will not have its value written to Elasticsearch, but when +returning data, the proeprty will be filled with the value returned in the document from Elasticsearch. One use case +for this are runtime fields defined in the index mapping. + +`@WriteOnlyProperty`: A property with this annotaiton will have its value stored in Elasticsearch but will not be set +with any value when reading document. This can be used for example for synthesized fields which should go into the +Elasticsearch index but are not used elsewhere. + + [[elasticsearch.mapping.meta-model.annotations.date-formats]] ==== Date format mapping diff --git a/src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java b/src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java index db09c04df..22af9b92d 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java @@ -408,7 +408,7 @@ protected T updateIndexedObject(T entity, IndexedObjectInformation indexedOb ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty(); // Only deal with text because ES generated Ids are strings! - if (indexedObjectInformation.getId() != null && idProperty != null && idProperty.isWritable() + if (indexedObjectInformation.getId() != null && idProperty != null && idProperty.isReadable() && idProperty.getType().isAssignableFrom(String.class)) { propertyAccessor.setProperty(idProperty, indexedObjectInformation.getId()); } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/AbstractReactiveElasticsearchTemplate.java b/src/main/java/org/springframework/data/elasticsearch/core/AbstractReactiveElasticsearchTemplate.java index 24203eed9..fc7e7f8b9 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/AbstractReactiveElasticsearchTemplate.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/AbstractReactiveElasticsearchTemplate.java @@ -261,7 +261,7 @@ protected T updateIndexedObject(T entity, IndexedObjectInformation indexedOb ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty(); // Only deal with text because ES generated Ids are strings! - if (indexedObjectInformation.getId() != null && idProperty != null && idProperty.isWritable() + if (indexedObjectInformation.getId() != null && idProperty != null && idProperty.isReadable() && idProperty.getType().isAssignableFrom(String.class)) { propertyAccessor.setProperty(idProperty, indexedObjectInformation.getId()); } 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 fc4aefab4..e1ebd09b3 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 @@ -349,7 +349,7 @@ private R readEntity(ElasticsearchPersistentEntity entity, Map propertyAccessor = new ConvertingPropertyAccessor<>( targetEntity.getPropertyAccessor(result), conversionService); // Only deal with String because ES generated Ids are strings ! - if (idProperty != null && idProperty.isWritable() && idProperty.getType().isAssignableFrom(String.class)) { + if (idProperty != null && idProperty.isReadable() && idProperty.getType().isAssignableFrom(String.class)) { propertyAccessor.setProperty(idProperty, document.getId()); } } @@ -411,7 +411,7 @@ protected R readProperties(ElasticsearchPersistentEntity entity, R instan for (ElasticsearchPersistentProperty prop : entity) { - if (entity.isCreatorArgument(prop) || !prop.isReadable() || !prop.isWritable()) { + if (entity.isCreatorArgument(prop) || !prop.isReadable()) { continue; } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentProperty.java b/src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentProperty.java index 4afa06be2..6ba751476 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentProperty.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentProperty.java @@ -61,7 +61,7 @@ public interface ElasticsearchPersistentProperty extends PersistentProperty