Skip to content

Commit

Permalink
Fix FieldType mapping.
Browse files Browse the repository at this point in the history
Original PullRequest #2026
Closes #2024
  • Loading branch information
sothawo committed Dec 13, 2021
1 parent d6e966e commit f7a6a97
Show file tree
Hide file tree
Showing 7 changed files with 402 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@
* @since 4.3
*/
enum ContextMappingType {
CATEGORY, GEO
CATEGORY("category"), GEO("geo");

private final String mappedName;

ContextMappingType(String mappedName) {
this.mappedName = mappedName;
}

public String getMappedName() {
return mappedName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,44 @@

/**
* Values for the {@code dynamic} mapping parameter.
*
*
* @author Sascha Woo
* @since 4.3
*/
public enum Dynamic {
/**
* New fields are added to the mapping.
*/
TRUE,
TRUE("true"),
/**
* New fields are added to the mapping as
* <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime.html">runtime fields</a>. These
* fields are not indexed, and are loaded from {@code _source} at query time.
*/
RUNTIME,
RUNTIME("runtime"),
/**
* New fields are ignored. These fields will not be indexed or searchable, but will still appear in the
* {@code _source} field of returned hits. These fields will not be added to the mapping, and new fields must be added
* explicitly.
*/
FALSE,
FALSE("false"),
/**
* If new fields are detected, an exception is thrown and the document is rejected. New fields must be explicitly
* added to the mapping.
*/
STRICT,
STRICT("strict"),
/**
* Inherit the dynamic setting from their parent object or from the mapping type.
*/
INHERIT
INHERIT("nherit");

private final String mappedName;

Dynamic(String mappedName) {
this.mappedName = mappedName;
}

public String getMappedName() {
return mappedName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@

/**
* values for the {@link DynamicMapping annotation}
*
*
* @author Peter-Josef Meisch
* @author Sascha Woo
* @since 4.0
* @deprecated since 4.3, use {@link Document#dynamic()} or {@link Field#dynamic()} instead.
* @deprecated since 4.3, use {@link Document#dynamic()} or {@link Field#dynamic()} instead.
*/
@Deprecated
public enum DynamicMappingValue {
True, False, Strict
True("true"), False("false"), Strict("strict");

private final String mappedName;

DynamicMappingValue(String mappedName) {
this.mappedName = mappedName;
}

public String getMappedName() {
return mappedName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,51 @@
* @author Morgan Lutz
*/
public enum FieldType {
Auto, //
Text, //
Keyword, //
Long, //
Integer, //
Short, //
Byte, //
Double, //
Float, //
Half_Float, //
Scaled_Float, //
Date, //
Date_Nanos, //
Boolean, //
Binary, //
Integer_Range, //
Float_Range, //
Long_Range, //
Double_Range, //
Date_Range, //
Ip_Range, //
Object, //
Nested, //
Ip, //
TokenCount, //
Percolator, //
Flattened, //
Search_As_You_Type, //
Auto("auto"), //
Text("text"), //
Keyword("keyword"), //
Long("long"), //
Integer("integer"), //
Short("short"), //
Byte("byte"), //
Double("double"), //
Float("float"), //
Half_Float("half_float"), //
Scaled_Float("scaled_float"), //
Date("date"), //
Date_Nanos("date_nanos"), //
Boolean("boolean"), //
Binary("binary"), //
Integer_Range("integer_range"), //
Float_Range("float_range"), //
Long_Range("long_range"), //
Double_Range("double_range"), //
Date_Range("date_range"), //
Ip_Range("ip_range"), //
Object("object"), //
Nested("nested"), //
Ip("ip"), //
TokenCount("token_count"), //
Percolator("percolator"), //
Flattened("flattened"), //
Search_As_You_Type("search_as_you_type"), //
/** @since 4.1 */
Rank_Feature, //
Rank_Feature("rank_feature"), //
/** @since 4.1 */
Rank_Features, //
Rank_Features("rank_features"), //
/** since 4.2 */
Wildcard, //
Wildcard("wildcard"), //
/** @since 4.2 */
Dense_Vector //
Dense_Vector("dense_vector") //
;

private final String mappedName;

FieldType(String mappedName) {
this.mappedName = mappedName;
}

public String getMappedName() {
return mappedName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ private void mapEntity(ObjectNode objectNode, @Nullable ElasticsearchPersistentE
boolean writeNestedProperties = !isRootObject && (isAnyPropertyAnnotatedWithField(entity) || nestedOrObjectField);
if (writeNestedProperties) {

String type = nestedOrObjectField ? fieldType.toString().toLowerCase()
: FieldType.Object.toString().toLowerCase();
String type = nestedOrObjectField ? fieldType.getMappedName() : FieldType.Object.getMappedName();

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

if (entity != null && entity.dynamic() != Dynamic.INHERIT) {
objectNode.put(TYPE_DYNAMIC, entity.dynamic().name().toLowerCase());
objectNode.put(TYPE_DYNAMIC, entity.dynamic().getMappedName());
} else if (dynamicMapping != null) {
objectNode.put(TYPE_DYNAMIC, dynamicMapping.value().name().toLowerCase());
objectNode.put(TYPE_DYNAMIC, dynamicMapping.value().getMappedName());
}

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

ObjectNode contextNode = contextsNode.addObject();
contextNode.put(FIELD_CONTEXT_NAME, context.name());
contextNode.put(FIELD_CONTEXT_TYPE, context.type().name().toLowerCase());
contextNode.put(FIELD_CONTEXT_TYPE, context.type().getMappedName());

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

propertiesNode.set(property.getFieldName(), objectMapper.createObjectNode() //
.put(FIELD_PARAM_TYPE, field.type().name().toLowerCase()) //
.put(FIELD_PARAM_TYPE, field.type().getMappedName()) //
.put(MAPPING_ENABLED, false) //
);

Expand Down Expand Up @@ -479,9 +478,9 @@ private void addSingleFieldMapping(ObjectNode propertiesNode, ElasticsearchPersi

if (nestedOrObjectField) {
if (annotation.dynamic() != Dynamic.INHERIT) {
fieldNode.put(TYPE_DYNAMIC, annotation.dynamic().name().toLowerCase());
fieldNode.put(TYPE_DYNAMIC, annotation.dynamic().getMappedName());
} else if (dynamicMapping != null) {
fieldNode.put(TYPE_DYNAMIC, dynamicMapping.value().name().toLowerCase());
fieldNode.put(TYPE_DYNAMIC, dynamicMapping.value().getMappedName());
}
}
}
Expand Down Expand Up @@ -530,9 +529,9 @@ private void addMultiFieldMapping(ObjectNode propertyNode, ElasticsearchPersiste

if (nestedOrObjectField) {
if (annotation.mainField().dynamic() != Dynamic.INHERIT) {
mainFieldNode.put(TYPE_DYNAMIC, annotation.mainField().dynamic().name().toLowerCase());
mainFieldNode.put(TYPE_DYNAMIC, annotation.mainField().dynamic().getMappedName());
} else if (dynamicMapping != null) {
mainFieldNode.put(TYPE_DYNAMIC, dynamicMapping.value().name().toLowerCase());
mainFieldNode.put(TYPE_DYNAMIC, dynamicMapping.value().getMappedName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public void writeTypeAndParametersTo(ObjectNode objectNode) throws IOException {
}

if (type != FieldType.Auto) {
objectNode.put(FIELD_PARAM_TYPE, type.toString().toLowerCase());
objectNode.put(FIELD_PARAM_TYPE, type.getMappedName());

if (type == FieldType.Date) {
List<String> formats = new ArrayList<>();
Expand Down
Loading

0 comments on commit f7a6a97

Please sign in to comment.