Add Option to not store the id property in the document source.

Original Pull Request #2438
Closes #2364
This commit is contained in:
Peter-Josef Meisch 2023-01-29 16:51:41 +01:00 committed by GitHub
parent cf9b106c31
commit 82607b3d4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 4 deletions

View File

@ -452,7 +452,7 @@
</jvmArgs>
<excludedGroups>integration-test</excludedGroups>
<targetClasses>
<param>org.springframework.data.elasticsearch.core.geo.*</param>
<param>org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter*</param>
</targetClasses>
<excludedMethods>toString</excludedMethods>
</configuration>

View File

@ -77,6 +77,14 @@ public @interface Document {
*/
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
*/

View File

@ -939,7 +939,13 @@ public class MappingElasticsearchConverter
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;
}

View File

@ -175,4 +175,10 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
* @since 4.3
*/
Dynamic dynamic();
/**
* @return the storeIdInSource value from the document annotation
* @since 5.1
*/
boolean storeIdInSource();
}

View File

@ -27,7 +27,6 @@ import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Dynamic;
import org.springframework.data.elasticsearch.annotations.Field;
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.Setting;
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 Lazy<EvaluationContext> indexNameEvaluationContext = Lazy.of(this::getIndexNameEvaluationContext);
private final boolean storeIdInSource;
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation,
ContextConfiguration contextConfiguration) {
@ -106,8 +107,10 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
this.versionType = document.versionType();
this.createIndexAndMapping = document.createIndex();
this.dynamic = document.dynamic();
this.storeIdInSource = document.storeIdInSource();
} else {
this.dynamic = Dynamic.INHERIT;
this.storeIdInSource = true;
}
Routing routingAnnotation = AnnotatedElementUtils.findMergedAnnotation(clazz, Routing.class);
@ -192,6 +195,11 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
return writeTypeHints;
}
@Override
public boolean storeIdInSource() {
return storeIdInSource;
}
@Override
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
super.addPersistentProperty(property);

View File

@ -1871,6 +1871,28 @@ public class MappingElasticsearchConverterUnitTests {
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
public static class Sample {
@Nullable public @ReadOnlyProperty String readOnly;
@ -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
private static String reverse(Object o) {