DATAES-896 - Use mainField property of @MultiField annotation.

Original PR: #524
This commit is contained in:
Peter-Josef Meisch 2020-09-24 21:19:07 +02:00 committed by GitHub
parent 82c314206a
commit 6bd96dc4d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 11 deletions

View File

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

View File

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

View File

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