Skip to content

Commit 1255dfa

Browse files
MewesKakonczak
authored andcommitted
DATAES-91 - fixed payload feature, added corresponding unit tests to cover it, fixed code format
1 parent 0ad982b commit 1255dfa

File tree

9 files changed

+431
-300
lines changed

9 files changed

+431
-300
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
@Retention(RetentionPolicy.RUNTIME)
2626
@Target(ElementType.FIELD)
2727
@Documented
28+
@Inherited
2829
public @interface CompletionField {
2930

3031
String searchAnalyzer() default "simple";

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

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.springframework.core.GenericCollectionTypeResolver;
3030
import org.springframework.data.annotation.Transient;
3131
import org.springframework.data.elasticsearch.annotations.*;
32-
import org.springframework.data.elasticsearch.core.completion.Completion;
32+
import org.springframework.data.elasticsearch.core.completion.Completion;
3333
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
3434
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
3535
import org.springframework.data.mapping.model.SimpleTypeHolder;
@@ -54,10 +54,15 @@ class MappingBuilder {
5454
public static final String FIELD_PROPERTIES = "properties";
5555
public static final String FIELD_PARENT = "_parent";
5656

57+
public static final String COMPLETION_PAYLOADS = "payloads";
58+
public static final String COMPLETION_PRESERVE_SEPARATORS = "preserve_separators";
59+
public static final String COMPLETION_PRESERVE_POSITION_INCREMENTS = "preserve_position_increments";
60+
public static final String COMPLETION_MAX_INPUT_LENGTH = "max_input_length";
61+
5762
public static final String INDEX_VALUE_NOT_ANALYZED = "not_analyzed";
5863
public static final String TYPE_VALUE_STRING = "string";
59-
public static final String TYPE_VALUE_GEO_POINT = "geo_point";
60-
public static final String TYPE_VALUE_COMPLETION = "completion";
64+
public static final String TYPE_VALUE_GEO_POINT = "geo_point";
65+
public static final String TYPE_VALUE_COMPLETION = "completion";
6166

6267
private static SimpleTypeHolder SIMPLE_TYPE_HOLDER = new SimpleTypeHolder();
6368

@@ -96,11 +101,11 @@ private static void mapEntity(XContentBuilder xContentBuilder, Class clazz, bool
96101
continue;
97102
}
98103

99-
boolean isGeoField = isGeoField(field);
100-
boolean isCompletionField = isCompletionField(field);
104+
boolean isGeoField = isGeoField(field);
105+
boolean isCompletionField = isCompletionField(field);
101106

102107
Field singleField = field.getAnnotation(Field.class);
103-
if (!isGeoField && !isCompletionField && isEntity(field) && isAnnotated(field)) {
108+
if (!isGeoField && !isCompletionField && isEntity(field) && isAnnotated(field)) {
104109
if (singleField == null) {
105110
continue;
106111
}
@@ -113,13 +118,14 @@ private static void mapEntity(XContentBuilder xContentBuilder, Class clazz, bool
113118

114119
MultiField multiField = field.getAnnotation(MultiField.class);
115120

116-
if (isGeoField) {
117-
applyGeoPointFieldMapping(xContentBuilder, field);
118-
}
119-
120-
if (isCompletionField) {
121-
applyCompletionFieldMapping(xContentBuilder, field);
122-
}
121+
if (isGeoField) {
122+
applyGeoPointFieldMapping(xContentBuilder, field);
123+
}
124+
125+
if (isCompletionField) {
126+
CompletionField completionField = field.getAnnotation(CompletionField.class);
127+
applyCompletionFieldMapping(xContentBuilder, field, completionField);
128+
}
123129

124130
if (isRootObject && singleField != null && isIdField(field, idFieldName)) {
125131
applyDefaultIdFieldMapping(xContentBuilder, field);
@@ -145,13 +151,13 @@ private static java.lang.reflect.Field[] retrieveFields(Class clazz) {
145151
fields.addAll(Arrays.asList(targetClass.getDeclaredFields()));
146152
targetClass = targetClass.getSuperclass();
147153
}
148-
while(targetClass != null && targetClass != Object.class);
154+
while (targetClass != null && targetClass != Object.class);
149155

150156
return fields.toArray(new java.lang.reflect.Field[fields.size()]);
151157
}
152158

153159
private static boolean isAnnotated(java.lang.reflect.Field field) {
154-
return field.getAnnotation(Field.class) != null || field.getAnnotation(MultiField.class) != null || field.getAnnotation(GeoPointField.class) != null;
160+
return field.getAnnotation(Field.class) != null || field.getAnnotation(MultiField.class) != null || field.getAnnotation(GeoPointField.class) != null || field.getAnnotation(CompletionField.class) != null;
155161
}
156162

157163
private static void applyGeoPointFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field) throws IOException {
@@ -160,12 +166,24 @@ private static void applyGeoPointFieldMapping(XContentBuilder xContentBuilder, j
160166
.endObject();
161167
}
162168

163-
private static void applyCompletionFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field) throws IOException {
164-
xContentBuilder.startObject(field.getName());
165-
xContentBuilder.field(FIELD_TYPE, TYPE_VALUE_COMPLETION)
166-
.endObject();
167-
}
168-
169+
private static void applyCompletionFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field, CompletionField annotation) throws IOException {
170+
xContentBuilder.startObject(field.getName());
171+
xContentBuilder.field(FIELD_TYPE, TYPE_VALUE_COMPLETION);
172+
if (annotation != null) {
173+
xContentBuilder.field(COMPLETION_MAX_INPUT_LENGTH, annotation.maxInputLength());
174+
xContentBuilder.field(COMPLETION_PAYLOADS, annotation.payloads());
175+
xContentBuilder.field(COMPLETION_PRESERVE_POSITION_INCREMENTS, annotation.preservePositionIncrements());
176+
xContentBuilder.field(COMPLETION_PRESERVE_SEPARATORS, annotation.preserveSeparators());
177+
if (isNotBlank(annotation.searchAnalyzer())) {
178+
xContentBuilder.field(FIELD_SEARCH_ANALYZER, annotation.searchAnalyzer());
179+
}
180+
if (isNotBlank(annotation.indexAnalyzer())) {
181+
xContentBuilder.field(FIELD_INDEX_ANALYZER, annotation.indexAnalyzer());
182+
}
183+
}
184+
xContentBuilder.endObject();
185+
}
186+
169187
private static void applyDefaultIdFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field)
170188
throws IOException {
171189
xContentBuilder.startObject(field.getName())
@@ -317,11 +335,11 @@ private static boolean isNestedOrObjectField(java.lang.reflect.Field field) {
317335
return fieldAnnotation != null && (FieldType.Nested == fieldAnnotation.type() || FieldType.Object == fieldAnnotation.type());
318336
}
319337

320-
private static boolean isGeoField(java.lang.reflect.Field field) {
321-
return field.getType() == GeoPoint.class || field.getAnnotation(GeoPointField.class) != null;
322-
}
323-
324-
private static boolean isCompletionField(java.lang.reflect.Field field) {
325-
return field.getType() == Completion.class;
326-
}
327-
}
338+
private static boolean isGeoField(java.lang.reflect.Field field) {
339+
return field.getType() == GeoPoint.class || field.getAnnotation(GeoPointField.class) != null;
340+
}
341+
342+
private static boolean isCompletionField(java.lang.reflect.Field field) {
343+
return field.getType() == Completion.class;
344+
}
345+
}

src/test/java/org/springframework/data/elasticsearch/core/completion/AnnotatedCompletionEntity.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,41 @@
1010
@Document(indexName = "test-completion-index", type = "annotated-completion-type", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
1111
public class AnnotatedCompletionEntity {
1212

13-
@Id
14-
private String id;
15-
private String name;
13+
@Id
14+
private String id;
15+
private String name;
1616

17-
@CompletionField(payloads = true, maxInputLength = 100)
18-
private Completion suggest;
17+
@CompletionField(payloads = true, maxInputLength = 100)
18+
private Completion suggest;
1919

20-
private AnnotatedCompletionEntity() {
21-
}
20+
private AnnotatedCompletionEntity() {
21+
}
2222

23-
public AnnotatedCompletionEntity(String id) {
24-
this.id = id;
25-
}
23+
public AnnotatedCompletionEntity(String id) {
24+
this.id = id;
25+
}
2626

27-
public String getId() {
28-
return id;
29-
}
27+
public String getId() {
28+
return id;
29+
}
3030

31-
public void setId(String id) {
32-
this.id = id;
33-
}
31+
public void setId(String id) {
32+
this.id = id;
33+
}
3434

35-
public String getName() {
36-
return name;
37-
}
35+
public String getName() {
36+
return name;
37+
}
3838

39-
public void setName(String name) {
40-
this.name = name;
41-
}
39+
public void setName(String name) {
40+
this.name = name;
41+
}
4242

43-
public Completion getSuggest() {
44-
return suggest;
45-
}
43+
public Completion getSuggest() {
44+
return suggest;
45+
}
4646

47-
public void setSuggest(Completion suggest) {
48-
this.suggest = suggest;
49-
}
47+
public void setSuggest(Completion suggest) {
48+
this.suggest = suggest;
49+
}
5050
}

src/test/java/org/springframework/data/elasticsearch/core/completion/AnnotatedCompletionEntityBuilder.java

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,47 +24,47 @@
2424
*/
2525
public class AnnotatedCompletionEntityBuilder {
2626

27-
private CompletionEntity result;
27+
private AnnotatedCompletionEntity result;
2828

29-
public AnnotatedCompletionEntityBuilder(String id) {
30-
result = new CompletionEntity(id);
31-
}
29+
public AnnotatedCompletionEntityBuilder(String id) {
30+
result = new AnnotatedCompletionEntity(id);
31+
}
3232

33-
public AnnotatedCompletionEntityBuilder name(String name) {
34-
result.setName(name);
35-
return this;
36-
}
33+
public AnnotatedCompletionEntityBuilder name(String name) {
34+
result.setName(name);
35+
return this;
36+
}
3737

38-
public AnnotatedCompletionEntityBuilder suggest(String[] input) {
39-
return suggest(input, null, null, null);
40-
}
38+
public AnnotatedCompletionEntityBuilder suggest(String[] input) {
39+
return suggest(input, null, null, null);
40+
}
4141

42-
public AnnotatedCompletionEntityBuilder suggest(String[] input, String output) {
43-
return suggest(input, output, null, null);
44-
}
42+
public AnnotatedCompletionEntityBuilder suggest(String[] input, String output) {
43+
return suggest(input, output, null, null);
44+
}
4545

46-
public AnnotatedCompletionEntityBuilder suggest(String[] input, String output, Object payload) {
47-
return suggest(input, output, payload, null);
48-
}
46+
public AnnotatedCompletionEntityBuilder suggest(String[] input, String output, Object payload) {
47+
return suggest(input, output, payload, null);
48+
}
4949

50-
public AnnotatedCompletionEntityBuilder suggest(String[] input, String output, Object payload, Integer weight) {
51-
Completion suggest = new Completion(input);
52-
suggest.setOutput(output);
53-
suggest.setPayload(payload);
54-
suggest.setWeight(weight);
50+
public AnnotatedCompletionEntityBuilder suggest(String[] input, String output, Object payload, Integer weight) {
51+
Completion suggest = new Completion(input);
52+
suggest.setOutput(output);
53+
suggest.setPayload(payload);
54+
suggest.setWeight(weight);
5555

56-
result.setSuggest(suggest);
57-
return this;
58-
}
56+
result.setSuggest(suggest);
57+
return this;
58+
}
5959

60-
public CompletionEntity build() {
61-
return result;
62-
}
60+
public AnnotatedCompletionEntity build() {
61+
return result;
62+
}
6363

64-
public IndexQuery buildIndex() {
65-
IndexQuery indexQuery = new IndexQuery();
66-
indexQuery.setId(result.getId());
67-
indexQuery.setObject(result);
68-
return indexQuery;
69-
}
64+
public IndexQuery buildIndex() {
65+
IndexQuery indexQuery = new IndexQuery();
66+
indexQuery.setId(result.getId());
67+
indexQuery.setObject(result);
68+
return indexQuery;
69+
}
7070
}

src/test/java/org/springframework/data/elasticsearch/core/completion/CompletionAnnotatedEntity.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,41 @@
1212
@Document(indexName = "test-completion-index", type = "completion-annotation-type", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
1313
public class CompletionAnnotatedEntity {
1414

15-
@Id
16-
private String id;
17-
private String name;
15+
@Id
16+
private String id;
17+
private String name;
1818

19-
@CompletionField(payloads = true)
20-
private Completion suggest;
19+
@CompletionField(payloads = true)
20+
private Completion suggest;
2121

22-
private CompletionAnnotatedEntity() {
23-
}
22+
private CompletionAnnotatedEntity() {
23+
}
2424

25-
public CompletionAnnotatedEntity(String id) {
26-
this.id = id;
27-
}
25+
public CompletionAnnotatedEntity(String id) {
26+
this.id = id;
27+
}
2828

29-
public String getId() {
30-
return id;
31-
}
29+
public String getId() {
30+
return id;
31+
}
3232

33-
public void setId(String id) {
34-
this.id = id;
35-
}
33+
public void setId(String id) {
34+
this.id = id;
35+
}
3636

37-
public String getName() {
38-
return name;
39-
}
37+
public String getName() {
38+
return name;
39+
}
4040

41-
public void setName(String name) {
42-
this.name = name;
43-
}
41+
public void setName(String name) {
42+
this.name = name;
43+
}
4444

45-
public Completion getSuggest() {
46-
return suggest;
47-
}
45+
public Completion getSuggest() {
46+
return suggest;
47+
}
4848

49-
public void setSuggest(Completion suggest) {
50-
this.suggest = suggest;
51-
}
49+
public void setSuggest(Completion suggest) {
50+
this.suggest = suggest;
51+
}
5252
}

0 commit comments

Comments
 (0)