Skip to content

Commit a179dd0

Browse files
committed
Add excludeFromSource handling to multifield.
Original Pull Request #2975 Closes #2971 (cherry picked from commit 555b570)
1 parent 310ea07 commit a179dd0

File tree

2 files changed

+52
-37
lines changed

2 files changed

+52
-37
lines changed

src/main/java/org/springframework/data/elasticsearch/core/index/MappingBuilder.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,10 @@ private void buildPropertyMapping(ObjectNode propertiesNode, boolean isRootObjec
345345
: nestedPropertyPrefix + '.' + property.getFieldName();
346346

347347
Field fieldAnnotation = property.findAnnotation(Field.class);
348+
MultiField multiFieldAnnotation = property.findAnnotation(MultiField.class);
348349

349-
if (fieldAnnotation != null && fieldAnnotation.excludeFromSource()) {
350+
if ((fieldAnnotation != null && fieldAnnotation.excludeFromSource()) ||
351+
multiFieldAnnotation != null && multiFieldAnnotation.mainField().excludeFromSource()) {
350352
excludeFromSource.add(nestedPropertyPath);
351353
}
352354

@@ -377,17 +379,15 @@ private void buildPropertyMapping(ObjectNode propertiesNode, boolean isRootObjec
377379
}
378380
}
379381

380-
MultiField multiField = property.findAnnotation(MultiField.class);
381-
382382
if (isCompletionProperty) {
383383
CompletionField completionField = property.findAnnotation(CompletionField.class);
384384
applyCompletionFieldMapping(propertiesNode, property, completionField);
385385
}
386386

387387
if (isRootObject && fieldAnnotation != null && property.isIdProperty()) {
388388
applyDefaultIdFieldMapping(propertiesNode, property);
389-
} else if (multiField != null) {
390-
addMultiFieldMapping(propertiesNode, property, multiField, isNestedOrObjectProperty, dynamicMapping);
389+
} else if (multiFieldAnnotation != null) {
390+
addMultiFieldMapping(propertiesNode, property, multiFieldAnnotation, isNestedOrObjectProperty, dynamicMapping);
391391
} else if (fieldAnnotation != null) {
392392
addSingleFieldMapping(propertiesNode, property, fieldAnnotation, isNestedOrObjectProperty, dynamicMapping);
393393
}

src/test/java/org/springframework/data/elasticsearch/core/index/MappingBuilderUnitTests.java

+47-32
Original file line numberDiff line numberDiff line change
@@ -1090,38 +1090,49 @@ void shouldAddFieldsThatAreExcludedFromSource() throws JSONException {
10901090

10911091
String expected = """
10921092
{
1093-
"properties": {
1094-
"_class": {
1095-
"type": "keyword",
1096-
"index": false,
1097-
"doc_values": false
1098-
},
1099-
"excluded-date": {
1100-
"type": "date",
1101-
"format": "date"
1102-
},
1103-
"nestedEntity": {
1104-
"type": "nested",
1105-
"properties": {
1106-
"_class": {
1107-
"type": "keyword",
1108-
"index": false,
1109-
"doc_values": false
1110-
},
1111-
"excluded-text": {
1112-
"type": "text"
1113-
}
1114-
}
1115-
}
1116-
},
1117-
"_source": {
1118-
"excludes": [
1119-
"excluded-date",
1120-
"nestedEntity.excluded-text"
1121-
]
1122-
}
1123-
}
1124-
"""; //
1093+
"properties": {
1094+
"_class": {
1095+
"type": "keyword",
1096+
"index": false,
1097+
"doc_values": false
1098+
},
1099+
"excluded-date": {
1100+
"type": "date",
1101+
"format": "date"
1102+
},
1103+
"nestedEntity": {
1104+
"type": "nested",
1105+
"properties": {
1106+
"_class": {
1107+
"type": "keyword",
1108+
"index": false,
1109+
"doc_values": false
1110+
},
1111+
"excluded-text": {
1112+
"type": "text"
1113+
}
1114+
}
1115+
},
1116+
"excluded-multifield": {
1117+
"type": "text",
1118+
"fields": {
1119+
"keyword": {
1120+
"type": "keyword"
1121+
}
1122+
}
1123+
}
1124+
},
1125+
"_source": {
1126+
"excludes": [
1127+
"excluded-date",
1128+
"nestedEntity.excluded-text",
1129+
"excluded-multifield"
1130+
]
1131+
}
1132+
1133+
}
1134+
1135+
"""; //
11251136

11261137
String mapping = getMappingBuilder().buildPropertyMapping(ExcludedFieldEntity.class);
11271138

@@ -2395,6 +2406,10 @@ private static class ExcludedFieldEntity {
23952406
excludeFromSource = true) private LocalDate excludedDate;
23962407
@Nullable
23972408
@Field(type = Nested) private NestedExcludedFieldEntity nestedEntity;
2409+
@Nullable
2410+
@MultiField(mainField = @Field(name = "excluded-multifield", type = Text, excludeFromSource = true), otherFields = {
2411+
@InnerField(suffix = "keyword", type = Keyword)
2412+
}) private String excludedMultifield;
23982413
}
23992414

24002415
@SuppressWarnings("unused")

0 commit comments

Comments
 (0)