mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-30 14:13:29 +00:00
DATAES-562 - Custom name attribute for @Field mapping in ElasticsearchEntityMapper.
Original pull request: #270
This commit is contained in:
parent
17d9022181
commit
e7857e874f
@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.annotations;
|
||||
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
@ -29,6 +31,7 @@ import java.lang.annotation.Target;
|
||||
* @author Jonathan Yan
|
||||
* @author Jakub Vavrik
|
||||
* @author Kevin Leturc
|
||||
* @author Peter-Josef Meisch
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@ -36,6 +39,21 @@ import java.lang.annotation.Target;
|
||||
@Inherited
|
||||
public @interface Field {
|
||||
|
||||
/**
|
||||
* Alias for {@link #name}.
|
||||
* @since 3.2
|
||||
*/
|
||||
@AliasFor("name")
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* The <em>name</em> to be used to store the field inside the document.
|
||||
* <p>If not set, the name of the annotated property is used.
|
||||
* @since 3.2
|
||||
*/
|
||||
@AliasFor("value")
|
||||
String name() default "";
|
||||
|
||||
FieldType type() default FieldType.Auto;
|
||||
|
||||
boolean index() default true;
|
||||
|
@ -25,9 +25,15 @@ import org.springframework.data.mapping.PersistentProperty;
|
||||
* @author Mohsin Husen
|
||||
* @author Sascha Woo
|
||||
* @author Oliver Gierke
|
||||
* @author Peter-Josef Meisch
|
||||
*/
|
||||
public interface ElasticsearchPersistentProperty extends PersistentProperty<ElasticsearchPersistentProperty> {
|
||||
|
||||
/**
|
||||
* Returns the name to be used to store the property in the document.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getFieldName();
|
||||
|
||||
/**
|
||||
|
@ -18,6 +18,7 @@ package org.springframework.data.elasticsearch.core.mapping;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.Parent;
|
||||
import org.springframework.data.elasticsearch.annotations.Score;
|
||||
import org.springframework.data.mapping.Association;
|
||||
@ -26,6 +27,7 @@ import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
|
||||
import org.springframework.data.mapping.model.Property;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Elasticsearch specific {@link org.springframework.data.mapping.PersistentProperty} implementation processing
|
||||
@ -35,6 +37,7 @@ import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
* @author Mark Paluch
|
||||
* @author Sascha Woo
|
||||
* @author Oliver Gierke
|
||||
* @author Peter-Josef Meisch
|
||||
*/
|
||||
public class SimpleElasticsearchPersistentProperty extends
|
||||
AnnotationBasedPersistentProperty<ElasticsearchPersistentProperty> implements ElasticsearchPersistentProperty {
|
||||
@ -44,12 +47,14 @@ public class SimpleElasticsearchPersistentProperty extends
|
||||
private final boolean isScore;
|
||||
private final boolean isParent;
|
||||
private final boolean isId;
|
||||
private final String annotatedFieldName;
|
||||
|
||||
public SimpleElasticsearchPersistentProperty(Property property,
|
||||
PersistentEntity<?, ElasticsearchPersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
|
||||
|
||||
super(property, owner, simpleTypeHolder);
|
||||
|
||||
this.annotatedFieldName = getAnnotatedFieldName();
|
||||
this.isId = super.isIdProperty() || SUPPORTED_ID_PROPERTY_NAMES.contains(getFieldName());
|
||||
this.isScore = isAnnotationPresent(Score.class);
|
||||
this.isParent = isAnnotationPresent(Parent.class);
|
||||
@ -68,13 +73,24 @@ public class SimpleElasticsearchPersistentProperty extends
|
||||
}
|
||||
}
|
||||
|
||||
private String getAnnotatedFieldName() {
|
||||
|
||||
if (isAnnotationPresent(Field.class)) {
|
||||
|
||||
String name = findAnnotation(Field.class).name();
|
||||
return StringUtils.hasText(name) ? name : null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty#getFieldName()
|
||||
*/
|
||||
@Override
|
||||
public String getFieldName() {
|
||||
return getProperty().getName();
|
||||
return annotatedFieldName == null ? getProperty().getName() : annotatedFieldName;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -104,7 +120,7 @@ public class SimpleElasticsearchPersistentProperty extends
|
||||
return isScore;
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mapping.model.AbstractPersistentProperty#isImmutable()
|
||||
*/
|
||||
|
@ -18,6 +18,7 @@ package org.springframework.data.elasticsearch.core.mapping;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.Score;
|
||||
import org.springframework.data.mapping.MappingException;
|
||||
|
||||
@ -25,20 +26,49 @@ import org.springframework.data.mapping.MappingException;
|
||||
* Unit tests for {@link SimpleElasticsearchPersistentProperty}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Peter-Josef Meisch
|
||||
*/
|
||||
public class SimpleElasticsearchPersistentPropertyUnitTests {
|
||||
|
||||
private final SimpleElasticsearchMappingContext context = new SimpleElasticsearchMappingContext();
|
||||
|
||||
@Test // DATAES-462
|
||||
public void rejectsScorePropertyOfTypeOtherthanFloat() {
|
||||
|
||||
SimpleElasticsearchMappingContext context = new SimpleElasticsearchMappingContext();
|
||||
|
||||
assertThatExceptionOfType(MappingException.class) //
|
||||
.isThrownBy(() -> context.getRequiredPersistentEntity(InvalidScoreProperty.class)) //
|
||||
.withMessageContaining("scoreProperty");
|
||||
}
|
||||
|
||||
@Test // DATAES-562
|
||||
public void fieldAnnotationWithNameSetsFieldname() {
|
||||
|
||||
final SimpleElasticsearchPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(FieldNameProperty.class);
|
||||
final ElasticsearchPersistentProperty persistentProperty = persistentEntity.getPersistentProperty("fieldProperty");
|
||||
|
||||
assertThat(persistentProperty).isNotNull();
|
||||
assertThat(persistentProperty.getFieldName()).isEqualTo("by-name");
|
||||
}
|
||||
|
||||
@Test // DATAES-562
|
||||
public void fieldAnnotationWithValueSetsFieldname() {
|
||||
|
||||
final SimpleElasticsearchPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(FieldValueProperty.class);
|
||||
final ElasticsearchPersistentProperty persistentProperty = persistentEntity.getPersistentProperty("fieldProperty");
|
||||
|
||||
assertThat(persistentProperty).isNotNull();
|
||||
assertThat(persistentProperty.getFieldName()).isEqualTo("by-value");
|
||||
}
|
||||
|
||||
static class InvalidScoreProperty {
|
||||
@Score String scoreProperty;
|
||||
}
|
||||
|
||||
static class FieldNameProperty {
|
||||
@Field(name = "by-name") String fieldProperty;
|
||||
}
|
||||
|
||||
static class FieldValueProperty {
|
||||
@Field(value = "by-value") String fieldProperty;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user