Skip to content

Commit ec77b3a

Browse files
authored
Add option to not write version to document source.
Original Pull Request #2487 Closes #2466
1 parent 63cebd7 commit ec77b3a

File tree

5 files changed

+86
-1
lines changed

5 files changed

+86
-1
lines changed

src/main/java/org/springframework/data/elasticsearch/annotations/Document.java

+8
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@
8585
*/
8686
boolean storeIdInSource() default true;
8787

88+
/**
89+
* Specifies if the version property should also be stored in the Elasticsearch document source. Default value is
90+
* true.
91+
*
92+
* @since 5.1
93+
*/
94+
boolean storeVersionInSource() default true;
95+
8896
/**
8997
* @since 4.3
9098
*/

src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,8 @@ private void writeProperties(ElasticsearchPersistentEntity<?> entity, Persistent
941941

942942
if (!property.isWritable() //
943943
|| property.isIndexedIndexNameProperty() //
944-
|| (property.isIdProperty() && !entity.storeIdInSource())) {
944+
|| (property.isIdProperty() && !entity.storeIdInSource()) //
945+
|| (property.isVersionProperty() && !entity.storeVersionInSource())) {
945946
continue;
946947
}
947948

src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentEntity.java

+6
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,10 @@ default ElasticsearchPersistentProperty getRequiredSeqNoPrimaryTermProperty() {
181181
* @since 5.1
182182
*/
183183
boolean storeIdInSource();
184+
185+
/**
186+
* @return the storeVersionInSource value from the document annotation.
187+
* @since 5.1
188+
*/
189+
boolean storeVersionInSource();
184190
}

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntity.java

+8
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
8484
private final Lazy<EvaluationContext> indexNameEvaluationContext = Lazy.of(this::getIndexNameEvaluationContext);
8585

8686
private final boolean storeIdInSource;
87+
private final boolean storeVersionInSource;
8788

8889
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation,
8990
ContextConfiguration contextConfiguration) {
@@ -108,9 +109,11 @@ public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation,
108109
this.createIndexAndMapping = document.createIndex();
109110
this.dynamic = document.dynamic();
110111
this.storeIdInSource = document.storeIdInSource();
112+
this.storeVersionInSource = document.storeVersionInSource();
111113
} else {
112114
this.dynamic = Dynamic.INHERIT;
113115
this.storeIdInSource = true;
116+
this.storeVersionInSource = true;
114117
}
115118
Routing routingAnnotation = AnnotatedElementUtils.findMergedAnnotation(clazz, Routing.class);
116119

@@ -200,6 +203,11 @@ public boolean storeIdInSource() {
200203
return storeIdInSource;
201204
}
202205

206+
@Override
207+
public boolean storeVersionInSource() {
208+
return storeVersionInSource;
209+
}
210+
203211
@Override
204212
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
205213
super.addPersistentProperty(property);

src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java

+62
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.springframework.data.annotation.ReadOnlyProperty;
5151
import org.springframework.data.annotation.Transient;
5252
import org.springframework.data.annotation.TypeAlias;
53+
import org.springframework.data.annotation.Version;
5354
import org.springframework.data.convert.ReadingConverter;
5455
import org.springframework.data.convert.WritingConverter;
5556
import org.springframework.data.domain.Range;
@@ -1893,6 +1894,30 @@ void shouldNotWriteIdPropertyToDocumentSourceIfConfiguredSo() throws JSONExcepti
18931894
assertEquals(expected, json, true);
18941895
}
18951896

1897+
@Test // #2364
1898+
@DisplayName("should not write version property to document source if configured so")
1899+
void shouldNotWriteVersionPropertyToDocumentSourceIfConfiguredSo() throws JSONException {
1900+
1901+
@Language("JSON")
1902+
var expected = """
1903+
{
1904+
"_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$DontWriteVersionToSourceEntity",
1905+
"id": "42",
1906+
"text": "some text"
1907+
}
1908+
""";
1909+
var entity = new DontWriteVersionToSourceEntity();
1910+
entity.setId("42");
1911+
entity.setVersion(7L);
1912+
entity.setText("some text");
1913+
1914+
Document document = Document.create();
1915+
mappingElasticsearchConverter.write(entity, document);
1916+
String json = document.toJson();
1917+
1918+
assertEquals(expected, json, true);
1919+
}
1920+
18961921
@Test // #2290
18971922
@DisplayName("should respect field setting for empty properties")
18981923
void shouldRespectFieldSettingForEmptyProperties() throws JSONException {
@@ -3004,6 +3029,43 @@ public void setText(@Nullable String text) {
30043029
}
30053030
}
30063031

3032+
@org.springframework.data.elasticsearch.annotations.Document(indexName = "doesnt-matter",
3033+
storeVersionInSource = false)
3034+
static class DontWriteVersionToSourceEntity {
3035+
@Nullable private String id;
3036+
@Version
3037+
@Nullable private Long version;
3038+
@Nullable
3039+
@Field(type = FieldType.Text) private String text;
3040+
3041+
@Nullable
3042+
public String getId() {
3043+
return id;
3044+
}
3045+
3046+
public void setId(@Nullable String id) {
3047+
this.id = id;
3048+
}
3049+
3050+
@Nullable
3051+
public Long getVersion() {
3052+
return version;
3053+
}
3054+
3055+
public void setVersion(@Nullable Long version) {
3056+
this.version = version;
3057+
}
3058+
3059+
@Nullable
3060+
public String getText() {
3061+
return text;
3062+
}
3063+
3064+
public void setText(@Nullable String text) {
3065+
this.text = text;
3066+
}
3067+
}
3068+
30073069
static class EntityWithPropertiesThatMightBeEmpty {
30083070
@Nullable private String id;
30093071

0 commit comments

Comments
 (0)