Skip to content

Commit bc4c913

Browse files
authored
Make CompletionField annotation composable.
Original Pull Request #1841 Closes #1836
1 parent b515f18 commit bc4c913

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
import java.lang.annotation.Target;
2424

2525
/**
26-
* Based on the reference doc - https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
26+
* Based on the reference doc -
27+
* https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
2728
*
2829
* @author Mewes Kochheim
2930
* @author Robert Gruendler
31+
* @author Peter-Josef Meisch
3032
*/
3133
@Retention(RetentionPolicy.RUNTIME)
32-
@Target(ElementType.FIELD)
34+
@Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE })
3335
@Documented
3436
@Inherited
3537
public @interface CompletionField {

src/test/java/org/springframework/data/elasticsearch/annotations/ComposableAnnotationsUnitTest.java

+35-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.junit.jupiter.api.Test;
3333
import org.springframework.core.annotation.AliasFor;
3434
import org.springframework.data.annotation.Id;
35+
import org.springframework.data.elasticsearch.core.completion.Completion;
3536
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
3637
import org.springframework.data.elasticsearch.core.index.MappingBuilder;
3738
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
@@ -73,12 +74,17 @@ void fieldAnnotationShouldBeComposable() {
7374
assertThat(property.storeNullValue()).isTrue();
7475
}
7576

76-
@Test // DATAES-362
77+
@Test // DATAES-362, #1836
7778
@DisplayName("should use composed Field annotations in MappingBuilder")
7879
void shouldUseComposedFieldAnnotationsInMappingBuilder() throws JSONException {
7980

8081
String expected = "{\n" + //
81-
" \"properties\":{\n" + //
82+
" \"properties\": {\n" + //
83+
" \"_class\": {\n" + //
84+
" \"type\": \"keyword\",\n" + //
85+
" \"index\": false,\n" + //
86+
" \"doc_values\": false\n" + //
87+
" },\n" + //
8288
" \"null-value\": {\n" + //
8389
" \"null_value\": \"NULL\"\n" + //
8490
" },\n" + //
@@ -93,13 +99,21 @@ void shouldUseComposedFieldAnnotationsInMappingBuilder() throws JSONException {
9399
" \"type\": \"keyword\"\n" + //
94100
" }\n" + //
95101
" }\n" + //
102+
" },\n" + //
103+
" \"suggest\": {\n" + //
104+
" \"type\": \"completion\",\n" + //
105+
" \"max_input_length\": 50,\n" + //
106+
" \"preserve_position_increments\": true,\n" + //
107+
" \"preserve_separators\": true,\n" + //
108+
" \"search_analyzer\": \"myAnalyzer\",\n" + //
109+
" \"analyzer\": \"myAnalyzer\"\n" + //
96110
" }\n" + //
97111
" }\n" + //
98112
"}\n"; //
99113

100114
String mapping = mappingBuilder.buildPropertyMapping(ComposedAnnotationEntity.class);
101115

102-
assertEquals(expected, mapping, false);
116+
assertEquals(expected, mapping, true);
103117
}
104118

105119
@Inherited
@@ -142,12 +156,21 @@ void shouldUseComposedFieldAnnotationsInMappingBuilder() throws JSONException {
142156
public @interface TextKeywordField {
143157
}
144158

159+
@Inherited
160+
@Documented
161+
@Retention(RetentionPolicy.RUNTIME)
162+
@Target(ElementType.FIELD)
163+
@CompletionField(analyzer = "myAnalyzer", searchAnalyzer = "myAnalyzer")
164+
public @interface MyAnalyzerCompletionField {
165+
}
166+
145167
@DocumentNoCreate(indexName = "test-no-create")
146168
static class ComposedAnnotationEntity {
147169
@Nullable @Id private String id;
148170
@Nullable @NullValueField(name = "null-value") private String nullValue;
149171
@Nullable @LocalDateField private LocalDate theDate;
150172
@Nullable @TextKeywordField private String multiField;
173+
@Nullable @MyAnalyzerCompletionField private Completion suggest;
151174

152175
@Nullable
153176
public String getId() {
@@ -184,5 +207,14 @@ public String getMultiField() {
184207
public void setMultiField(@Nullable String multiField) {
185208
this.multiField = multiField;
186209
}
210+
211+
@Nullable
212+
public Completion getSuggest() {
213+
return suggest;
214+
}
215+
216+
public void setSuggest(@Nullable Completion suggest) {
217+
this.suggest = suggest;
218+
}
187219
}
188220
}

0 commit comments

Comments
 (0)