Skip to content

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

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
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@
*/
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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,13 @@ private void writeProperties(ElasticsearchPersistentEntity<?> entity, Persistent

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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,10 @@ default ElasticsearchPersistentProperty getRequiredSeqNoPrimaryTermProperty() {
* @since 4.3
*/
Dynamic dynamic();

/**
* @return the storeIdInSource value from the document annotation
* @since 5.1
*/
boolean storeIdInSource();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
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;
Expand Down Expand Up @@ -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) {

Expand All @@ -106,8 +107,10 @@ public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation,
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);

Expand Down Expand Up @@ -192,6 +195,11 @@ public boolean writeTypeHints() {
return writeTypeHints;
}

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

@Override
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
super.addPersistentProperty(property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,28 @@ private Map<String, Object> writeToMap(Object source) {
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;
Expand Down Expand Up @@ -2885,7 +2907,7 @@ private static final class ImmutableEntityWithCollections {
@Nullable private Set<Child> childrenSet;

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.stringSet = stringSet;
this.childrenList = childrenList;
Expand Down Expand Up @@ -2927,6 +2949,31 @@ public String getName() {
}
}
}

@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) {
Expand Down