Skip to content

Commit 82607b3

Browse files
authored
Add Option to not store the id property in the document source.
Original Pull Request #2438 Closes #2364
1 parent cf9b106 commit 82607b3

File tree

6 files changed

+79
-4
lines changed

6 files changed

+79
-4
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@
452452
</jvmArgs>
453453
<excludedGroups>integration-test</excludedGroups>
454454
<targetClasses>
455-
<param>org.springframework.data.elasticsearch.core.geo.*</param>
455+
<param>org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter*</param>
456456
</targetClasses>
457457
<excludedMethods>toString</excludedMethods>
458458
</configuration>

src/main/java/org/springframework/data/elasticsearch/annotations/Document.java

+8
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@
7777
*/
7878
Dynamic dynamic() default Dynamic.INHERIT;
7979

80+
/**
81+
* Specifies if the id property should also be stored in the Elasticsearch document source. Default value is
82+
* {@literal true}
83+
*
84+
* @since 5.1
85+
*/
86+
boolean storeIdInSource() default true;
87+
8088
/**
8189
* @since 4.3
8290
*/

src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,13 @@ private void writeProperties(ElasticsearchPersistentEntity<?> entity, Persistent
939939

940940
for (ElasticsearchPersistentProperty property : entity) {
941941

942-
if (!property.isWritable() || property.isIndexedIndexNameProperty()) {
942+
if (!property.isWritable() //
943+
|| property.isIndexedIndexNameProperty() //
944+
|| (property.isIdProperty() && !entity.storeIdInSource())) {
945+
continue;
946+
}
947+
948+
if (property.isIdProperty() && !entity.storeIdInSource()) {
943949
continue;
944950
}
945951

src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentEntity.java

+6
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,10 @@ default ElasticsearchPersistentProperty getRequiredSeqNoPrimaryTermProperty() {
175175
* @since 4.3
176176
*/
177177
Dynamic dynamic();
178+
179+
/**
180+
* @return the storeIdInSource value from the document annotation
181+
* @since 5.1
182+
*/
183+
boolean storeIdInSource();
178184
}

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntity.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.data.elasticsearch.annotations.Dynamic;
2828
import org.springframework.data.elasticsearch.annotations.Field;
2929
import org.springframework.data.elasticsearch.annotations.FieldType;
30-
import org.springframework.data.elasticsearch.annotations.IndexedIndexName;
3130
import org.springframework.data.elasticsearch.annotations.Routing;
3231
import org.springframework.data.elasticsearch.annotations.Setting;
3332
import org.springframework.data.elasticsearch.core.index.Settings;
@@ -84,6 +83,8 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
8483
private final ConcurrentHashMap<String, Expression> indexNameExpressions = new ConcurrentHashMap<>();
8584
private final Lazy<EvaluationContext> indexNameEvaluationContext = Lazy.of(this::getIndexNameEvaluationContext);
8685

86+
private final boolean storeIdInSource;
87+
8788
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation,
8889
ContextConfiguration contextConfiguration) {
8990

@@ -106,8 +107,10 @@ public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation,
106107
this.versionType = document.versionType();
107108
this.createIndexAndMapping = document.createIndex();
108109
this.dynamic = document.dynamic();
110+
this.storeIdInSource = document.storeIdInSource();
109111
} else {
110112
this.dynamic = Dynamic.INHERIT;
113+
this.storeIdInSource = true;
111114
}
112115
Routing routingAnnotation = AnnotatedElementUtils.findMergedAnnotation(clazz, Routing.class);
113116

@@ -192,6 +195,11 @@ public boolean writeTypeHints() {
192195
return writeTypeHints;
193196
}
194197

198+
@Override
199+
public boolean storeIdInSource() {
200+
return storeIdInSource;
201+
}
202+
195203
@Override
196204
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
197205
super.addPersistentProperty(property);

src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java

+48-1
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,28 @@ private Map<String, Object> writeToMap(Object source) {
18711871
return sink;
18721872
}
18731873

1874+
@Test // #2364
1875+
@DisplayName("should not write id property to document source if configured so")
1876+
void shouldNotWriteIdPropertyToDocumentSourceIfConfiguredSo() throws JSONException {
1877+
1878+
@Language("JSON")
1879+
var expected = """
1880+
{
1881+
"_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$DontWriteIdToSourceEntity",
1882+
"text": "some text"
1883+
}
1884+
""";
1885+
var entity = new DontWriteIdToSourceEntity();
1886+
entity.setId("42");
1887+
entity.setText("some text");
1888+
1889+
Document document = Document.create();
1890+
mappingElasticsearchConverter.write(entity, document);
1891+
String json = document.toJson();
1892+
1893+
assertEquals(expected, json, true);
1894+
}
1895+
18741896
// region entities
18751897
public static class Sample {
18761898
@Nullable public @ReadOnlyProperty String readOnly;
@@ -2885,7 +2907,7 @@ private static final class ImmutableEntityWithCollections {
28852907
@Nullable private Set<Child> childrenSet;
28862908

28872909
public ImmutableEntityWithCollections(@Nullable List<String> stringList, @Nullable Set<String> stringSet,
2888-
@Nullable List<Child> childrenList, @Nullable Set<Child> childrenSet) {
2910+
@Nullable List<Child> childrenList, @Nullable Set<Child> childrenSet) {
28892911
this.stringList = stringList;
28902912
this.stringSet = stringSet;
28912913
this.childrenList = childrenList;
@@ -2927,6 +2949,31 @@ public String getName() {
29272949
}
29282950
}
29292951
}
2952+
2953+
@org.springframework.data.elasticsearch.annotations.Document(indexName = "doesnt-matter", storeIdInSource = false)
2954+
static class DontWriteIdToSourceEntity {
2955+
@Nullable private String id;
2956+
@Nullable
2957+
@Field(type = FieldType.Text) private String text;
2958+
2959+
@Nullable
2960+
public String getId() {
2961+
return id;
2962+
}
2963+
2964+
public void setId(@Nullable String id) {
2965+
this.id = id;
2966+
}
2967+
2968+
@Nullable
2969+
public String getText() {
2970+
return text;
2971+
}
2972+
2973+
public void setText(@Nullable String text) {
2974+
this.text = text;
2975+
}
2976+
}
29302977
// endregion
29312978

29322979
private static String reverse(Object o) {

0 commit comments

Comments
 (0)