diff --git a/src/main/java/org/springframework/data/elasticsearch/annotations/Field.java b/src/main/java/org/springframework/data/elasticsearch/annotations/Field.java
index 24f039c5..38b11bf7 100644
--- a/src/main/java/org/springframework/data/elasticsearch/annotations/Field.java
+++ b/src/main/java/org/springframework/data/elasticsearch/annotations/Field.java
@@ -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 name to be used to store the field inside the document.
+ *
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;
diff --git a/src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentProperty.java b/src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentProperty.java
index 6e5a2ef1..170c39f8 100644
--- a/src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentProperty.java
+++ b/src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentProperty.java
@@ -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 {
+ /**
+ * Returns the name to be used to store the property in the document.
+ *
+ * @return
+ */
String getFieldName();
/**
diff --git a/src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentProperty.java b/src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentProperty.java
index 4f3e3168..d13cc7fb 100644
--- a/src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentProperty.java
+++ b/src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentProperty.java
@@ -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 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()
*/
diff --git a/src/test/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentPropertyUnitTests.java b/src/test/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentPropertyUnitTests.java
index 0c270415..59e57aae 100644
--- a/src/test/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentPropertyUnitTests.java
+++ b/src/test/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentPropertyUnitTests.java
@@ -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;
+ }
}