Skip to content

Commit e7857e8

Browse files
sothawoxhaggi
authored andcommitted
DATAES-562 - Custom name attribute for @field mapping in ElasticsearchEntityMapper.
Original pull request: spring-projects#270
1 parent 17d9022 commit e7857e8

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

src/main/java/org/springframework/data/elasticsearch/annotations/Field.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.elasticsearch.annotations;
1717

18+
import org.springframework.core.annotation.AliasFor;
19+
1820
import java.lang.annotation.Documented;
1921
import java.lang.annotation.ElementType;
2022
import java.lang.annotation.Inherited;
@@ -29,13 +31,29 @@
2931
* @author Jonathan Yan
3032
* @author Jakub Vavrik
3133
* @author Kevin Leturc
34+
* @author Peter-Josef Meisch
3235
*/
3336
@Retention(RetentionPolicy.RUNTIME)
3437
@Target(ElementType.FIELD)
3538
@Documented
3639
@Inherited
3740
public @interface Field {
3841

42+
/**
43+
* Alias for {@link #name}.
44+
* @since 3.2
45+
*/
46+
@AliasFor("name")
47+
String value() default "";
48+
49+
/**
50+
* The <em>name</em> to be used to store the field inside the document.
51+
* <p>If not set, the name of the annotated property is used.
52+
* @since 3.2
53+
*/
54+
@AliasFor("value")
55+
String name() default "";
56+
3957
FieldType type() default FieldType.Auto;
4058

4159
boolean index() default true;

src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentProperty.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@
2525
* @author Mohsin Husen
2626
* @author Sascha Woo
2727
* @author Oliver Gierke
28+
* @author Peter-Josef Meisch
2829
*/
2930
public interface ElasticsearchPersistentProperty extends PersistentProperty<ElasticsearchPersistentProperty> {
3031

32+
/**
33+
* Returns the name to be used to store the property in the document.
34+
*
35+
* @return
36+
*/
3137
String getFieldName();
3238

3339
/**

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentProperty.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Arrays;
1919
import java.util.List;
2020

21+
import org.springframework.data.elasticsearch.annotations.Field;
2122
import org.springframework.data.elasticsearch.annotations.Parent;
2223
import org.springframework.data.elasticsearch.annotations.Score;
2324
import org.springframework.data.mapping.Association;
@@ -26,6 +27,7 @@
2627
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
2728
import org.springframework.data.mapping.model.Property;
2829
import org.springframework.data.mapping.model.SimpleTypeHolder;
30+
import org.springframework.util.StringUtils;
2931

3032
/**
3133
* Elasticsearch specific {@link org.springframework.data.mapping.PersistentProperty} implementation processing
@@ -35,6 +37,7 @@
3537
* @author Mark Paluch
3638
* @author Sascha Woo
3739
* @author Oliver Gierke
40+
* @author Peter-Josef Meisch
3841
*/
3942
public class SimpleElasticsearchPersistentProperty extends
4043
AnnotationBasedPersistentProperty<ElasticsearchPersistentProperty> implements ElasticsearchPersistentProperty {
@@ -44,12 +47,14 @@ public class SimpleElasticsearchPersistentProperty extends
4447
private final boolean isScore;
4548
private final boolean isParent;
4649
private final boolean isId;
50+
private final String annotatedFieldName;
4751

4852
public SimpleElasticsearchPersistentProperty(Property property,
4953
PersistentEntity<?, ElasticsearchPersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
5054

5155
super(property, owner, simpleTypeHolder);
5256

57+
this.annotatedFieldName = getAnnotatedFieldName();
5358
this.isId = super.isIdProperty() || SUPPORTED_ID_PROPERTY_NAMES.contains(getFieldName());
5459
this.isScore = isAnnotationPresent(Score.class);
5560
this.isParent = isAnnotationPresent(Parent.class);
@@ -68,13 +73,24 @@ public SimpleElasticsearchPersistentProperty(Property property,
6873
}
6974
}
7075

76+
private String getAnnotatedFieldName() {
77+
78+
if (isAnnotationPresent(Field.class)) {
79+
80+
String name = findAnnotation(Field.class).name();
81+
return StringUtils.hasText(name) ? name : null;
82+
}
83+
84+
return null;
85+
}
86+
7187
/*
7288
* (non-Javadoc)
7389
* @see org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty#getFieldName()
7490
*/
7591
@Override
7692
public String getFieldName() {
77-
return getProperty().getName();
93+
return annotatedFieldName == null ? getProperty().getName() : annotatedFieldName;
7894
}
7995

8096
/*
@@ -104,7 +120,7 @@ public boolean isScoreProperty() {
104120
return isScore;
105121
}
106122

107-
/*
123+
/*
108124
* (non-Javadoc)
109125
* @see org.springframework.data.mapping.model.AbstractPersistentProperty#isImmutable()
110126
*/

src/test/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentPropertyUnitTests.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,57 @@
1818
import static org.assertj.core.api.Assertions.*;
1919

2020
import org.junit.Test;
21+
import org.springframework.data.elasticsearch.annotations.Field;
2122
import org.springframework.data.elasticsearch.annotations.Score;
2223
import org.springframework.data.mapping.MappingException;
2324

2425
/**
2526
* Unit tests for {@link SimpleElasticsearchPersistentProperty}.
2627
*
2728
* @author Oliver Gierke
29+
* @author Peter-Josef Meisch
2830
*/
2931
public class SimpleElasticsearchPersistentPropertyUnitTests {
3032

33+
private final SimpleElasticsearchMappingContext context = new SimpleElasticsearchMappingContext();
34+
3135
@Test // DATAES-462
3236
public void rejectsScorePropertyOfTypeOtherthanFloat() {
3337

34-
SimpleElasticsearchMappingContext context = new SimpleElasticsearchMappingContext();
35-
3638
assertThatExceptionOfType(MappingException.class) //
3739
.isThrownBy(() -> context.getRequiredPersistentEntity(InvalidScoreProperty.class)) //
3840
.withMessageContaining("scoreProperty");
3941
}
4042

43+
@Test // DATAES-562
44+
public void fieldAnnotationWithNameSetsFieldname() {
45+
46+
final SimpleElasticsearchPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(FieldNameProperty.class);
47+
final ElasticsearchPersistentProperty persistentProperty = persistentEntity.getPersistentProperty("fieldProperty");
48+
49+
assertThat(persistentProperty).isNotNull();
50+
assertThat(persistentProperty.getFieldName()).isEqualTo("by-name");
51+
}
52+
53+
@Test // DATAES-562
54+
public void fieldAnnotationWithValueSetsFieldname() {
55+
56+
final SimpleElasticsearchPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(FieldValueProperty.class);
57+
final ElasticsearchPersistentProperty persistentProperty = persistentEntity.getPersistentProperty("fieldProperty");
58+
59+
assertThat(persistentProperty).isNotNull();
60+
assertThat(persistentProperty.getFieldName()).isEqualTo("by-value");
61+
}
62+
4163
static class InvalidScoreProperty {
4264
@Score String scoreProperty;
4365
}
66+
67+
static class FieldNameProperty {
68+
@Field(name = "by-name") String fieldProperty;
69+
}
70+
71+
static class FieldValueProperty {
72+
@Field(value = "by-value") String fieldProperty;
73+
}
4474
}

0 commit comments

Comments
 (0)