Skip to content

Commit 555b570

Browse files
authored
Add excludeFromSource handling to multifield.
Original Pull Request #2975 Closes #2971
1 parent 81eb167 commit 555b570

File tree

2 files changed

+59
-44
lines changed

2 files changed

+59
-44
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
@@ -349,8 +349,10 @@ private void buildPropertyMapping(ObjectNode propertiesNode, boolean isRootObjec
349349
: nestedPropertyPrefix + '.' + property.getFieldName();
350350

351351
Field fieldAnnotation = property.findAnnotation(Field.class);
352+
MultiField multiFieldAnnotation = property.findAnnotation(MultiField.class);
352353

353-
if (fieldAnnotation != null && fieldAnnotation.excludeFromSource()) {
354+
if ((fieldAnnotation != null && fieldAnnotation.excludeFromSource()) ||
355+
multiFieldAnnotation != null && multiFieldAnnotation.mainField().excludeFromSource()) {
354356
excludeFromSource.add(nestedPropertyPath);
355357
}
356358

@@ -381,17 +383,15 @@ private void buildPropertyMapping(ObjectNode propertiesNode, boolean isRootObjec
381383
}
382384
}
383385

384-
MultiField multiField = property.findAnnotation(MultiField.class);
385-
386386
if (isCompletionProperty) {
387387
CompletionField completionField = property.findAnnotation(CompletionField.class);
388388
applyCompletionFieldMapping(propertiesNode, property, completionField);
389389
}
390390

391391
if (isRootObject && fieldAnnotation != null && property.isIdProperty()) {
392392
applyDefaultIdFieldMapping(propertiesNode, property);
393-
} else if (multiField != null) {
394-
addMultiFieldMapping(propertiesNode, property, multiField, isNestedOrObjectProperty, dynamicMapping);
393+
} else if (multiFieldAnnotation != null) {
394+
addMultiFieldMapping(propertiesNode, property, multiFieldAnnotation, isNestedOrObjectProperty, dynamicMapping);
395395
} else if (fieldAnnotation != null) {
396396
addSingleFieldMapping(propertiesNode, property, fieldAnnotation, isNestedOrObjectProperty, dynamicMapping);
397397
}

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

+54-39
Original file line numberDiff line numberDiff line change
@@ -1126,38 +1126,49 @@ void shouldAddFieldsThatAreExcludedFromSource() throws JSONException {
11261126

11271127
String expected = """
11281128
{
1129-
"properties": {
1130-
"_class": {
1131-
"type": "keyword",
1132-
"index": false,
1133-
"doc_values": false
1134-
},
1135-
"excluded-date": {
1136-
"type": "date",
1137-
"format": "date"
1138-
},
1139-
"nestedEntity": {
1140-
"type": "nested",
1141-
"properties": {
1142-
"_class": {
1143-
"type": "keyword",
1144-
"index": false,
1145-
"doc_values": false
1146-
},
1147-
"excluded-text": {
1148-
"type": "text"
1149-
}
1150-
}
1151-
}
1152-
},
1153-
"_source": {
1154-
"excludes": [
1155-
"excluded-date",
1156-
"nestedEntity.excluded-text"
1157-
]
1158-
}
1159-
}
1160-
"""; //
1129+
"properties": {
1130+
"_class": {
1131+
"type": "keyword",
1132+
"index": false,
1133+
"doc_values": false
1134+
},
1135+
"excluded-date": {
1136+
"type": "date",
1137+
"format": "date"
1138+
},
1139+
"nestedEntity": {
1140+
"type": "nested",
1141+
"properties": {
1142+
"_class": {
1143+
"type": "keyword",
1144+
"index": false,
1145+
"doc_values": false
1146+
},
1147+
"excluded-text": {
1148+
"type": "text"
1149+
}
1150+
}
1151+
},
1152+
"excluded-multifield": {
1153+
"type": "text",
1154+
"fields": {
1155+
"keyword": {
1156+
"type": "keyword"
1157+
}
1158+
}
1159+
}
1160+
},
1161+
"_source": {
1162+
"excludes": [
1163+
"excluded-date",
1164+
"nestedEntity.excluded-text",
1165+
"excluded-multifield"
1166+
]
1167+
}
1168+
1169+
}
1170+
1171+
"""; //
11611172

11621173
String mapping = getMappingBuilder().buildPropertyMapping(ExcludedFieldEntity.class);
11631174

@@ -1243,7 +1254,7 @@ void shouldWriteFieldAliasesToTheMapping() throws JSONException {
12431254

12441255
assertEquals(expected, mapping, true);
12451256
}
1246-
1257+
12471258
@Test // #2942
12481259
@DisplayName("should use custom mapped name")
12491260
void shouldUseCustomMappedName() throws JSONException {
@@ -2192,8 +2203,7 @@ static class DenseVectorEntityWithKnnSearch {
21922203
@Nullable
21932204
@Field(type = FieldType.Dense_Vector, dims = 16, elementType = FieldElementType.FLOAT,
21942205
knnIndexOptions = @KnnIndexOptions(type = KnnAlgorithmType.HNSW, m = 16, efConstruction = 100),
2195-
knnSimilarity = KnnSimilarity.DOT_PRODUCT)
2196-
private float[] my_vector;
2206+
knnSimilarity = KnnSimilarity.DOT_PRODUCT) private float[] my_vector;
21972207

21982208
@Nullable
21992209
public String getId() {
@@ -2269,8 +2279,7 @@ public void setText(@Nullable String text) {
22692279
static class DenseVectorMisMatchConfidenceIntervalClass {
22702280
@Field(type = Dense_Vector, dims = 16, elementType = FieldElementType.FLOAT,
22712281
knnIndexOptions = @KnnIndexOptions(type = KnnAlgorithmType.HNSW, m = 16, confidenceInterval = 0.95F),
2272-
knnSimilarity = KnnSimilarity.DOT_PRODUCT)
2273-
private float[] dense_vector;
2282+
knnSimilarity = KnnSimilarity.DOT_PRODUCT) private float[] dense_vector;
22742283
}
22752284

22762285
static class DisabledMappingProperty {
@@ -2553,6 +2562,10 @@ private static class ExcludedFieldEntity {
25532562
excludeFromSource = true) private LocalDate excludedDate;
25542563
@Nullable
25552564
@Field(type = Nested) private NestedExcludedFieldEntity nestedEntity;
2565+
@Nullable
2566+
@MultiField(mainField = @Field(name = "excluded-multifield", type = Text, excludeFromSource = true), otherFields = {
2567+
@InnerField(suffix = "keyword", type = Keyword)
2568+
}) private String excludedMultifield;
25562569
}
25572570

25582571
@SuppressWarnings("unused")
@@ -2599,8 +2612,10 @@ private static class FieldMappedNameEntity {
25992612
@SuppressWarnings("unused")
26002613
private static class MultiFieldMappedNameEntity {
26012614
@Nullable
2602-
@MultiField(mainField = @Field(type = FieldType.Text, mappedTypeName = "match_only_text"), otherFields = { @InnerField(suffix = "lower_case",
2603-
type = FieldType.Keyword, normalizer = "lower_case_normalizer", mappedTypeName = "constant_keyword") }) private String description;
2615+
@MultiField(mainField = @Field(type = FieldType.Text, mappedTypeName = "match_only_text"),
2616+
otherFields = { @InnerField(suffix = "lower_case",
2617+
type = FieldType.Keyword, normalizer = "lower_case_normalizer",
2618+
mappedTypeName = "constant_keyword") }) private String description;
26042619
}
26052620

26062621
@SuppressWarnings("unused")

0 commit comments

Comments
 (0)