mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-15 16:42:11 +00:00
DATAES-896 - Use mainField property of @MultiField annotation.
Original PR: #524
This commit is contained in:
parent
82c314206a
commit
6bd96dc4d8
@ -19,6 +19,7 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.data.elasticsearch.annotations.Field;
|
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.Parent;
|
||||||
import org.springframework.data.elasticsearch.annotations.Score;
|
import org.springframework.data.elasticsearch.annotations.Score;
|
||||||
import org.springframework.data.mapping.Association;
|
import org.springframework.data.mapping.Association;
|
||||||
@ -77,13 +78,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(MultiField.class)) {
|
||||||
return StringUtils.hasText(name) ? name : null;
|
name = findAnnotation(MultiField.class).mainField().name();
|
||||||
|
} else if (isAnnotationPresent(Field.class)) {
|
||||||
|
name = findAnnotation(Field.class).name();
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return StringUtils.hasText(name) ? name : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -394,13 +394,31 @@ 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 IOException, JSONException {
|
public void shouldUseFieldNameOnMultiField() throws IOException, JSONException {
|
||||||
|
|
||||||
// given
|
// given
|
||||||
String expected = "{\"fieldname-type\":{\"properties\":{" + "\"id-property\":{\"type\":\"keyword\",\"index\":true},"
|
String expected = "{\n" + //
|
||||||
+ "\"multifield-property\":{\"store\":false,\"type\":\"text\",\"analyzer\":\"whitespace\",\"fields\":{\"prefix\":{\"store\":false,\"type\":\"text\",\"analyzer\":\"stop\",\"search_analyzer\":\"standard\"}}}"
|
" \"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
|
// when
|
||||||
String mapping = getMappingBuilder().buildPropertyMapping(FieldNameEntity.MultiFieldEntity.class);
|
String mapping = getMappingBuilder().buildPropertyMapping(FieldNameEntity.MultiFieldEntity.class);
|
||||||
@ -470,9 +488,9 @@ public class MappingBuilderTests extends MappingContextBaseTests {
|
|||||||
@Id @Field("id-property") private String id;
|
@Id @Field("id-property") private String id;
|
||||||
|
|
||||||
@Field("multifield-property") //
|
@Field("multifield-property") //
|
||||||
@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",
|
||||||
private String description;
|
searchAnalyzer = "standard") }) private String description;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,8 +19,12 @@ import static org.assertj.core.api.Assertions.*;
|
|||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.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.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.mapping.MappingException;
|
import org.springframework.data.mapping.MappingException;
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for {@link SimpleElasticsearchPersistentProperty}.
|
* Unit tests for {@link SimpleElasticsearchPersistentProperty}.
|
||||||
@ -62,6 +66,16 @@ public class SimpleElasticsearchPersistentPropertyUnitTests {
|
|||||||
assertThat(persistentProperty.getFieldName()).isEqualTo("by-value");
|
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 {
|
static class InvalidScoreProperty {
|
||||||
@Score String scoreProperty;
|
@Score String scoreProperty;
|
||||||
}
|
}
|
||||||
@ -73,4 +87,9 @@ public class SimpleElasticsearchPersistentPropertyUnitTests {
|
|||||||
static class FieldValueProperty {
|
static class FieldValueProperty {
|
||||||
@Field(value = "by-value") String fieldProperty;
|
@Field(value = "by-value") String fieldProperty;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class MultiFieldProperty {
|
||||||
|
@Nullable @MultiField(mainField = @Field("mainfield"),
|
||||||
|
otherFields = { @InnerField(suffix = "suff", type = FieldType.Keyword) }) String mainfieldProperty;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user