Skip to content

Add option to not write version to document source. #2487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
private final Lazy<EvaluationContext> indexNameEvaluationContext = Lazy.of(this::getIndexNameEvaluationContext);

private final boolean storeIdInSource;
private final boolean storeVersionInSource;

public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation,
ContextConfiguration contextConfiguration) {
Expand All @@ -108,9 +109,11 @@ public SimpleElasticsearchPersistentEntity(TypeInformation<T> 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);

Expand Down Expand Up @@ -200,6 +203,11 @@ public boolean storeIdInSource() {
return storeIdInSource;
}

@Override
public boolean storeVersionInSource() {
return storeVersionInSource;
}

@Override
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
super.addPersistentProperty(property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;

Expand Down