Make CompletionField annotation composable.

Original Pull Request #1841
Closes #1836
This commit is contained in:
Peter-Josef Meisch 2021-06-06 21:14:20 +02:00 committed by GitHub
parent b515f18b33
commit bc4c913a97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 5 deletions

View File

@ -23,13 +23,15 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Based on the reference doc - https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
* Based on the reference doc -
* https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
*
* @author Mewes Kochheim
* @author Robert Gruendler
* @author Peter-Josef Meisch
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE })
@Documented
@Inherited
public @interface CompletionField {

View File

@ -32,6 +32,7 @@ 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.completion.Completion;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.index.MappingBuilder;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
@ -73,12 +74,17 @@ public class ComposableAnnotationsUnitTest {
assertThat(property.storeNullValue()).isTrue();
}
@Test // DATAES-362
@Test // DATAES-362, #1836
@DisplayName("should use composed Field annotations in MappingBuilder")
void shouldUseComposedFieldAnnotationsInMappingBuilder() throws JSONException {
String expected = "{\n" + //
" \"properties\": {\n" + //
" \"_class\": {\n" + //
" \"type\": \"keyword\",\n" + //
" \"index\": false,\n" + //
" \"doc_values\": false\n" + //
" },\n" + //
" \"null-value\": {\n" + //
" \"null_value\": \"NULL\"\n" + //
" },\n" + //
@ -93,13 +99,21 @@ public class ComposableAnnotationsUnitTest {
" \"type\": \"keyword\"\n" + //
" }\n" + //
" }\n" + //
" },\n" + //
" \"suggest\": {\n" + //
" \"type\": \"completion\",\n" + //
" \"max_input_length\": 50,\n" + //
" \"preserve_position_increments\": true,\n" + //
" \"preserve_separators\": true,\n" + //
" \"search_analyzer\": \"myAnalyzer\",\n" + //
" \"analyzer\": \"myAnalyzer\"\n" + //
" }\n" + //
" }\n" + //
"}\n"; //
String mapping = mappingBuilder.buildPropertyMapping(ComposedAnnotationEntity.class);
assertEquals(expected, mapping, false);
assertEquals(expected, mapping, true);
}
@Inherited
@ -142,12 +156,21 @@ public class ComposableAnnotationsUnitTest {
public @interface TextKeywordField {
}
@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@CompletionField(analyzer = "myAnalyzer", searchAnalyzer = "myAnalyzer")
public @interface MyAnalyzerCompletionField {
}
@DocumentNoCreate(indexName = "test-no-create")
static class ComposedAnnotationEntity {
@Nullable @Id private String id;
@Nullable @NullValueField(name = "null-value") private String nullValue;
@Nullable @LocalDateField private LocalDate theDate;
@Nullable @TextKeywordField private String multiField;
@Nullable @MyAnalyzerCompletionField private Completion suggest;
@Nullable
public String getId() {
@ -184,5 +207,14 @@ public class ComposableAnnotationsUnitTest {
public void setMultiField(@Nullable String multiField) {
this.multiField = multiField;
}
@Nullable
public Completion getSuggest() {
return suggest;
}
public void setSuggest(@Nullable Completion suggest) {
this.suggest = suggest;
}
}
}