Skip to content

Commit a42de9b

Browse files
authored
DATAES-362 - Add support for composable meta annotations.
Original PR: spring-projects#566
1 parent 6edb835 commit a42de9b

File tree

7 files changed

+229
-25
lines changed

7 files changed

+229
-25
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @author Aleksei Arsenev
3737
*/
3838
@Retention(RetentionPolicy.RUNTIME)
39-
@Target(ElementType.FIELD)
39+
@Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE })
4040
@Documented
4141
@Inherited
4242
public @interface Field {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
* @author Artur Konczak
2828
* @author Jonathan Yan
2929
* @author Xiao Yu
30+
* @author Peter-Josef Meisch
3031
*/
3132
@Retention(RetentionPolicy.RUNTIME)
32-
@Target(ElementType.FIELD)
33+
@Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE })
3334
@Documented
3435
public @interface MultiField {
3536

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.elasticsearch.cluster.metadata.AliasMetadata;
2626
import org.slf4j.Logger;
2727
import org.slf4j.LoggerFactory;
28+
import org.springframework.core.annotation.AnnotatedElementUtils;
29+
import org.springframework.core.annotation.AnnotationAttributes;
2830
import org.springframework.dao.InvalidDataAccessApiUsageException;
2931
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
3032
import org.springframework.data.elasticsearch.annotations.Mapping;
@@ -105,9 +107,13 @@ public Document createSettings(Class<?> clazz) {
105107

106108
Document settings = null;
107109

108-
if (clazz.isAnnotationPresent(Setting.class)) {
109-
String settingPath = clazz.getAnnotation(Setting.class).settingPath();
110-
settings = loadSettings(settingPath);
110+
if (AnnotatedElementUtils.hasAnnotation(clazz, Setting.class)) {
111+
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(clazz, Setting.class);
112+
113+
if (attributes != null) {
114+
String settingPath = attributes.getString("settingPath");
115+
settings = loadSettings(settingPath);
116+
}
111117
}
112118

113119
if (settings == null) {
@@ -224,22 +230,28 @@ public Document createMapping(Class<?> clazz) {
224230
protected Document buildMapping(Class<?> clazz) {
225231

226232
// load mapping specified in Mapping annotation if present
227-
if (clazz.isAnnotationPresent(Mapping.class)) {
228-
String mappingPath = clazz.getAnnotation(Mapping.class).mappingPath();
233+
if (AnnotatedElementUtils.hasAnnotation(clazz, Mapping.class)) {
234+
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(clazz, Mapping.class);
229235

230-
if (!StringUtils.isEmpty(mappingPath)) {
231-
String mappings = ResourceUtil.readFileFromClasspath(mappingPath);
236+
if (attributes != null) {
237+
String mappingPath = attributes.getString("mappingPath");
232238

233-
if (!StringUtils.isEmpty(mappings)) {
234-
return Document.parse(mappings);
239+
if (StringUtils.hasText(mappingPath)) {
240+
String mappings = ResourceUtil.readFileFromClasspath(mappingPath);
241+
242+
if (StringUtils.hasText(mappings)) {
243+
return Document.parse(mappings);
244+
}
245+
} else {
246+
LOGGER.info("mappingPath in @Mapping has to be defined. Building mappings using @Field");
235247
}
236-
} else {
237-
LOGGER.info("mappingPath in @Mapping has to be defined. Building mappings using @Field");
238248
}
239249
}
240250

241251
// build mapping from field annotations
242-
try {
252+
try
253+
254+
{
243255
String mapping = new MappingBuilder(elasticsearchConverter).buildPropertyMapping(clazz);
244256
return Document.parse(mapping);
245257
} catch (Exception e) {

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
3838
import org.slf4j.Logger;
3939
import org.slf4j.LoggerFactory;
40+
import org.springframework.core.annotation.AnnotatedElementUtils;
41+
import org.springframework.core.annotation.AnnotationAttributes;
4042
import org.springframework.dao.InvalidDataAccessApiUsageException;
4143
import org.springframework.data.elasticsearch.NoSuchIndexException;
4244
import org.springframework.data.elasticsearch.annotations.Mapping;
@@ -157,9 +159,13 @@ public Mono<Document> createMapping() {
157159
@Override
158160
public Mono<Document> createMapping(Class<?> clazz) {
159161

160-
if (clazz.isAnnotationPresent(Mapping.class)) {
161-
String mappingPath = clazz.getAnnotation(Mapping.class).mappingPath();
162-
return loadDocument(mappingPath, "@Mapping");
162+
if (AnnotatedElementUtils.hasAnnotation(clazz, Mapping.class)) {
163+
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(clazz, Mapping.class);
164+
165+
if (attributes != null) {
166+
String mappingPath = clazz.getAnnotation(Mapping.class).mappingPath();
167+
return loadDocument(mappingPath, "@Mapping");
168+
}
163169
}
164170

165171
String mapping = new MappingBuilder(converter).buildPropertyMapping(clazz);
@@ -198,10 +204,13 @@ public Mono<Document> createSettings() {
198204
@Override
199205
public Mono<Document> createSettings(Class<?> clazz) {
200206

201-
if (clazz.isAnnotationPresent(Setting.class)) {
202-
String settingPath = clazz.getAnnotation(Setting.class).settingPath();
207+
if (AnnotatedElementUtils.hasAnnotation(clazz, Setting.class)) {
208+
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(clazz, Setting.class);
203209

204-
return loadDocument(settingPath, "@Setting");
210+
if (attributes != null) {
211+
String settingPath = attributes.getString("settingPath");
212+
return loadDocument(settingPath, "@Setting");
213+
}
205214
}
206215

207216
return Mono.just(getRequiredPersistentEntity(clazz).getDefaultSettings());

src/main/java/org/springframework/data/elasticsearch/core/mapping/IndexCoordinates.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ public String[] getIndexNames() {
5252
return Arrays.copyOf(indexNames, indexNames.length);
5353
}
5454

55+
/**
56+
* @since 4.2
57+
*/
58+
@Override
59+
public boolean equals(Object o) {
60+
if (this == o) return true;
61+
if (o == null || getClass() != o.getClass()) return false;
62+
IndexCoordinates that = (IndexCoordinates) o;
63+
return Arrays.equals(indexNames, that.indexNames);
64+
}
65+
66+
/**
67+
* @since 4.2
68+
*/
69+
@Override
70+
public int hashCode() {
71+
return Arrays.hashCode(indexNames);
72+
}
73+
5574
@Override
5675
public String toString() {
5776
return "IndexCoordinates{" + "indexNames=" + Arrays.toString(indexNames) + '}';

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntity.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.index.VersionType;
2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
26+
import org.springframework.core.annotation.AnnotatedElementUtils;
2627
import org.springframework.data.elasticsearch.annotations.Parent;
2728
import org.springframework.data.elasticsearch.annotations.Setting;
2829
import org.springframework.data.elasticsearch.core.document.Document;
@@ -87,9 +88,11 @@ public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
8788
super(typeInformation);
8889

8990
Class<T> clazz = typeInformation.getType();
90-
if (clazz.isAnnotationPresent(org.springframework.data.elasticsearch.annotations.Document.class)) {
91-
org.springframework.data.elasticsearch.annotations.Document document = clazz
92-
.getAnnotation(org.springframework.data.elasticsearch.annotations.Document.class);
91+
org.springframework.data.elasticsearch.annotations.Document document = AnnotatedElementUtils
92+
.findMergedAnnotation(clazz, org.springframework.data.elasticsearch.annotations.Document.class);
93+
94+
if (document != null) {
95+
9396
Assert.hasText(document.indexName(),
9497
" Unknown indexName. Make sure the indexName is defined. e.g @Document(indexName=\"foo\")");
9598
this.indexName = document.indexName();
@@ -101,8 +104,11 @@ public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
101104
this.versionType = document.versionType();
102105
this.createIndexAndMapping = document.createIndex();
103106
}
104-
if (clazz.isAnnotationPresent(Setting.class)) {
105-
this.settingPath = typeInformation.getType().getAnnotation(Setting.class).settingPath();
107+
108+
Setting setting = AnnotatedElementUtils.getMergedAnnotation(clazz, Setting.class);
109+
110+
if (setting != null) {
111+
this.settingPath = setting.settingPath();
106112
}
107113
}
108114

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* Copyright 2020 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+
* https://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.annotations;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.skyscreamer.jsonassert.JSONAssert.*;
20+
21+
import lombok.AllArgsConstructor;
22+
import lombok.Data;
23+
import lombok.NoArgsConstructor;
24+
25+
import java.lang.annotation.Documented;
26+
import java.lang.annotation.ElementType;
27+
import java.lang.annotation.Inherited;
28+
import java.lang.annotation.Retention;
29+
import java.lang.annotation.RetentionPolicy;
30+
import java.lang.annotation.Target;
31+
import java.time.LocalDate;
32+
33+
import org.json.JSONException;
34+
import org.junit.jupiter.api.DisplayName;
35+
import org.junit.jupiter.api.Test;
36+
import org.springframework.core.annotation.AliasFor;
37+
import org.springframework.data.annotation.Id;
38+
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
39+
import org.springframework.data.elasticsearch.core.index.MappingBuilder;
40+
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
41+
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
42+
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
43+
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchPersistentEntity;
44+
45+
/**
46+
* @author Peter-Josef Meisch
47+
*/
48+
public class ComposableAnnotationsUnitTest {
49+
50+
private static SimpleElasticsearchMappingContext mappingContext = new SimpleElasticsearchMappingContext();
51+
private static MappingElasticsearchConverter converter = new MappingElasticsearchConverter(mappingContext);
52+
private static MappingBuilder mappingBuilder = new MappingBuilder(converter);
53+
54+
@Test // DATAES-362
55+
@DisplayName("Document annotation should be composable")
56+
void documentAnnotationShouldBeComposable() {
57+
58+
SimpleElasticsearchPersistentEntity<?> entity = mappingContext
59+
.getRequiredPersistentEntity(ComposedAnnotationEntity.class);
60+
61+
assertThat(entity.getIndexCoordinates()).isEqualTo(IndexCoordinates.of("test-no-create"));
62+
assertThat(entity.isCreateIndexAndMapping()).isFalse();
63+
assertThat(entity.getShards()).isEqualTo((short) 42);
64+
}
65+
66+
@Test // DATAES-362
67+
@DisplayName("Field annotation should be composable")
68+
void fieldAnnotationShouldBeComposable() {
69+
SimpleElasticsearchPersistentEntity<?> entity = mappingContext
70+
.getRequiredPersistentEntity(ComposedAnnotationEntity.class);
71+
72+
ElasticsearchPersistentProperty property = entity.getRequiredPersistentProperty("nullValue");
73+
74+
assertThat(property.getFieldName()).isEqualTo("null-value");
75+
assertThat(property.storeNullValue()).isTrue();
76+
}
77+
78+
@Test // DATAES-362
79+
@DisplayName("should use composed Field annotations in MappingBuilder")
80+
void shouldUseComposedFieldAnnotationsInMappingBuilder() throws JSONException {
81+
82+
String expected = "{\n" + //
83+
" \"properties\":{\n" + //
84+
" \"null-value\": {\n" + //
85+
" \"null_value\": \"NULL\"\n" + //
86+
" },\n" + //
87+
" \"theDate\": {\n" + //
88+
" \"type\": \"date\",\n" + //
89+
" \"format\": \"date\"\n" + //
90+
" },\n" + //
91+
" \"multiField\": {\n" + //
92+
" \"type\": \"text\",\n" + //
93+
" \"fields\": {\n" + //
94+
" \"keyword\": {\n" + //
95+
" \"type\": \"keyword\"\n" + //
96+
" }\n" + //
97+
" }\n" + //
98+
" }\n" + //
99+
" }\n" + //
100+
"}\n"; //
101+
102+
String mapping = mappingBuilder.buildPropertyMapping(ComposedAnnotationEntity.class);
103+
104+
assertEquals(expected, mapping, false);
105+
}
106+
107+
@Inherited
108+
@Documented
109+
@Retention(RetentionPolicy.RUNTIME)
110+
@Target({ ElementType.TYPE })
111+
@Document(indexName = "", createIndex = false, shards = 42)
112+
public @interface DocumentNoCreate {
113+
114+
@AliasFor(value = "indexName", annotation = Document.class)
115+
String indexName();
116+
}
117+
118+
@Inherited
119+
@Documented
120+
@Retention(RetentionPolicy.RUNTIME)
121+
@Target(ElementType.FIELD)
122+
@Field(storeNullValue = true, nullValue = "NULL")
123+
public @interface NullValueField {
124+
@AliasFor(value = "name", annotation = Field.class)
125+
String name();
126+
}
127+
128+
@Inherited
129+
@Documented
130+
@Retention(RetentionPolicy.RUNTIME)
131+
@Target(ElementType.FIELD)
132+
@Field(type = FieldType.Date, format = DateFormat.date)
133+
public @interface LocalDateField {
134+
@AliasFor(value = "name", annotation = Field.class)
135+
String name() default "";
136+
}
137+
138+
@Inherited
139+
@Documented
140+
@Retention(RetentionPolicy.RUNTIME)
141+
@Target(ElementType.FIELD)
142+
@MultiField(mainField = @Field(type = FieldType.Text),
143+
otherFields = { @InnerField(suffix = "keyword", type = FieldType.Keyword) })
144+
public @interface TextKeywordField {
145+
}
146+
147+
@Data
148+
@NoArgsConstructor
149+
@AllArgsConstructor
150+
@DocumentNoCreate(indexName = "test-no-create")
151+
static class ComposedAnnotationEntity {
152+
@Id private String id;
153+
@NullValueField(name = "null-value") private String nullValue;
154+
@LocalDateField private LocalDate theDate;
155+
@TextKeywordField private String multiField;
156+
}
157+
}

0 commit comments

Comments
 (0)