Skip to content

Commit 527f669

Browse files
committed
DATAES-503 - Added missing copy_to property to @field annotation.
Original pull request: spring-projects#227
1 parent dcf4cbe commit 527f669

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,6 @@
5757
String[] ignoreFields() default {};
5858

5959
boolean includeInParent() default false;
60+
61+
String[] copyTo() default {};
6062
}

src/main/java/org/springframework/data/elasticsearch/core/MappingBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class MappingBuilder {
6666
public static final String FIELD_NORMALIZER = "normalizer";
6767
public static final String FIELD_PROPERTIES = "properties";
6868
public static final String FIELD_PARENT = "_parent";
69+
public static final String FIELD_COPY_TO = "copy_to";
6970

7071
public static final String COMPLETION_PRESERVE_SEPARATORS = "preserve_separators";
7172
public static final String COMPLETION_PRESERVE_POSITION_INCREMENTS = "preserve_position_increments";
@@ -271,6 +272,7 @@ private static void addFieldMappingParameters(XContentBuilder builder, Object an
271272
String analyzer = null;
272273
String searchAnalyzer = null;
273274
String normalizer = null;
275+
String[] copyTo = null;
274276

275277
if (annotation instanceof Field) {
276278
// @Field
@@ -284,6 +286,7 @@ private static void addFieldMappingParameters(XContentBuilder builder, Object an
284286
analyzer = fieldAnnotation.analyzer();
285287
searchAnalyzer = fieldAnnotation.searchAnalyzer();
286288
normalizer = fieldAnnotation.normalizer();
289+
copyTo = fieldAnnotation.copyTo();
287290
} else if (annotation instanceof InnerField) {
288291
// @InnerField
289292
InnerField fieldAnnotation = (InnerField) annotation;
@@ -325,6 +328,9 @@ private static void addFieldMappingParameters(XContentBuilder builder, Object an
325328
if (!StringUtils.isEmpty(normalizer)) {
326329
builder.field(FIELD_NORMALIZER, normalizer);
327330
}
331+
if (copyTo != null && copyTo.length > 0) {
332+
builder.field(FIELD_COPY_TO, copyTo);
333+
}
328334
}
329335

330336
protected static boolean isEntity(java.lang.reflect.Field field) {

src/test/java/org/springframework/data/elasticsearch/core/MappingBuilderTests.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.ByteArrayOutputStream;
2525
import java.io.IOException;
2626
import java.math.BigDecimal;
27+
import java.util.Arrays;
2728
import java.util.Date;
2829
import java.util.List;
2930
import java.util.Map;
@@ -32,11 +33,21 @@
3233
import org.junit.Test;
3334
import org.junit.runner.RunWith;
3435
import org.springframework.beans.factory.annotation.Autowired;
36+
import org.springframework.data.elasticsearch.annotations.Field;
3537
import org.springframework.data.elasticsearch.builder.SampleInheritedEntityBuilder;
3638
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
3739
import org.springframework.data.elasticsearch.core.query.SearchQuery;
38-
import org.springframework.data.elasticsearch.entities.*;
40+
import org.springframework.data.elasticsearch.entities.Book;
41+
import org.springframework.data.elasticsearch.entities.CopyToEntity;
3942
import org.springframework.data.elasticsearch.entities.GeoEntity;
43+
import org.springframework.data.elasticsearch.entities.Group;
44+
import org.springframework.data.elasticsearch.entities.MinimalEntity;
45+
import org.springframework.data.elasticsearch.entities.NormalizerEntity;
46+
import org.springframework.data.elasticsearch.entities.SampleInheritedEntity;
47+
import org.springframework.data.elasticsearch.entities.SampleTransientEntity;
48+
import org.springframework.data.elasticsearch.entities.SimpleRecursiveEntity;
49+
import org.springframework.data.elasticsearch.entities.StockPrice;
50+
import org.springframework.data.elasticsearch.entities.User;
4051
import org.springframework.test.context.ContextConfiguration;
4152
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
4253

@@ -244,4 +255,24 @@ public void shouldUseKeywordNormalizer() throws IOException {
244255
assertThat(fieldDescriptionLowerCase.get("type"), equalTo("keyword"));
245256
assertThat(fieldDescriptionLowerCase.get("normalizer"), equalTo("lower_case_normalizer"));
246257
}
258+
259+
@Test // DATAES-503
260+
public void shouldUseCopyTo() throws IOException {
261+
262+
// given
263+
elasticsearchTemplate.deleteIndex(CopyToEntity.class);
264+
elasticsearchTemplate.createIndex(CopyToEntity.class);
265+
elasticsearchTemplate.putMapping(CopyToEntity.class);
266+
267+
// when
268+
Map mapping = elasticsearchTemplate.getMapping(CopyToEntity.class);
269+
Map properties = (Map) mapping.get("properties");
270+
Map fieldFirstName = (Map) properties.get("firstName");
271+
Map fieldLastName = (Map) properties.get("lastName");
272+
273+
// then
274+
List<String> copyToValue = Arrays.asList("name");
275+
assertThat(fieldFirstName.get("copy_to"), equalTo(copyToValue));
276+
assertThat(fieldLastName.get("copy_to"), equalTo(copyToValue));
277+
}
247278
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.entities;
17+
18+
import lombok.AllArgsConstructor;
19+
import lombok.Builder;
20+
import lombok.Getter;
21+
import lombok.NoArgsConstructor;
22+
import lombok.Setter;
23+
24+
import org.springframework.data.annotation.Id;
25+
import org.springframework.data.elasticsearch.annotations.Document;
26+
import org.springframework.data.elasticsearch.annotations.Field;
27+
import org.springframework.data.elasticsearch.annotations.FieldType;
28+
29+
/**
30+
* @author Sascha Woo
31+
*/
32+
@Setter
33+
@Getter
34+
@NoArgsConstructor
35+
@AllArgsConstructor
36+
@Builder
37+
@Document(indexName = "test-copy-to", type = "test", shards = 1, replicas = 0, refreshInterval = "-1")
38+
public class CopyToEntity {
39+
40+
@Id private String id;
41+
42+
@Field(type = FieldType.Keyword, copyTo = "name") private String firstName;
43+
44+
@Field(type = FieldType.Keyword, copyTo = "name") private String lastName;
45+
46+
@Field(type = FieldType.Keyword) private String name;
47+
}

0 commit comments

Comments
 (0)