From c3b1750ea65042c6f1c1395eb6082a6169c98621 Mon Sep 17 00:00:00 2001 From: Peter-Josef Meisch Date: Sun, 5 Mar 2023 09:52:38 +0100 Subject: [PATCH] Add option to not write version to document source. Closes #2466 --- .../elasticsearch/annotations/Document.java | 8 +++ .../MappingElasticsearchConverter.java | 3 +- .../ElasticsearchPersistentEntity.java | 6 ++ .../SimpleElasticsearchPersistentEntity.java | 8 +++ ...appingElasticsearchConverterUnitTests.java | 62 +++++++++++++++++++ 5 files changed, 86 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/elasticsearch/annotations/Document.java b/src/main/java/org/springframework/data/elasticsearch/annotations/Document.java index f64335842..0d7855c01 100644 --- a/src/main/java/org/springframework/data/elasticsearch/annotations/Document.java +++ b/src/main/java/org/springframework/data/elasticsearch/annotations/Document.java @@ -85,6 +85,14 @@ */ boolean storeIdInSource() default true; + /** + * Specifies if the version property should also be stored in the Elasticsearch document source. Default value is + * true. + * + * @since 5.1 + */ + boolean storeVersionInSource() default true; + /** * @since 4.3 */ 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 0d7861366..e219ffaf2 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 @@ -941,7 +941,8 @@ private void writeProperties(ElasticsearchPersistentEntity entity, Persistent if (!property.isWritable() // || property.isIndexedIndexNameProperty() // - || (property.isIdProperty() && !entity.storeIdInSource())) { + || (property.isIdProperty() && !entity.storeIdInSource()) // + || (property.isVersionProperty() && !entity.storeVersionInSource())) { continue; } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentEntity.java b/src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentEntity.java index ae21d3a35..c8a03d8b0 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentEntity.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentEntity.java @@ -181,4 +181,10 @@ default ElasticsearchPersistentProperty getRequiredSeqNoPrimaryTermProperty() { * @since 5.1 */ boolean storeIdInSource(); + + /** + * @return the storeVersionInSource value from the document annotation. + * @since 5.1 + */ + boolean storeVersionInSource(); } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntity.java b/src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntity.java index 7b33291e5..396f379af 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntity.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntity.java @@ -84,6 +84,7 @@ public class SimpleElasticsearchPersistentEntity extends BasicPersistentEntit private final Lazy indexNameEvaluationContext = Lazy.of(this::getIndexNameEvaluationContext); private final boolean storeIdInSource; + private final boolean storeVersionInSource; public SimpleElasticsearchPersistentEntity(TypeInformation typeInformation, ContextConfiguration contextConfiguration) { @@ -108,9 +109,11 @@ public SimpleElasticsearchPersistentEntity(TypeInformation typeInformation, this.createIndexAndMapping = document.createIndex(); this.dynamic = document.dynamic(); this.storeIdInSource = document.storeIdInSource(); + this.storeVersionInSource = document.storeVersionInSource(); } else { this.dynamic = Dynamic.INHERIT; this.storeIdInSource = true; + this.storeVersionInSource = true; } Routing routingAnnotation = AnnotatedElementUtils.findMergedAnnotation(clazz, Routing.class); @@ -200,6 +203,11 @@ public boolean storeIdInSource() { return storeIdInSource; } + @Override + public boolean storeVersionInSource() { + return storeVersionInSource; + } + @Override public void addPersistentProperty(ElasticsearchPersistentProperty property) { super.addPersistentProperty(property); diff --git a/src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java b/src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java index 636243291..7a7323b0e 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java @@ -50,6 +50,7 @@ import org.springframework.data.annotation.ReadOnlyProperty; import org.springframework.data.annotation.Transient; import org.springframework.data.annotation.TypeAlias; +import org.springframework.data.annotation.Version; import org.springframework.data.convert.ReadingConverter; import org.springframework.data.convert.WritingConverter; import org.springframework.data.domain.Range; @@ -1893,6 +1894,30 @@ void shouldNotWriteIdPropertyToDocumentSourceIfConfiguredSo() throws JSONExcepti assertEquals(expected, json, true); } + @Test // #2364 + @DisplayName("should not write version property to document source if configured so") + void shouldNotWriteVersionPropertyToDocumentSourceIfConfiguredSo() throws JSONException { + + @Language("JSON") + var expected = """ + { + "_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$DontWriteVersionToSourceEntity", + "id": "42", + "text": "some text" + } + """; + var entity = new DontWriteVersionToSourceEntity(); + entity.setId("42"); + entity.setVersion(7L); + entity.setText("some text"); + + Document document = Document.create(); + mappingElasticsearchConverter.write(entity, document); + String json = document.toJson(); + + assertEquals(expected, json, true); + } + @Test // #2290 @DisplayName("should respect field setting for empty properties") void shouldRespectFieldSettingForEmptyProperties() throws JSONException { @@ -3004,6 +3029,43 @@ public void setText(@Nullable String text) { } } + @org.springframework.data.elasticsearch.annotations.Document(indexName = "doesnt-matter", + storeVersionInSource = false) + static class DontWriteVersionToSourceEntity { + @Nullable private String id; + @Version + @Nullable private Long version; + @Nullable + @Field(type = FieldType.Text) private String text; + + @Nullable + public String getId() { + return id; + } + + public void setId(@Nullable String id) { + this.id = id; + } + + @Nullable + public Long getVersion() { + return version; + } + + public void setVersion(@Nullable Long version) { + this.version = version; + } + + @Nullable + public String getText() { + return text; + } + + public void setText(@Nullable String text) { + this.text = text; + } + } + static class EntityWithPropertiesThatMightBeEmpty { @Nullable private String id;