mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-01 09:42:11 +00:00
DATAES-896 - Use mainField property of @MultiField annotation.
Original PR: #500
This commit is contained in:
parent
fd77f62cc4
commit
fd23c10c16
@ -29,7 +29,7 @@ import java.lang.annotation.Target;
|
|||||||
* @author Aleksei Arsenev
|
* @author Aleksei Arsenev
|
||||||
*/
|
*/
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target(ElementType.FIELD)
|
@Target(ElementType.ANNOTATION_TYPE)
|
||||||
public @interface InnerField {
|
public @interface InnerField {
|
||||||
|
|
||||||
String suffix();
|
String suffix();
|
||||||
|
@ -133,8 +133,8 @@ public class MappingBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void mapEntity(XContentBuilder builder, @Nullable ElasticsearchPersistentEntity entity, boolean isRootObject,
|
private void mapEntity(XContentBuilder builder, @Nullable ElasticsearchPersistentEntity<?> entity,
|
||||||
String nestedObjectFieldName, boolean nestedOrObjectField, FieldType fieldType,
|
boolean isRootObject, String nestedObjectFieldName, boolean nestedOrObjectField, FieldType fieldType,
|
||||||
@Nullable Field parentFieldAnnotation, @Nullable DynamicMapping dynamicMapping) throws IOException {
|
@Nullable Field parentFieldAnnotation, @Nullable DynamicMapping dynamicMapping) throws IOException {
|
||||||
|
|
||||||
boolean writeNestedProperties = !isRootObject && (isAnyPropertyAnnotatedWithField(entity) || nestedOrObjectField);
|
boolean writeNestedProperties = !isRootObject && (isAnyPropertyAnnotatedWithField(entity) || nestedOrObjectField);
|
||||||
@ -146,7 +146,7 @@ public class MappingBuilder {
|
|||||||
|
|
||||||
if (nestedOrObjectField && FieldType.Nested == fieldType && parentFieldAnnotation != null
|
if (nestedOrObjectField && FieldType.Nested == fieldType && parentFieldAnnotation != null
|
||||||
&& parentFieldAnnotation.includeInParent()) {
|
&& parentFieldAnnotation.includeInParent()) {
|
||||||
builder.field("include_in_parent", parentFieldAnnotation.includeInParent());
|
builder.field("include_in_parent", true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -396,7 +396,7 @@ public class MappingBuilder {
|
|||||||
MappingParameters mappingParameters = MappingParameters.from(annotation);
|
MappingParameters mappingParameters = MappingParameters.from(annotation);
|
||||||
|
|
||||||
if (!nestedOrObjectField && mappingParameters.isStore()) {
|
if (!nestedOrObjectField && mappingParameters.isStore()) {
|
||||||
builder.field(FIELD_PARAM_STORE, mappingParameters.isStore());
|
builder.field(FIELD_PARAM_STORE, true);
|
||||||
}
|
}
|
||||||
mappingParameters.writeTypeAndParametersTo(builder);
|
mappingParameters.writeTypeAndParametersTo(builder);
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@ import org.springframework.data.annotation.Id;
|
|||||||
import org.springframework.data.elasticsearch.annotations.DateFormat;
|
import org.springframework.data.elasticsearch.annotations.DateFormat;
|
||||||
import org.springframework.data.elasticsearch.annotations.Field;
|
import org.springframework.data.elasticsearch.annotations.Field;
|
||||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||||
|
import org.springframework.data.elasticsearch.annotations.MultiField;
|
||||||
import org.springframework.data.elasticsearch.annotations.Parent;
|
import org.springframework.data.elasticsearch.annotations.Parent;
|
||||||
import org.springframework.data.elasticsearch.annotations.Score;
|
import org.springframework.data.elasticsearch.annotations.Score;
|
||||||
import org.springframework.data.elasticsearch.core.convert.ElasticsearchDateConverter;
|
import org.springframework.data.elasticsearch.core.convert.ElasticsearchDateConverter;
|
||||||
@ -97,6 +98,10 @@ public class SimpleElasticsearchPersistentProperty extends
|
|||||||
throw new MappingException(String.format("Parent property %s must be of type String!", property.getName()));
|
throw new MappingException(String.format("Parent property %s must be of type String!", property.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isAnnotationPresent(Field.class) && isAnnotationPresent(MultiField.class)) {
|
||||||
|
throw new MappingException("@Field annotation must not be used on a @MultiField property.");
|
||||||
|
}
|
||||||
|
|
||||||
initDateConverter();
|
initDateConverter();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,13 +187,15 @@ public class SimpleElasticsearchPersistentProperty extends
|
|||||||
@Nullable
|
@Nullable
|
||||||
private String getAnnotatedFieldName() {
|
private String getAnnotatedFieldName() {
|
||||||
|
|
||||||
if (isAnnotationPresent(Field.class)) {
|
String name = null;
|
||||||
|
|
||||||
String name = findAnnotation(Field.class).name();
|
if (isAnnotationPresent(Field.class)) {
|
||||||
return StringUtils.hasText(name) ? name : null;
|
name = findAnnotation(Field.class).name();
|
||||||
|
} else if (isAnnotationPresent(MultiField.class)) {
|
||||||
|
name = findAnnotation(MultiField.class).mainField().name();
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return StringUtils.hasText(name) ? name : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -42,6 +42,7 @@ import java.util.HashMap;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.assertj.core.data.Percentage;
|
import org.assertj.core.data.Percentage;
|
||||||
@ -255,6 +256,7 @@ public class MappingBuilderTests extends MappingContextBaseTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-420
|
@Test // DATAES-420
|
||||||
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
public void shouldUseBothAnalyzer() {
|
public void shouldUseBothAnalyzer() {
|
||||||
|
|
||||||
// given
|
// given
|
||||||
@ -277,6 +279,7 @@ public class MappingBuilderTests extends MappingContextBaseTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-492
|
@Test // DATAES-492
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
public void shouldUseKeywordNormalizer() {
|
public void shouldUseKeywordNormalizer() {
|
||||||
|
|
||||||
// given
|
// given
|
||||||
@ -297,6 +300,7 @@ public class MappingBuilderTests extends MappingContextBaseTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-503
|
@Test // DATAES-503
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
public void shouldUseCopyTo() {
|
public void shouldUseCopyTo() {
|
||||||
|
|
||||||
// given
|
// given
|
||||||
@ -400,12 +404,29 @@ public class MappingBuilderTests extends MappingContextBaseTests {
|
|||||||
assertEquals(expected, mapping, false);
|
assertEquals(expected, mapping, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-568
|
@Test // DATAES-568, DATAES-896
|
||||||
public void shouldUseFieldNameOnMultiField() throws JSONException {
|
public void shouldUseFieldNameOnMultiField() throws JSONException {
|
||||||
|
|
||||||
// given
|
// given
|
||||||
String expected = "{\"properties\":{" + "\"id-property\":{\"type\":\"keyword\",\"index\":true},"
|
String expected = "{\n" + //
|
||||||
+ "\"multifield-property\":{\"type\":\"text\",\"analyzer\":\"whitespace\",\"fields\":{\"prefix\":{\"type\":\"text\",\"analyzer\":\"stop\",\"search_analyzer\":\"standard\"}}}}}";
|
" \"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"; //
|
||||||
|
|
||||||
// when
|
// when
|
||||||
String mapping = getMappingBuilder().buildPropertyMapping(FieldNameEntity.MultiFieldEntity.class);
|
String mapping = getMappingBuilder().buildPropertyMapping(FieldNameEntity.MultiFieldEntity.class);
|
||||||
@ -651,9 +672,10 @@ public class MappingBuilderTests extends MappingContextBaseTests {
|
|||||||
|
|
||||||
@Nullable @Id @Field("id-property") private String id;
|
@Nullable @Id @Field("id-property") private String id;
|
||||||
|
|
||||||
@Nullable @Field("multifield-property") //
|
@Nullable //
|
||||||
@MultiField(mainField = @Field(type = FieldType.Text, analyzer = "whitespace"), otherFields = {
|
@MultiField(mainField = @Field(name = "main-field", type = FieldType.Text, analyzer = "whitespace"),
|
||||||
@InnerField(suffix = "prefix", type = FieldType.Text, analyzer = "stop", searchAnalyzer = "standard") }) //
|
otherFields = {
|
||||||
|
@InnerField(suffix = "suff-ix", type = FieldType.Text, analyzer = "stop", searchAnalyzer = "standard") }) //
|
||||||
private String description;
|
private String description;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -684,6 +706,7 @@ public class MappingBuilderTests extends MappingContextBaseTests {
|
|||||||
* @author Stuart Stevenson
|
* @author Stuart Stevenson
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
*/
|
*/
|
||||||
|
@Data
|
||||||
@Document(indexName = "test-index-simple-recursive-mapping-builder", replicas = 0, refreshInterval = "-1")
|
@Document(indexName = "test-index-simple-recursive-mapping-builder", replicas = 0, refreshInterval = "-1")
|
||||||
static class SimpleRecursiveEntity {
|
static class SimpleRecursiveEntity {
|
||||||
|
|
||||||
@ -783,7 +806,7 @@ public class MappingBuilderTests extends MappingContextBaseTests {
|
|||||||
*/
|
*/
|
||||||
static class SampleInheritedEntityBuilder {
|
static class SampleInheritedEntityBuilder {
|
||||||
|
|
||||||
private SampleInheritedEntity result;
|
private final SampleInheritedEntity result;
|
||||||
|
|
||||||
public SampleInheritedEntityBuilder(String id) {
|
public SampleInheritedEntityBuilder(String id) {
|
||||||
result = new SampleInheritedEntity();
|
result = new SampleInheritedEntity();
|
||||||
@ -806,7 +829,7 @@ public class MappingBuilderTests extends MappingContextBaseTests {
|
|||||||
|
|
||||||
public IndexQuery buildIndex() {
|
public IndexQuery buildIndex() {
|
||||||
IndexQuery indexQuery = new IndexQuery();
|
IndexQuery indexQuery = new IndexQuery();
|
||||||
indexQuery.setId(result.getId());
|
indexQuery.setId(Objects.requireNonNull(result.getId()));
|
||||||
indexQuery.setObject(result);
|
indexQuery.setObject(result);
|
||||||
return indexQuery;
|
return indexQuery;
|
||||||
}
|
}
|
||||||
@ -1030,7 +1053,7 @@ public class MappingBuilderTests extends MappingContextBaseTests {
|
|||||||
|
|
||||||
@Document(indexName = "valueDoc")
|
@Document(indexName = "valueDoc")
|
||||||
static class ValueDoc {
|
static class ValueDoc {
|
||||||
@Field(type = Text) private ValueObject valueObject;
|
@Nullable @Field(type = Text) private ValueObject valueObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.springframework.data.elasticsearch.annotations.Field;
|
import org.springframework.data.elasticsearch.annotations.Field;
|
||||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||||
import org.springframework.data.elasticsearch.annotations.InnerField;
|
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.elasticsearch.annotations.Score;
|
||||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
@ -31,9 +32,10 @@ public class MappingParametersTest extends MappingContextBaseTests {
|
|||||||
|
|
||||||
@Test // DATAES-621
|
@Test // DATAES-621
|
||||||
public void shouldCreateParametersForInnerFieldAnnotation() {
|
public void shouldCreateParametersForInnerFieldAnnotation() {
|
||||||
Annotation annotation = entity.getRequiredPersistentProperty("innerField").findAnnotation(InnerField.class);
|
|
||||||
|
|
||||||
MappingParameters mappingParameters = MappingParameters.from(annotation);
|
MultiField multiField = entity.getRequiredPersistentProperty("mainField").findAnnotation(MultiField.class);
|
||||||
|
InnerField innerField = multiField.otherFields()[0];
|
||||||
|
MappingParameters mappingParameters = MappingParameters.from(innerField);
|
||||||
|
|
||||||
assertThat(mappingParameters).isNotNull();
|
assertThat(mappingParameters).isNotNull();
|
||||||
}
|
}
|
||||||
@ -61,7 +63,8 @@ public class MappingParametersTest extends MappingContextBaseTests {
|
|||||||
|
|
||||||
static class AnnotatedClass {
|
static class AnnotatedClass {
|
||||||
@Nullable @Field private String field;
|
@Nullable @Field private String field;
|
||||||
@Nullable @InnerField(suffix = "test", type = FieldType.Text) private String innerField;
|
@Nullable @MultiField(mainField = @Field,
|
||||||
|
otherFields = { @InnerField(suffix = "test", type = FieldType.Text) }) private String mainField;
|
||||||
@Score private float score;
|
@Score private float score;
|
||||||
@Nullable @Field(type = FieldType.Text, docValues = false) private String docValuesText;
|
@Nullable @Field(type = FieldType.Text, docValues = false) private String docValuesText;
|
||||||
@Nullable @Field(type = FieldType.Nested, docValues = false) private String docValuesNested;
|
@Nullable @Field(type = FieldType.Nested, docValues = false) private String docValuesNested;
|
||||||
|
@ -28,6 +28,8 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.springframework.data.elasticsearch.annotations.DateFormat;
|
import org.springframework.data.elasticsearch.annotations.DateFormat;
|
||||||
import org.springframework.data.elasticsearch.annotations.Field;
|
import org.springframework.data.elasticsearch.annotations.Field;
|
||||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
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.elasticsearch.annotations.Score;
|
||||||
import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm;
|
import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm;
|
||||||
import org.springframework.data.mapping.MappingException;
|
import org.springframework.data.mapping.MappingException;
|
||||||
@ -74,6 +76,16 @@ public class SimpleElasticsearchPersistentPropertyUnitTests {
|
|||||||
assertThat(persistentProperty.getFieldName()).isEqualTo("by-value");
|
assertThat(persistentProperty.getFieldName()).isEqualTo("by-value");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // DATAES-896
|
||||||
|
void shouldUseNameFromMultiFieldMainField() {
|
||||||
|
SimpleElasticsearchPersistentEntity<?> persistentEntity = context
|
||||||
|
.getRequiredPersistentEntity(MultiFieldProperty.class);
|
||||||
|
ElasticsearchPersistentProperty persistentProperty = persistentEntity.getPersistentProperty("mainfieldProperty");
|
||||||
|
|
||||||
|
assertThat(persistentProperty).isNotNull();
|
||||||
|
assertThat(persistentProperty.getFieldName()).isEqualTo("mainfield");
|
||||||
|
}
|
||||||
|
|
||||||
@Test // DATAES-716, DATAES-792
|
@Test // DATAES-716, DATAES-792
|
||||||
void shouldSetPropertyConverters() {
|
void shouldSetPropertyConverters() {
|
||||||
SimpleElasticsearchPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(DatesProperty.class);
|
SimpleElasticsearchPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(DatesProperty.class);
|
||||||
@ -199,6 +211,11 @@ public class SimpleElasticsearchPersistentPropertyUnitTests {
|
|||||||
@Nullable @Field(value = "by-value") String fieldProperty;
|
@Nullable @Field(value = "by-value") String fieldProperty;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class MultiFieldProperty {
|
||||||
|
@Nullable @MultiField(mainField = @Field("mainfield"),
|
||||||
|
otherFields = { @InnerField(suffix = "suff", type = FieldType.Keyword) }) String mainfieldProperty;
|
||||||
|
}
|
||||||
|
|
||||||
static class DatesProperty {
|
static class DatesProperty {
|
||||||
@Nullable @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "dd.MM.uuuu") LocalDate localDate;
|
@Nullable @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "dd.MM.uuuu") LocalDate localDate;
|
||||||
@Nullable @Field(type = FieldType.Date, format = DateFormat.basic_date_time) LocalDateTime localDateTime;
|
@Nullable @Field(type = FieldType.Date, format = DateFormat.basic_date_time) LocalDateTime localDateTime;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user