mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-24 13:02:10 +00:00
Add Option to not store the id property in the document source.
Original Pull Request #2438 Closes #2364
This commit is contained in:
parent
cf9b106c31
commit
82607b3d4d
2
pom.xml
2
pom.xml
@ -452,7 +452,7 @@
|
|||||||
</jvmArgs>
|
</jvmArgs>
|
||||||
<excludedGroups>integration-test</excludedGroups>
|
<excludedGroups>integration-test</excludedGroups>
|
||||||
<targetClasses>
|
<targetClasses>
|
||||||
<param>org.springframework.data.elasticsearch.core.geo.*</param>
|
<param>org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter*</param>
|
||||||
</targetClasses>
|
</targetClasses>
|
||||||
<excludedMethods>toString</excludedMethods>
|
<excludedMethods>toString</excludedMethods>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
@ -77,6 +77,14 @@ public @interface Document {
|
|||||||
*/
|
*/
|
||||||
Dynamic dynamic() default Dynamic.INHERIT;
|
Dynamic dynamic() default Dynamic.INHERIT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specifies if the id property should also be stored in the Elasticsearch document source. Default value is
|
||||||
|
* {@literal true}
|
||||||
|
*
|
||||||
|
* @since 5.1
|
||||||
|
*/
|
||||||
|
boolean storeIdInSource() default true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @since 4.3
|
* @since 4.3
|
||||||
*/
|
*/
|
||||||
|
@ -939,7 +939,13 @@ public class MappingElasticsearchConverter
|
|||||||
|
|
||||||
for (ElasticsearchPersistentProperty property : entity) {
|
for (ElasticsearchPersistentProperty property : entity) {
|
||||||
|
|
||||||
if (!property.isWritable() || property.isIndexedIndexNameProperty()) {
|
if (!property.isWritable() //
|
||||||
|
|| property.isIndexedIndexNameProperty() //
|
||||||
|
|| (property.isIdProperty() && !entity.storeIdInSource())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (property.isIdProperty() && !entity.storeIdInSource()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,4 +175,10 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
|
|||||||
* @since 4.3
|
* @since 4.3
|
||||||
*/
|
*/
|
||||||
Dynamic dynamic();
|
Dynamic dynamic();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the storeIdInSource value from the document annotation
|
||||||
|
* @since 5.1
|
||||||
|
*/
|
||||||
|
boolean storeIdInSource();
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,6 @@ import org.springframework.data.elasticsearch.annotations.Document;
|
|||||||
import org.springframework.data.elasticsearch.annotations.Dynamic;
|
import org.springframework.data.elasticsearch.annotations.Dynamic;
|
||||||
import org.springframework.data.elasticsearch.annotations.Field;
|
import org.springframework.data.elasticsearch.annotations.Field;
|
||||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||||
import org.springframework.data.elasticsearch.annotations.IndexedIndexName;
|
|
||||||
import org.springframework.data.elasticsearch.annotations.Routing;
|
import org.springframework.data.elasticsearch.annotations.Routing;
|
||||||
import org.springframework.data.elasticsearch.annotations.Setting;
|
import org.springframework.data.elasticsearch.annotations.Setting;
|
||||||
import org.springframework.data.elasticsearch.core.index.Settings;
|
import org.springframework.data.elasticsearch.core.index.Settings;
|
||||||
@ -84,6 +83,8 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
|||||||
private final ConcurrentHashMap<String, Expression> indexNameExpressions = new ConcurrentHashMap<>();
|
private final ConcurrentHashMap<String, Expression> indexNameExpressions = new ConcurrentHashMap<>();
|
||||||
private final Lazy<EvaluationContext> indexNameEvaluationContext = Lazy.of(this::getIndexNameEvaluationContext);
|
private final Lazy<EvaluationContext> indexNameEvaluationContext = Lazy.of(this::getIndexNameEvaluationContext);
|
||||||
|
|
||||||
|
private final boolean storeIdInSource;
|
||||||
|
|
||||||
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation,
|
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation,
|
||||||
ContextConfiguration contextConfiguration) {
|
ContextConfiguration contextConfiguration) {
|
||||||
|
|
||||||
@ -106,8 +107,10 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
|||||||
this.versionType = document.versionType();
|
this.versionType = document.versionType();
|
||||||
this.createIndexAndMapping = document.createIndex();
|
this.createIndexAndMapping = document.createIndex();
|
||||||
this.dynamic = document.dynamic();
|
this.dynamic = document.dynamic();
|
||||||
|
this.storeIdInSource = document.storeIdInSource();
|
||||||
} else {
|
} else {
|
||||||
this.dynamic = Dynamic.INHERIT;
|
this.dynamic = Dynamic.INHERIT;
|
||||||
|
this.storeIdInSource = true;
|
||||||
}
|
}
|
||||||
Routing routingAnnotation = AnnotatedElementUtils.findMergedAnnotation(clazz, Routing.class);
|
Routing routingAnnotation = AnnotatedElementUtils.findMergedAnnotation(clazz, Routing.class);
|
||||||
|
|
||||||
@ -192,6 +195,11 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
|||||||
return writeTypeHints;
|
return writeTypeHints;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean storeIdInSource() {
|
||||||
|
return storeIdInSource;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
|
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
|
||||||
super.addPersistentProperty(property);
|
super.addPersistentProperty(property);
|
||||||
|
@ -1871,6 +1871,28 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
return sink;
|
return sink;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // #2364
|
||||||
|
@DisplayName("should not write id property to document source if configured so")
|
||||||
|
void shouldNotWriteIdPropertyToDocumentSourceIfConfiguredSo() throws JSONException {
|
||||||
|
|
||||||
|
@Language("JSON")
|
||||||
|
var expected = """
|
||||||
|
{
|
||||||
|
"_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$DontWriteIdToSourceEntity",
|
||||||
|
"text": "some text"
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
var entity = new DontWriteIdToSourceEntity();
|
||||||
|
entity.setId("42");
|
||||||
|
entity.setText("some text");
|
||||||
|
|
||||||
|
Document document = Document.create();
|
||||||
|
mappingElasticsearchConverter.write(entity, document);
|
||||||
|
String json = document.toJson();
|
||||||
|
|
||||||
|
assertEquals(expected, json, true);
|
||||||
|
}
|
||||||
|
|
||||||
// region entities
|
// region entities
|
||||||
public static class Sample {
|
public static class Sample {
|
||||||
@Nullable public @ReadOnlyProperty String readOnly;
|
@Nullable public @ReadOnlyProperty String readOnly;
|
||||||
@ -2885,7 +2907,7 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
@Nullable private Set<Child> childrenSet;
|
@Nullable private Set<Child> childrenSet;
|
||||||
|
|
||||||
public ImmutableEntityWithCollections(@Nullable List<String> stringList, @Nullable Set<String> stringSet,
|
public ImmutableEntityWithCollections(@Nullable List<String> stringList, @Nullable Set<String> stringSet,
|
||||||
@Nullable List<Child> childrenList, @Nullable Set<Child> childrenSet) {
|
@Nullable List<Child> childrenList, @Nullable Set<Child> childrenSet) {
|
||||||
this.stringList = stringList;
|
this.stringList = stringList;
|
||||||
this.stringSet = stringSet;
|
this.stringSet = stringSet;
|
||||||
this.childrenList = childrenList;
|
this.childrenList = childrenList;
|
||||||
@ -2927,6 +2949,31 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@org.springframework.data.elasticsearch.annotations.Document(indexName = "doesnt-matter", storeIdInSource = false)
|
||||||
|
static class DontWriteIdToSourceEntity {
|
||||||
|
@Nullable private String id;
|
||||||
|
@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 String getText() {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setText(@Nullable String text) {
|
||||||
|
this.text = text;
|
||||||
|
}
|
||||||
|
}
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
private static String reverse(Object o) {
|
private static String reverse(Object o) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user