Skip to content

Commit f7a6a97

Browse files
authored
Fix FieldType mapping.
Original PullRequest spring-projects#2026 Closes spring-projects#2024
1 parent d6e966e commit f7a6a97

File tree

7 files changed

+402
-178
lines changed

7 files changed

+402
-178
lines changed

Diff for: src/main/java/org/springframework/data/elasticsearch/annotations/CompletionContext.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646
* @since 4.3
4747
*/
4848
enum ContextMappingType {
49-
CATEGORY, GEO
49+
CATEGORY("category"), GEO("geo");
50+
51+
private final String mappedName;
52+
53+
ContextMappingType(String mappedName) {
54+
this.mappedName = mappedName;
55+
}
56+
57+
public String getMappedName() {
58+
return mappedName;
59+
}
5060
}
5161
}

Diff for: src/main/java/org/springframework/data/elasticsearch/annotations/Dynamic.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,44 @@
1717

1818
/**
1919
* Values for the {@code dynamic} mapping parameter.
20-
*
20+
*
2121
* @author Sascha Woo
2222
* @since 4.3
2323
*/
2424
public enum Dynamic {
2525
/**
2626
* New fields are added to the mapping.
2727
*/
28-
TRUE,
28+
TRUE("true"),
2929
/**
3030
* New fields are added to the mapping as
3131
* <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime.html">runtime fields</a>. These
3232
* fields are not indexed, and are loaded from {@code _source} at query time.
3333
*/
34-
RUNTIME,
34+
RUNTIME("runtime"),
3535
/**
3636
* New fields are ignored. These fields will not be indexed or searchable, but will still appear in the
3737
* {@code _source} field of returned hits. These fields will not be added to the mapping, and new fields must be added
3838
* explicitly.
3939
*/
40-
FALSE,
40+
FALSE("false"),
4141
/**
4242
* If new fields are detected, an exception is thrown and the document is rejected. New fields must be explicitly
4343
* added to the mapping.
4444
*/
45-
STRICT,
45+
STRICT("strict"),
4646
/**
4747
* Inherit the dynamic setting from their parent object or from the mapping type.
4848
*/
49-
INHERIT
49+
INHERIT("nherit");
50+
51+
private final String mappedName;
52+
53+
Dynamic(String mappedName) {
54+
this.mappedName = mappedName;
55+
}
56+
57+
public String getMappedName() {
58+
return mappedName;
59+
}
5060
}

Diff for: src/main/java/org/springframework/data/elasticsearch/annotations/DynamicMappingValue.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,23 @@
1717

1818
/**
1919
* values for the {@link DynamicMapping annotation}
20-
*
20+
*
2121
* @author Peter-Josef Meisch
2222
* @author Sascha Woo
2323
* @since 4.0
24-
* @deprecated since 4.3, use {@link Document#dynamic()} or {@link Field#dynamic()} instead.
24+
* @deprecated since 4.3, use {@link Document#dynamic()} or {@link Field#dynamic()} instead.
2525
*/
2626
@Deprecated
2727
public enum DynamicMappingValue {
28-
True, False, Strict
28+
True("true"), False("false"), Strict("strict");
29+
30+
private final String mappedName;
31+
32+
DynamicMappingValue(String mappedName) {
33+
this.mappedName = mappedName;
34+
}
35+
36+
public String getMappedName() {
37+
return mappedName;
38+
}
2939
}

Diff for: src/main/java/org/springframework/data/elasticsearch/annotations/FieldType.java

