DATAES-362 - Add support for composable meta annotations.

Original PR: #566
This commit is contained in:
Peter-Josef Meisch 2020-12-05 22:53:18 +01:00 committed by GitHub
parent 6edb8353b5
commit a42de9b51b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 229 additions and 25 deletions

View File

@ -36,7 +36,7 @@ import org.springframework.core.annotation.AliasFor;
* @author Aleksei Arsenev
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE })
@Documented
@Inherited
public @interface Field {

View File

@ -27,9 +27,10 @@ import java.lang.annotation.Target;
* @author Artur Konczak
* @author Jonathan Yan
* @author Xiao Yu
* @author Peter-Josef Meisch
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE })
@Documented
public @interface MultiField {

View File

@ -25,6 +25,8 @@ import java.util.Set;
import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
import org.springframework.data.elasticsearch.annotations.Mapping;
@ -105,9 +107,13 @@ abstract class AbstractDefaultIndexOperations implements IndexOperations {
Document settings = null;
if (clazz.isAnnotationPresent(Setting.class)) {
String settingPath = clazz.getAnnotation(Setting.class).settingPath();
settings = loadSettings(settingPath);
if (AnnotatedElementUtils.hasAnnotation(clazz, Setting.class)) {
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(clazz, Setting.class);
if (attributes != null) {
String settingPath = attributes.getString("settingPath");
settings = loadSettings(settingPath);
}
}
if (settings == null) {
@ -224,22 +230,28 @@ abstract class AbstractDefaultIndexOperations implements IndexOperations {
protected Document buildMapping(Class<?> clazz) {
// load mapping specified in Mapping annotation if present
if (clazz.isAnnotationPresent(Mapping.class)) {
String mappingPath = clazz.getAnnotation(Mapping.class).mappingPath();
if (AnnotatedElementUtils.hasAnnotation(clazz, Mapping.class)) {
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(clazz, Mapping.class);
if (!StringUtils.isEmpty(mappingPath)) {
String mappings = ResourceUtil.readFileFromClasspath(mappingPath);
if (attributes != null) {
String mappingPath = attributes.getString("mappingPath");
if (!StringUtils.isEmpty(mappings)) {
return Document.parse(mappings);
if (StringUtils.hasText(mappingPath)) {
String mappings = ResourceUtil.readFileFromClasspath(mappingPath);
if (StringUtils.hasText(mappings)) {
return Document.parse(mappings);
}
} else {
LOGGER.info("mappingPath in @Mapping has to be defined. Building mappings using @Field");
}
} else {
LOGGER.info("mappingPath in @Mapping has to be defined. Building mappings using @Field");
}
}
// build mapping from field annotations
try {
try
{
String mapping = new MappingBuilder(elasticsearchConverter).buildPropertyMapping(clazz);
return Document.parse(mapping);
} catch (Exception e) {

View File

@ -37,6 +37,8 @@ import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.elasticsearch.NoSuchIndexException;
import org.springframework.data.elasticsearch.annotations.Mapping;
@ -157,9 +159,13 @@ class DefaultReactiveIndexOperations implements ReactiveIndexOperations {
@Override
public Mono<Document> createMapping(Class<?> clazz) {
if (clazz.isAnnotationPresent(Mapping.class)) {
String mappingPath = clazz.getAnnotation(Mapping.class).mappingPath();
return loadDocument(mappingPath, "@Mapping");
if (AnnotatedElementUtils.hasAnnotation(clazz, Mapping.class)) {
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(clazz, Mapping.class);
if (attributes != null) {
String mappingPath = clazz.getAnnotation(Mapping.class).mappingPath();
return loadDocument(mappingPath, "@Mapping");
}
}
String mapping = new MappingBuilder(converter).buildPropertyMapping(clazz);
@ -198,10 +204,13 @@ class DefaultReactiveIndexOperations implements ReactiveIndexOperations {
@Override
public Mono<Document> createSettings(Class<?> clazz) {
if (clazz.isAnnotationPresent(Setting.class)) {
String settingPath = clazz.getAnnotation(Setting.class).settingPath();
if (AnnotatedElementUtils.hasAnnotation(clazz, Setting.class)) {
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(clazz, Setting.class);
return loadDocument(settingPath, "@Setting");
if (attributes != null) {
String settingPath = attributes.getString("settingPath");
return loadDocument(settingPath, "@Setting");
}
}
return Mono.just(getRequiredPersistentEntity(clazz).getDefaultSettings());

View File

@ -52,6 +52,25 @@ public class IndexCoordinates {
return Arrays.copyOf(indexNames, indexNames.length);
}
/**
* @since 4.2
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IndexCoordinates that = (IndexCoordinates) o;
return Arrays.equals(indexNames, that.indexNames);
}
/**
* @since 4.2
*/
@Override
public int hashCode() {
return Arrays.hashCode(indexNames);
}
@Override
public String toString() {
return "IndexCoordinates{" + "indexNames=" + Arrays.toString(indexNames) + '}';

View File

@ -23,6 +23,7 @@ import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.index.VersionType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.data.elasticsearch.annotations.Parent;
import org.springframework.data.elasticsearch.annotations.Setting;
import org.springframework.data.elasticsearch.core.document.Document;
@ -87,9 +88,11 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
super(typeInformation);
Class<T> clazz = typeInformation.getType();
if (clazz.isAnnotationPresent(org.springframework.data.elasticsearch.annotations.Document.class)) {
org.springframework.data.elasticsearch.annotations.Document document = clazz
.getAnnotation(org.springframework.data.elasticsearch.annotations.Document.class);
org.springframework.data.elasticsearch.annotations.Document document = AnnotatedElementUtils
.findMergedAnnotation(clazz, org.springframework.data.elasticsearch.annotations.Document.class);
if (document != null) {
Assert.hasText(document.indexName(),
" Unknown indexName. Make sure the indexName is defined. e.g @Document(indexName=\"foo\")");
this.indexName = document.indexName();
@ -101,8 +104,11 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
this.versionType = document.versionType();
this.createIndexAndMapping = document.createIndex();
}
if (clazz.isAnnotationPresent(Setting.class)) {
this.settingPath = typeInformation.getType().getAnnotation(Setting.class).settingPath();
Setting setting = AnnotatedElementUtils.getMergedAnnotation(clazz, Setting.class);
if (setting != null) {
this.settingPath = setting.settingPath();
}
}

View File

@ -0,0 +1,157 @@
/*
* Copyright 2020 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
*
* https://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.annotations;
import static org.assertj.core.api.Assertions.*;
import static org.skyscreamer.jsonassert.JSONAssert.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.time.LocalDate;
import org.json.JSONException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.core.annotation.AliasFor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.index.MappingBuilder;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchPersistentEntity;
/**
* @author Peter-Josef Meisch
*/
public class ComposableAnnotationsUnitTest {
private static SimpleElasticsearchMappingContext mappingContext = new SimpleElasticsearchMappingContext();
private static MappingElasticsearchConverter converter = new MappingElasticsearchConverter(mappingContext);
private static MappingBuilder mappingBuilder = new MappingBuilder(converter);
@Test // DATAES-362
@DisplayName("Document annotation should be composable")
void documentAnnotationShouldBeComposable() {
SimpleElasticsearchPersistentEntity<?> entity = mappingContext
.getRequiredPersistentEntity(ComposedAnnotationEntity.class);
assertThat(entity.getIndexCoordinates()).isEqualTo(IndexCoordinates.of("test-no-create"));
assertThat(entity.isCreateIndexAndMapping()).isFalse();
assertThat(entity.getShards()).isEqualTo((short) 42);
}
@Test // DATAES-362
@DisplayName("Field annotation should be composable")
void fieldAnnotationShouldBeComposable() {
SimpleElasticsearchPersistentEntity<?> entity = mappingContext
.getRequiredPersistentEntity(ComposedAnnotationEntity.class);
ElasticsearchPersistentProperty property = entity.getRequiredPersistentProperty("nullValue");
assertThat(property.getFieldName()).isEqualTo("null-value");
assertThat(property.storeNullValue()).isTrue();
}
@Test // DATAES-362
@DisplayName("should use composed Field annotations in MappingBuilder")
void shouldUseComposedFieldAnnotationsInMappingBuilder() throws JSONException {
String expected = "{\n" + //
" \"properties\":{\n" + //
" \"null-value\": {\n" + //
" \"null_value\": \"NULL\"\n" + //
" },\n" + //
" \"theDate\": {\n" + //
" \"type\": \"date\",\n" + //
" \"format\": \"date\"\n" + //
" },\n" + //
" \"multiField\": {\n" + //
" \"type\": \"text\",\n" + //
" \"fields\": {\n" + //
" \"keyword\": {\n" + //
" \"type\": \"keyword\"\n" + //
" }\n" + //
" }\n" + //
" }\n" + //
" }\n" + //
"}\n"; //
String mapping = mappingBuilder.buildPropertyMapping(ComposedAnnotationEntity.class);
assertEquals(expected, mapping, false);
}
@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
@Document(indexName = "", createIndex = false, shards = 42)
public @interface DocumentNoCreate {
@AliasFor(value = "indexName", annotation = Document.class)
String indexName();
}
@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Field(storeNullValue = true, nullValue = "NULL")
public @interface NullValueField {
@AliasFor(value = "name", annotation = Field.class)
String name();
}
@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Field(type = FieldType.Date, format = DateFormat.date)
public @interface LocalDateField {
@AliasFor(value = "name", annotation = Field.class)
String name() default "";
}
@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@MultiField(mainField = @Field(type = FieldType.Text),
otherFields = { @InnerField(suffix = "keyword", type = FieldType.Keyword) })
public @interface TextKeywordField {
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@DocumentNoCreate(indexName = "test-no-create")
static class ComposedAnnotationEntity {
@Id private String id;
@NullValueField(name = "null-value") private String nullValue;
@LocalDateField private LocalDate theDate;
@TextKeywordField private String multiField;
}
}