mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-24 04:52:12 +00:00
Add option to not write version to document source.
Original Pull Request #2487 Closes #2466
This commit is contained in:
parent
63cebd7038
commit
ec77b3a082
@ -85,6 +85,14 @@ public @interface Document {
|
|||||||
*/
|
*/
|
||||||
boolean storeIdInSource() default true;
|
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
|
* @since 4.3
|
||||||
*/
|
*/
|
||||||
|
@ -941,7 +941,8 @@ public class MappingElasticsearchConverter
|
|||||||
|
|
||||||
if (!property.isWritable() //
|
if (!property.isWritable() //
|
||||||
|| property.isIndexedIndexNameProperty() //
|
|| property.isIndexedIndexNameProperty() //
|
||||||
|| (property.isIdProperty() && !entity.storeIdInSource())) {
|
|| (property.isIdProperty() && !entity.storeIdInSource()) //
|
||||||
|
|| (property.isVersionProperty() && !entity.storeVersionInSource())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,4 +181,10 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
|
|||||||
* @since 5.1
|
* @since 5.1
|
||||||
*/
|
*/
|
||||||
boolean storeIdInSource();
|
boolean storeIdInSource();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the storeVersionInSource value from the document annotation.
|
||||||
|
* @since 5.1
|
||||||
|
*/
|
||||||
|
boolean storeVersionInSource();
|
||||||
}
|
}
|
||||||
|
@ -84,6 +84,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
|||||||
private final Lazy<EvaluationContext> indexNameEvaluationContext = Lazy.of(this::getIndexNameEvaluationContext);
|
private final Lazy<EvaluationContext> indexNameEvaluationContext = Lazy.of(this::getIndexNameEvaluationContext);
|
||||||
|
|
||||||
private final boolean storeIdInSource;
|
private final boolean storeIdInSource;
|
||||||
|
private final boolean storeVersionInSource;
|
||||||
|
|
||||||
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation,
|
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation,
|
||||||
ContextConfiguration contextConfiguration) {
|
ContextConfiguration contextConfiguration) {
|
||||||
@ -108,9 +109,11 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
|||||||
this.createIndexAndMapping = document.createIndex();
|
this.createIndexAndMapping = document.createIndex();
|
||||||
this.dynamic = document.dynamic();
|
this.dynamic = document.dynamic();
|
||||||
this.storeIdInSource = document.storeIdInSource();
|
this.storeIdInSource = document.storeIdInSource();
|
||||||
|
this.storeVersionInSource = document.storeVersionInSource();
|
||||||
} else {
|
} else {
|
||||||
this.dynamic = Dynamic.INHERIT;
|
this.dynamic = Dynamic.INHERIT;
|
||||||
this.storeIdInSource = true;
|
this.storeIdInSource = true;
|
||||||
|
this.storeVersionInSource = true;
|
||||||
}
|
}
|
||||||
Routing routingAnnotation = AnnotatedElementUtils.findMergedAnnotation(clazz, Routing.class);
|
Routing routingAnnotation = AnnotatedElementUtils.findMergedAnnotation(clazz, Routing.class);
|
||||||
|
|
||||||
@ -200,6 +203,11 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
|||||||
return storeIdInSource;
|
return storeIdInSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean storeVersionInSource() {
|
||||||
|
return storeVersionInSource;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
|
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
|
||||||
super.addPersistentProperty(property);
|
super.addPersistentProperty(property);
|
||||||
|
@ -50,6 +50,7 @@ import org.springframework.data.annotation.Id;
|
|||||||
import org.springframework.data.annotation.ReadOnlyProperty;
|
import org.springframework.data.annotation.ReadOnlyProperty;
|
||||||
import org.springframework.data.annotation.Transient;
|
import org.springframework.data.annotation.Transient;
|
||||||
import org.springframework.data.annotation.TypeAlias;
|
import org.springframework.data.annotation.TypeAlias;
|
||||||
|
import org.springframework.data.annotation.Version;
|
||||||
import org.springframework.data.convert.ReadingConverter;
|
import org.springframework.data.convert.ReadingConverter;
|
||||||
import org.springframework.data.convert.WritingConverter;
|
import org.springframework.data.convert.WritingConverter;
|
||||||
import org.springframework.data.domain.Range;
|
import org.springframework.data.domain.Range;
|
||||||
@ -1893,6 +1894,30 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
assertEquals(expected, json, true);
|
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
|
@Test // #2290
|
||||||
@DisplayName("should respect field setting for empty properties")
|
@DisplayName("should respect field setting for empty properties")
|
||||||
void shouldRespectFieldSettingForEmptyProperties() throws JSONException {
|
void shouldRespectFieldSettingForEmptyProperties() throws JSONException {
|
||||||
@ -3004,6 +3029,43 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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 {
|
static class EntityWithPropertiesThatMightBeEmpty {
|
||||||
@Nullable private String id;
|
@Nullable private String id;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user