+43-32
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,51 @@
2626
* @author Morgan Lutz
2727
*/
2828
public enum FieldType {
29-
Auto, //
30-
Text, //
31-
Keyword, //
32-
Long, //
33-
Integer, //
34-
Short, //
35-
Byte, //
36-
Double, //
37-
Float, //
38-
Half_Float, //
39-
Scaled_Float, //
40-
Date, //
41-
Date_Nanos, //
42-
Boolean, //
43-
Binary, //
44-
Integer_Range, //
45-
Float_Range, //
46-
Long_Range, //
47-
Double_Range, //
48-
Date_Range, //
49-
Ip_Range, //
50-
Object, //
51-
Nested, //
52-
Ip, //
53-
TokenCount, //
54-
Percolator, //
55-
Flattened, //
56-
Search_As_You_Type, //
29+
Auto("auto"), //
30+
Text("text"), //
31+
Keyword("keyword"), //
32+
Long("long"), //
33+
Integer("integer"), //
34+
Short("short"), //
35+
Byte("byte"), //
36+
Double("double"), //
37+
Float("float"), //
38+
Half_Float("half_float"), //
39+
Scaled_Float("scaled_float"), //
40+
Date("date"), //
41+
Date_Nanos("date_nanos"), //
42+
Boolean("boolean"), //
43+
Binary("binary"), //
44+
Integer_Range("integer_range"), //
45+
Float_Range("float_range"), //
46+
Long_Range("long_range"), //
47+
Double_Range("double_range"), //
48+
Date_Range("date_range"), //
49+
Ip_Range("ip_range"), //
50+
Object("object"), //
51+
Nested("nested"), //
52+
Ip("ip"), //
53+
TokenCount("token_count"), //
54+
Percolator("percolator"), //
55+
Flattened("flattened"), //
56+
Search_As_You_Type("search_as_you_type"), //
5757
/** @since 4.1 */
58-
Rank_Feature, //
58+
Rank_Feature("rank_feature"), //
5959
/** @since 4.1 */
60-
Rank_Features, //
60+
Rank_Features("rank_features"), //
6161
/** since 4.2 */
62-
Wildcard, //
62+
Wildcard("wildcard"), //
6363
/** @since 4.2 */
64-
Dense_Vector //
64+
Dense_Vector("dense_vector") //
65+
;
66+
67+
private final String mappedName;
68+
69+
FieldType(String mappedName) {
70+
this.mappedName = mappedName;
71+
}
72+
73+
public String getMappedName() {
74+
return mappedName;
75+
}
6576
}

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ private void mapEntity(ObjectNode objectNode, @Nullable ElasticsearchPersistentE
230230
boolean writeNestedProperties = !isRootObject && (isAnyPropertyAnnotatedWithField(entity) || nestedOrObjectField);
231231
if (writeNestedProperties) {
232232

233-
String type = nestedOrObjectField ? fieldType.toString().toLowerCase()
234-
: FieldType.Object.toString().toLowerCase();
233+
String type = nestedOrObjectField ? fieldType.getMappedName() : FieldType.Object.getMappedName();
235234

236235
ObjectNode nestedObjectNode = objectMapper.createObjectNode();
237236
nestedObjectNode.put(FIELD_PARAM_TYPE, type);
@@ -247,9 +246,9 @@ private void mapEntity(ObjectNode objectNode, @Nullable ElasticsearchPersistentE
247246
}
248247

249248
if (entity != null && entity.dynamic() != Dynamic.INHERIT) {
250-
objectNode.put(TYPE_DYNAMIC, entity.dynamic().name().toLowerCase());
249+
objectNode.put(TYPE_DYNAMIC, entity.dynamic().getMappedName());
251250
} else if (dynamicMapping != null) {
252-
objectNode.put(TYPE_DYNAMIC, dynamicMapping.value().name().toLowerCase());
251+
objectNode.put(TYPE_DYNAMIC, dynamicMapping.value().getMappedName());
253252
}
254253

255254
ObjectNode propertiesNode = objectNode.putObject(FIELD_PROPERTIES);
@@ -418,7 +417,7 @@ private void applyCompletionFieldMapping(ObjectNode propertyNode, ElasticsearchP
418417

419418
ObjectNode contextNode = contextsNode.addObject();
420419
contextNode.put(FIELD_CONTEXT_NAME, context.name());
421-
contextNode.put(FIELD_CONTEXT_TYPE, context.type().name().toLowerCase());
420+
contextNode.put(FIELD_CONTEXT_TYPE, context.type().getMappedName());
422421

423422
if (context.precision().length() > 0) {
424423
contextNode.put(FIELD_CONTEXT_PRECISION, context.precision());
@@ -450,7 +449,7 @@ private void applyDisabledPropertyMapping(ObjectNode propertiesNode, Elasticsear
450449
}
451450

452451
propertiesNode.set(property.getFieldName(), objectMapper.createObjectNode() //
453-
.put(FIELD_PARAM_TYPE, field.type().name().toLowerCase()) //
452+
.put(FIELD_PARAM_TYPE, field.type().getMappedName()) //
454453
.put(MAPPING_ENABLED, false) //
455454
);
456455

@@ -479,9 +478,9 @@ private void addSingleFieldMapping(ObjectNode propertiesNode, ElasticsearchPersi
479478

480479
if (nestedOrObjectField) {
481480
if (annotation.dynamic() != Dynamic.INHERIT) {
482-
fieldNode.put(TYPE_DYNAMIC, annotation.dynamic().name().toLowerCase());
481+
fieldNode.put(TYPE_DYNAMIC, annotation.dynamic().getMappedName());
483482
} else if (dynamicMapping != null) {
484-
fieldNode.put(TYPE_DYNAMIC, dynamicMapping.value().name().toLowerCase());
483+
fieldNode.put(TYPE_DYNAMIC, dynamicMapping.value().getMappedName());
485484
}
486485
}
487486
}
@@ -530,9 +529,9 @@ private void addMultiFieldMapping(ObjectNode propertyNode, ElasticsearchPersiste
530529

531530
if (nestedOrObjectField) {
532531
if (annotation.mainField().dynamic() != Dynamic.INHERIT) {
533-
mainFieldNode.put(TYPE_DYNAMIC, annotation.mainField().dynamic().name().toLowerCase());
532+
mainFieldNode.put(TYPE_DYNAMIC, annotation.mainField().dynamic().getMappedName());
534533
} else if (dynamicMapping != null) {
535-
mainFieldNode.put(TYPE_DYNAMIC, dynamicMapping.value().name().toLowerCase());
534+
mainFieldNode.put(TYPE_DYNAMIC, dynamicMapping.value().getMappedName());
536535
}
537536
}
538537

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public void writeTypeAndParametersTo(ObjectNode objectNode) throws IOException {
237237
}
238238

239239
if (type != FieldType.Auto) {
240-
objectNode.put(FIELD_PARAM_TYPE, type.toString().toLowerCase());
240+
objectNode.put(FIELD_PARAM_TYPE, type.getMappedName());
241241

242242
if (type == FieldType.Date) {
243243
List<String> formats = new ArrayList<>();

0 commit comments

Comments
 (0)