From 02761a48e01b9f3f0fb952cb13870cff84f2c2b6 Mon Sep 17 00:00:00 2001 From: xhaggi Date: Tue, 20 Nov 2018 11:18:20 +0100 Subject: [PATCH] DATAES-503 - Added missing copy_to property to @Field annotation. Original pull request: #227 --- .../data/elasticsearch/annotations/Field.java | 2 + .../elasticsearch/core/MappingBuilder.java | 6 +++ .../core/MappingBuilderTests.java | 33 ++++++++++++- .../elasticsearch/entities/CopyToEntity.java | 47 +++++++++++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/springframework/data/elasticsearch/entities/CopyToEntity.java 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 18a5a3fe3..8f139f74a 100644 --- a/src/main/java/org/springframework/data/elasticsearch/annotations/Field.java +++ b/src/main/java/org/springframework/data/elasticsearch/annotations/Field.java @@ -57,4 +57,6 @@ public @interface Field { String[] ignoreFields() default {}; boolean includeInParent() default false; + + String[] copyTo() default {}; } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/MappingBuilder.java b/src/main/java/org/springframework/data/elasticsearch/core/MappingBuilder.java index 6658bad44..7d059d570 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/MappingBuilder.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/MappingBuilder.java @@ -66,6 +66,7 @@ class MappingBuilder { public static final String FIELD_NORMALIZER = "normalizer"; public static final String FIELD_PROPERTIES = "properties"; public static final String FIELD_PARENT = "_parent"; + public static final String FIELD_COPY_TO = "copy_to"; public static final String COMPLETION_PRESERVE_SEPARATORS = "preserve_separators"; public static final String COMPLETION_PRESERVE_POSITION_INCREMENTS = "preserve_position_increments"; @@ -271,6 +272,7 @@ class MappingBuilder { String analyzer = null; String searchAnalyzer = null; String normalizer = null; + String[] copyTo = null; if (annotation instanceof Field) { // @Field @@ -284,6 +286,7 @@ class MappingBuilder { analyzer = fieldAnnotation.analyzer(); searchAnalyzer = fieldAnnotation.searchAnalyzer(); normalizer = fieldAnnotation.normalizer(); + copyTo = fieldAnnotation.copyTo(); } else if (annotation instanceof InnerField) { // @InnerField InnerField fieldAnnotation = (InnerField) annotation; @@ -325,6 +328,9 @@ class MappingBuilder { if (!StringUtils.isEmpty(normalizer)) { builder.field(FIELD_NORMALIZER, normalizer); } + if (copyTo != null && copyTo.length > 0) { + builder.field(FIELD_COPY_TO, copyTo); + } } protected static boolean isEntity(java.lang.reflect.Field field) { 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 3ed606667..637827c17 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/MappingBuilderTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/MappingBuilderTests.java @@ -23,6 +23,7 @@ import static org.springframework.data.elasticsearch.utils.IndexBuilder.*; import java.io.IOException; import java.math.BigDecimal; +import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Map; @@ -31,11 +32,21 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.builder.SampleInheritedEntityBuilder; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.data.elasticsearch.core.query.SearchQuery; -import org.springframework.data.elasticsearch.entities.*; +import org.springframework.data.elasticsearch.entities.Book; +import org.springframework.data.elasticsearch.entities.CopyToEntity; import org.springframework.data.elasticsearch.entities.GeoEntity; +import org.springframework.data.elasticsearch.entities.Group; +import org.springframework.data.elasticsearch.entities.MinimalEntity; +import org.springframework.data.elasticsearch.entities.NormalizerEntity; +import org.springframework.data.elasticsearch.entities.SampleInheritedEntity; +import org.springframework.data.elasticsearch.entities.SampleTransientEntity; +import org.springframework.data.elasticsearch.entities.SimpleRecursiveEntity; +import org.springframework.data.elasticsearch.entities.StockPrice; +import org.springframework.data.elasticsearch.entities.User; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -237,4 +248,24 @@ public class MappingBuilderTests { assertThat(fieldDescriptionLowerCase.get("type"), equalTo("keyword")); assertThat(fieldDescriptionLowerCase.get("normalizer"), equalTo("lower_case_normalizer")); } + + @Test // DATAES-503 + public void shouldUseCopyTo() throws IOException { + + // given + elasticsearchTemplate.deleteIndex(CopyToEntity.class); + elasticsearchTemplate.createIndex(CopyToEntity.class); + elasticsearchTemplate.putMapping(CopyToEntity.class); + + // when + Map mapping = elasticsearchTemplate.getMapping(CopyToEntity.class); + Map properties = (Map) mapping.get("properties"); + Map fieldFirstName = (Map) properties.get("firstName"); + Map fieldLastName = (Map) properties.get("lastName"); + + // then + List copyToValue = Arrays.asList("name"); + assertThat(fieldFirstName.get("copy_to"), equalTo(copyToValue)); + assertThat(fieldLastName.get("copy_to"), equalTo(copyToValue)); + } } diff --git a/src/test/java/org/springframework/data/elasticsearch/entities/CopyToEntity.java b/src/test/java/org/springframework/data/elasticsearch/entities/CopyToEntity.java new file mode 100644 index 000000000..d11551ec8 --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/entities/CopyToEntity.java @@ -0,0 +1,47 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.entities; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import org.springframework.data.annotation.Id; +import org.springframework.data.elasticsearch.annotations.Document; +import org.springframework.data.elasticsearch.annotations.Field; +import org.springframework.data.elasticsearch.annotations.FieldType; + +/** + * @author Sascha Woo + */ +@Setter +@Getter +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Document(indexName = "test-copy-to", type = "test", shards = 1, replicas = 0, refreshInterval = "-1") +public class CopyToEntity { + + @Id private String id; + + @Field(type = FieldType.Keyword, copyTo = "name") private String firstName; + + @Field(type = FieldType.Keyword, copyTo = "name") private String lastName; + + @Field(type = FieldType.Keyword) private String name; +}