Skip to content

Commit 5a2dcd3

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

File tree

2 files changed

+52
-37
lines changed

2 files changed

+52
-37
lines changed

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

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

336336
Field fieldAnnotation = property.findAnnotation(Field.class);
337+
MultiField multiFieldAnnotation = property.findAnnotation(MultiField.class);
337338

338-
if (fieldAnnotation != null && fieldAnnotation.excludeFromSource()) {
339+
if ((fieldAnnotation != null && fieldAnnotation.excludeFromSource()) ||
340+
multiFieldAnnotation != null && multiFieldAnnotation.mainField().excludeFromSource()) {
339341
excludeFromSource.add(nestedPropertyPath);
340342
}
341343

@@ -366,17 +368,15 @@ private void buildPropertyMapping(ObjectNode propertiesNode, boolean isRootObjec
366368
}
367369
}
368370

369-
MultiField multiField = property.findAnnotation(MultiField.class);
370-
371371
if (isCompletionProperty) {
372372
CompletionField completionField = property.findAnnotation(CompletionField.class);
373373
applyCompletionFieldMapping(propertiesNode, property, completionField);
374374
}
375375

376376
if (isRootObject && fieldAnnotation != null && property.isIdProperty()) {
377377
applyDefaultIdFieldMapping(propertiesNode, property);
378-
} else if (multiField != null) {
379-
addMultiFieldMapping(propertiesNode, property, multiField, isNestedOrObjectProperty, dynamicMapping);
378+
} else if (multiFieldAnnotation != null) {
379+
addMultiFieldMapping(propertiesNode, property, multiFieldAnnotation, isNestedOrObjectProperty, dynamicMapping);
380380
} else if (fieldAnnotation != null) {
381381
addSingleFieldMapping(propertiesNode, property, fieldAnnotation, isNestedOrObjectProperty, dynamicMapping);
382382
}

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

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

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

11311142
String mapping = getMappingBuilder().buildPropertyMapping(ExcludedFieldEntity.class);
11321143

@@ -2367,6 +2378,10 @@ private static class ExcludedFieldEntity {
23672378
excludeFromSource = true) private LocalDate excludedDate;
23682379
@Nullable
23692380
@Field(type = Nested) private NestedExcludedFieldEntity nestedEntity;
2381+
@Nullable
2382+
@MultiField(mainField = @Field(name = "excluded-multifield", type = Text, excludeFromSource = true), otherFields = {
2383+
@InnerField(suffix = "keyword", type = Keyword)
2384+
}) private String excludedMultifield;
23702385
}
23712386

23722387
@SuppressWarnings("unused")

0 commit comments

Comments
 (0)