-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Allow to customize the mapped type name for @InnerField and @Field annotations #2950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -38,6 +38,7 @@ | |||||
* @author Morgan Lutz | ||||||
* @author Sascha Woo | ||||||
* @author Haibo Liu | ||||||
* @author Andriy Redko | ||||||
*/ | ||||||
@Retention(RetentionPolicy.RUNTIME) | ||||||
@Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.METHOD }) | ||||||
|
@@ -240,4 +241,11 @@ | |||||
* @since 5.1 | ||||||
*/ | ||||||
boolean storeEmptyValue() default true; | ||||||
|
||||||
/** | ||||||
* overrides the mapping field type which otherwise will be taken from corresponding {@link FieldType} | ||||||
* | ||||||
* @since 5.4 | ||||||
*/ | ||||||
String mappedName() default ""; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
to not confuse with the field name |
||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
* @author Brian Kimmig | ||
* @author Morgan Lutz | ||
* @author Haibo Liu | ||
* @author Andriy Redko | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.ANNOTATION_TYPE) | ||
|
@@ -171,4 +172,11 @@ | |
* @since 5.4 | ||
*/ | ||
KnnIndexOptions[] knnIndexOptions() default {}; | ||
|
||
/** | ||
* overrides the mapping field type which otherwise will be taken from corresponding {@link FieldType} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
* | ||
* @since 5.4 | ||
*/ | ||
String mappedName() default ""; | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -69,6 +69,7 @@ | |||||
* @author Peter-Josef Meisch | ||||||
* @author Xiao Yu | ||||||
* @author Subhobrata Dey | ||||||
* @author Andriy Redko | ||||||
*/ | ||||||
public class MappingBuilder { | ||||||
|
||||||
|
@@ -175,7 +176,8 @@ protected String buildPropertyMapping(ElasticsearchPersistentEntity<?> entity, | |||||
.findAnnotation(org.springframework.data.elasticsearch.annotations.Document.class); | ||||||
var dynamicMapping = docAnnotation != null ? docAnnotation.dynamic() : null; | ||||||
|
||||||
mapEntity(objectNode, entity, true, "", false, FieldType.Auto, null, dynamicMapping, runtimeFields); | ||||||
final FieldType fieldType = FieldType.Auto; | ||||||
mapEntity(objectNode, entity, true, "", false, fieldType, fieldType.getMappedName(), null, dynamicMapping, runtimeFields); | ||||||
|
||||||
if (!excludeFromSource.isEmpty()) { | ||||||
ObjectNode sourceNode = objectNode.putObject(SOURCE); | ||||||
|
@@ -210,7 +212,7 @@ private void writeTypeHintMapping(ObjectNode propertiesNode) throws IOException | |||||
} | ||||||
|
||||||
private void mapEntity(ObjectNode objectNode, @Nullable ElasticsearchPersistentEntity<?> entity, | ||||||
boolean isRootObject, String nestedObjectFieldName, boolean nestedOrObjectField, FieldType fieldType, | ||||||
boolean isRootObject, String nestedObjectFieldName, boolean nestedOrObjectField, FieldType fieldType, String mappedName, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
might be mistaken as as mapped name of the entity |
||||||
@Nullable Field parentFieldAnnotation, @Nullable Dynamic dynamicMapping, @Nullable Document runtimeFields) | ||||||
throws IOException { | ||||||
|
||||||
|
@@ -244,7 +246,7 @@ private void mapEntity(ObjectNode objectNode, @Nullable ElasticsearchPersistentE | |||||
boolean writeNestedProperties = !isRootObject && (isAnyPropertyAnnotatedWithField(entity) || nestedOrObjectField); | ||||||
if (writeNestedProperties) { | ||||||
|
||||||
String type = nestedOrObjectField ? fieldType.getMappedName() : FieldType.Object.getMappedName(); | ||||||
String type = nestedOrObjectField ? mappedName : FieldType.Object.getMappedName(); | ||||||
|
||||||
ObjectNode nestedObjectNode = objectMapper.createObjectNode(); | ||||||
nestedObjectNode.put(FIELD_PARAM_TYPE, type); | ||||||
|
@@ -370,7 +372,7 @@ private void buildPropertyMapping(ObjectNode propertiesNode, boolean isRootObjec | |||||
nestedPropertyPrefix = nestedPropertyPath; | ||||||
|
||||||
mapEntity(propertiesNode, persistentEntity, false, property.getFieldName(), true, fieldAnnotation.type(), | ||||||
fieldAnnotation, dynamicMapping, null); | ||||||
getMappedName(fieldAnnotation), fieldAnnotation, dynamicMapping, null); | ||||||
|
||||||
nestedPropertyPrefix = currentNestedPropertyPrefix; | ||||||
return; | ||||||
|
@@ -473,7 +475,7 @@ private void applyDisabledPropertyMapping(ObjectNode propertiesNode, Elasticsear | |||||
} | ||||||
|
||||||
propertiesNode.set(property.getFieldName(), objectMapper.createObjectNode() // | ||||||
.put(FIELD_PARAM_TYPE, field.type().getMappedName()) // | ||||||
.put(FIELD_PARAM_TYPE, getMappedName(field)) // | ||||||
.put(MAPPING_ENABLED, false) // | ||||||
); | ||||||
|
||||||
|
@@ -482,6 +484,15 @@ private void applyDisabledPropertyMapping(ObjectNode propertiesNode, Elasticsear | |||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Return the mapping type name to be used for the {@link Field} | ||||||
* @param field field to return the mapping type name for | ||||||
* @return the mapping type name | ||||||
*/ | ||||||
private String getMappedName(Field field) { | ||||||
return StringUtils.hasText(field.mappedName()) ? field.mappedName() : field.type().getMappedName(); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Add mapping for @Field annotation | ||||||
* | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,7 @@ | |
* @author Brian Kimmig | ||
* @author Morgan Lutz | ||
* @author Haibo Liu | ||
* @author Andriy Redko | ||
*/ | ||
@SpringIntegrationTest | ||
public abstract class MappingBuilderIntegrationTests extends MappingContextBaseTests { | ||
|
@@ -77,6 +78,12 @@ void cleanup() { | |
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete(); | ||
} | ||
|
||
@Test | ||
public void shouldSupportAllTypes() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one is not strictly related but |
||
IndexOperations indexOperations = operations.indexOps(EntityWithAllTypes.class); | ||
indexOperations.createWithMapping(); | ||
} | ||
|
||
@Test | ||
public void shouldNotFailOnCircularReference() { | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems a little clearer for me