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 b3addbcc2..3346aaf57 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 @@ -19,6 +19,7 @@ import java.util.Arrays; import java.util.List; import org.springframework.data.elasticsearch.annotations.Field; +import org.springframework.data.elasticsearch.annotations.MultiField; import org.springframework.data.elasticsearch.annotations.Parent; import org.springframework.data.elasticsearch.annotations.Score; import org.springframework.data.mapping.Association; @@ -77,13 +78,15 @@ public class SimpleElasticsearchPersistentProperty extends @Nullable private String getAnnotatedFieldName() { - if (isAnnotationPresent(Field.class)) { + String name = null; - String name = findAnnotation(Field.class).name(); - return StringUtils.hasText(name) ? name : null; + if (isAnnotationPresent(MultiField.class)) { + name = findAnnotation(MultiField.class).mainField().name(); + } else if (isAnnotationPresent(Field.class)) { + name = findAnnotation(Field.class).name(); } - return null; + return StringUtils.hasText(name) ? name : null; } /* diff --git a/src/test/java/org/springframework/data/elasticsearch/core/MappingBuilderTests.java b/src/test/java/org/springframework/data/elasticsearch/core/MappingBuilderTests.java index f528a0bbd..5a79ba305 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/MappingBuilderTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/MappingBuilderTests.java @@ -394,13 +394,31 @@ public class MappingBuilderTests extends MappingContextBaseTests { assertEquals(expected, mapping, false); } - @Test // DATAES-568 + @Test // DATAES-568, DATAES-896 public void shouldUseFieldNameOnMultiField() throws IOException, JSONException { // given - String expected = "{\"fieldname-type\":{\"properties\":{" + "\"id-property\":{\"type\":\"keyword\",\"index\":true}," - + "\"multifield-property\":{\"store\":false,\"type\":\"text\",\"analyzer\":\"whitespace\",\"fields\":{\"prefix\":{\"store\":false,\"type\":\"text\",\"analyzer\":\"stop\",\"search_analyzer\":\"standard\"}}}" - + "}}}"; + String expected = "{\n" + // + " \"fieldname-type\": {\n" + // + " \"properties\": {\n" + // + " \"id-property\": {\n" + // + " \"type\": \"keyword\",\n" + // + " \"index\": true\n" + // + " },\n" + // + " \"main-field\": {\n" + // + " \"type\": \"text\",\n" + // + " \"analyzer\": \"whitespace\",\n" + // + " \"fields\": {\n" + // + " \"suff-ix\": {\n" + // + " \"type\": \"text\",\n" + // + " \"analyzer\": \"stop\",\n" + // + " \"search_analyzer\": \"standard\"\n" + // + " }\n" + // + " }\n" + // + " }\n" + // + " }\n" + // + " }\n" + // + "}\n"; // // when String mapping = getMappingBuilder().buildPropertyMapping(FieldNameEntity.MultiFieldEntity.class); @@ -470,9 +488,9 @@ public class MappingBuilderTests extends MappingContextBaseTests { @Id @Field("id-property") private String id; @Field("multifield-property") // - @MultiField(mainField = @Field(type = FieldType.Text, analyzer = "whitespace"), otherFields = { - @InnerField(suffix = "prefix", type = FieldType.Text, analyzer = "stop", searchAnalyzer = "standard") }) // - private String description; + @MultiField(mainField = @Field(name = "main-field", type = FieldType.Text, analyzer = "whitespace"), + otherFields = { @InnerField(suffix = "suff-ix", type = FieldType.Text, analyzer = "stop", + searchAnalyzer = "standard") }) private String description; } } 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 50bc04493..3c3e3a398 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 @@ -19,8 +19,12 @@ import static org.assertj.core.api.Assertions.*; import org.junit.Test; import org.springframework.data.elasticsearch.annotations.Field; +import org.springframework.data.elasticsearch.annotations.FieldType; +import org.springframework.data.elasticsearch.annotations.InnerField; +import org.springframework.data.elasticsearch.annotations.MultiField; import org.springframework.data.elasticsearch.annotations.Score; import org.springframework.data.mapping.MappingException; +import org.springframework.lang.Nullable; /** * Unit tests for {@link SimpleElasticsearchPersistentProperty}. @@ -62,6 +66,16 @@ public class SimpleElasticsearchPersistentPropertyUnitTests { assertThat(persistentProperty.getFieldName()).isEqualTo("by-value"); } + @Test // DATAES-896 + public void shouldUseNameFromMultiFieldMainField() { + SimpleElasticsearchPersistentEntity persistentEntity = context + .getRequiredPersistentEntity(MultiFieldProperty.class); + ElasticsearchPersistentProperty persistentProperty = persistentEntity.getPersistentProperty("mainfieldProperty"); + + assertThat(persistentProperty).isNotNull(); + assertThat(persistentProperty.getFieldName()).isEqualTo("mainfield"); + } + static class InvalidScoreProperty { @Score String scoreProperty; } @@ -73,4 +87,9 @@ public class SimpleElasticsearchPersistentPropertyUnitTests { static class FieldValueProperty { @Field(value = "by-value") String fieldProperty; } + + static class MultiFieldProperty { + @Nullable @MultiField(mainField = @Field("mainfield"), + otherFields = { @InnerField(suffix = "suff", type = FieldType.Keyword) }) String mainfieldProperty; + } }