DATAES-503 - Added missing copy_to property to @Field annotation.

Original pull request: #227
This commit is contained in:
xhaggi 2018-11-20 11:18:20 +01:00
parent e6fbc37550
commit 02761a48e0
4 changed files with 87 additions and 1 deletions

View File

@ -57,4 +57,6 @@ public @interface Field {
String[] ignoreFields() default {}; String[] ignoreFields() default {};
boolean includeInParent() default false; boolean includeInParent() default false;
String[] copyTo() default {};
} }

View File

@ -66,6 +66,7 @@ class MappingBuilder {
public static final String FIELD_NORMALIZER = "normalizer"; public static final String FIELD_NORMALIZER = "normalizer";
public static final String FIELD_PROPERTIES = "properties"; public static final String FIELD_PROPERTIES = "properties";
public static final String FIELD_PARENT = "_parent"; 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_SEPARATORS = "preserve_separators";
public static final String COMPLETION_PRESERVE_POSITION_INCREMENTS = "preserve_position_increments"; public static final String COMPLETION_PRESERVE_POSITION_INCREMENTS = "preserve_position_increments";
@ -271,6 +272,7 @@ class MappingBuilder {
String analyzer = null; String analyzer = null;
String searchAnalyzer = null; String searchAnalyzer = null;
String normalizer = null; String normalizer = null;
String[] copyTo = null;
if (annotation instanceof Field) { if (annotation instanceof Field) {
// @Field // @Field
@ -284,6 +286,7 @@ class MappingBuilder {
analyzer = fieldAnnotation.analyzer(); analyzer = fieldAnnotation.analyzer();
searchAnalyzer = fieldAnnotation.searchAnalyzer(); searchAnalyzer = fieldAnnotation.searchAnalyzer();
normalizer = fieldAnnotation.normalizer(); normalizer = fieldAnnotation.normalizer();
copyTo = fieldAnnotation.copyTo();
} else if (annotation instanceof InnerField) { } else if (annotation instanceof InnerField) {
// @InnerField // @InnerField
InnerField fieldAnnotation = (InnerField) annotation; InnerField fieldAnnotation = (InnerField) annotation;
@ -325,6 +328,9 @@ class MappingBuilder {
if (!StringUtils.isEmpty(normalizer)) { if (!StringUtils.isEmpty(normalizer)) {
builder.field(FIELD_NORMALIZER, 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) { protected static boolean isEntity(java.lang.reflect.Field field) {

View File

@ -23,6 +23,7 @@ import static org.springframework.data.elasticsearch.utils.IndexBuilder.*;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -31,11 +32,21 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; 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.builder.SampleInheritedEntityBuilder;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery; 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.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.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -237,4 +248,24 @@ public class MappingBuilderTests {
assertThat(fieldDescriptionLowerCase.get("type"), equalTo("keyword")); assertThat(fieldDescriptionLowerCase.get("type"), equalTo("keyword"));
assertThat(fieldDescriptionLowerCase.get("normalizer"), equalTo("lower_case_normalizer")); 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<String> copyToValue = Arrays.asList("name");
assertThat(fieldFirstName.get("copy_to"), equalTo(copyToValue));
assertThat(fieldLastName.get("copy_to"), equalTo(copyToValue));
}
} }

View File

@ -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;
}