Skip to content

Commit 092690d

Browse files
pulse00xhaggi
authored andcommitted
DATAES-536 - Add support for context suggester
Original pull request: spring-projects#241
1 parent 16f2139 commit 092690d

File tree

8 files changed

+384
-3
lines changed

8 files changed

+384
-3
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.springframework.data.elasticsearch.annotations;
2+
3+
import org.elasticsearch.search.suggest.completion.context.ContextMapping;
4+
5+
import java.lang.annotation.Documented;
6+
import java.lang.annotation.ElementType;
7+
import java.lang.annotation.Inherited;
8+
import java.lang.annotation.Retention;
9+
import java.lang.annotation.RetentionPolicy;
10+
import java.lang.annotation.Target;
11+
12+
/**
13+
* Based on reference doc - https://www.elastic.co/guide/en/elasticsearch/reference/current/suggester-context.html
14+
*
15+
* @author Robert Gruendler
16+
*/
17+
@Retention(RetentionPolicy.RUNTIME)
18+
@Target(ElementType.FIELD)
19+
@Documented
20+
@Inherited
21+
public @interface CompletionContext {
22+
23+
String name();
24+
25+
ContextMapping.Type type();
26+
27+
String precision() default "";
28+
29+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.springframework.data.elasticsearch.annotations;
2+
3+
/**
4+
* Based on reference doc - https://www.elastic.co/guide/en/elasticsearch/reference/current/suggester-context.html
5+
*
6+
* @author Robert Gruendler
7+
*/
8+
public enum CompletionContextType {
9+
10+
CATEGORY, GEO
11+
12+
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@
1515
*/
1616
package org.springframework.data.elasticsearch.annotations;
1717

18-
import java.lang.annotation.*;
18+
import java.lang.annotation.Documented;
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Inherited;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
1924

2025
/**
2126
* Based on the reference doc - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
2227
*
2328
* @author Mewes Kochheim
29+
* @author Robert Gruendler
2430
*/
2531
@Retention(RetentionPolicy.RUNTIME)
2632
@Target(ElementType.FIELD)
@@ -37,4 +43,6 @@
3743
boolean preservePositionIncrements() default true;
3844

3945
int maxInputLength() default 50;
46+
47+
CompletionContext[] contexts() default {};
4048
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.core.ResolvableType;
2525
import org.springframework.core.io.ClassPathResource;
2626
import org.springframework.data.annotation.Transient;
27+
import org.springframework.data.elasticsearch.annotations.CompletionContext;
2728
import org.springframework.data.elasticsearch.annotations.CompletionField;
2829
import org.springframework.data.elasticsearch.annotations.DateFormat;
2930
import org.springframework.data.elasticsearch.annotations.Field;
@@ -53,6 +54,7 @@
5354
* @author Mark Paluch
5455
* @author Sascha Woo
5556
* @author Nordine Bittich
57+
* @author Robert Gruendler
5658
*/
5759
class MappingBuilder {
5860

@@ -67,10 +69,14 @@ class MappingBuilder {
6769
public static final String FIELD_PROPERTIES = "properties";
6870
public static final String FIELD_PARENT = "_parent";
6971
public static final String FIELD_COPY_TO = "copy_to";
72+
public static final String FIELD_CONTEXT_NAME = "name";
73+
public static final String FIELD_CONTEXT_TYPE = "type";
74+
public static final String FIELD_CONTEXT_PRECISION = "precision";
7075

7176
public static final String COMPLETION_PRESERVE_SEPARATORS = "preserve_separators";
7277
public static final String COMPLETION_PRESERVE_POSITION_INCREMENTS = "preserve_position_increments";
7378
public static final String COMPLETION_MAX_INPUT_LENGTH = "max_input_length";
79+
public static final String COMPLETION_CONTEXTS = "contexts";
7480

7581
public static final String TYPE_VALUE_KEYWORD = "keyword";
7682
public static final String TYPE_VALUE_GEO_POINT = "geo_point";
@@ -79,6 +85,7 @@ class MappingBuilder {
7985
public static final String TYPE_VALUE_GEO_HASH_PRECISION = "geohash_precision";
8086

8187
private static SimpleTypeHolder SIMPLE_TYPE_HOLDER = SimpleTypeHolder.DEFAULT;
88+
private XContentBuilder xContentBuilder;
8289

8390
static XContentBuilder buildMapping(Class clazz, String indexType, String idFieldName, String parentType) throws IOException {
8491

@@ -212,6 +219,20 @@ private static void applyCompletionFieldMapping(XContentBuilder xContentBuilder,
212219
if (!StringUtils.isEmpty(annotation.analyzer())) {
213220
xContentBuilder.field(FIELD_INDEX_ANALYZER, annotation.analyzer());
214221
}
222+
if (annotation.contexts().length > 0) {
223+
xContentBuilder.startArray(COMPLETION_CONTEXTS);
224+
for (CompletionContext context : annotation.contexts()) {
225+
xContentBuilder.startObject();
226+
xContentBuilder.field(FIELD_CONTEXT_NAME, context.name());
227+
xContentBuilder.field(FIELD_CONTEXT_TYPE, context.type().name().toLowerCase());
228+
if (context.precision().length() > 0) {
229+
xContentBuilder.field(FIELD_CONTEXT_PRECISION, context.precision());
230+
}
231+
xContentBuilder.endObject();
232+
}
233+
xContentBuilder.endArray();
234+
}
235+
215236
}
216237
xContentBuilder.endObject();
217238
}

src/main/java/org/springframework/data/elasticsearch/core/completion/Completion.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@
22

33
import com.fasterxml.jackson.annotation.JsonInclude;
44

5+
import java.util.List;
6+
import java.util.Map;
7+
58
/**
6-
* Based on the reference doc - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
9+
* Based on the reference doc -
10+
* http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
711
*
812
* @author Mewes Kochheim
13+
* @author Robert Gruendler
914
*/
1015
@JsonInclude(value = JsonInclude.Include.NON_NULL)
1116
public class Completion {
1217

1318
private String[] input;
19+
private Map<String, List<String>> contexts;
1420
private Integer weight;
1521

1622
private Completion() {
17-
//required by mapper to instantiate object
23+
// required by mapper to instantiate object
1824
}
1925

2026
public Completion(String[] input) {
@@ -36,4 +42,13 @@ public Integer getWeight() {
3642
public void setWeight(Integer weight) {
3743
this.weight = weight;
3844
}
45+
46+
public Map<String, List<String>> getContexts() {
47+
return contexts;
48+
}
49+
50+
public void setContexts(Map<String, List<String>> contexts) {
51+
this.contexts = contexts;
52+
}
53+
3954
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.springframework.data.elasticsearch.core.completion;
2+
3+
import org.elasticsearch.search.suggest.completion.context.ContextMapping;
4+
import org.springframework.data.annotation.Id;
5+
import org.springframework.data.elasticsearch.annotations.CompletionContext;
6+
import org.springframework.data.elasticsearch.annotations.CompletionContextType;
7+
import org.springframework.data.elasticsearch.annotations.CompletionField;
8+
import org.springframework.data.elasticsearch.annotations.Document;
9+
10+
/**
11+
* @author Mewes Kochheim
12+
* @author Robert Gruendler
13+
*/
14+
@Document(indexName = "test-index-context-completion", type = "context-completion-type", shards = 1, replicas = 0, refreshInterval = "-1")
15+
public class ContextCompletionEntity {
16+
17+
public static final String LANGUAGE_CATEGORY = "language";
18+
@Id
19+
private String id;
20+
private String name;
21+
22+
@CompletionField(maxInputLength = 100, contexts = {
23+
@CompletionContext(name = LANGUAGE_CATEGORY, type = ContextMapping.Type.CATEGORY)
24+
})
25+
private Completion suggest;
26+
27+
private ContextCompletionEntity() {
28+
}
29+
30+
public ContextCompletionEntity(String id) {
31+
this.id = id;
32+
}
33+
34+
public String getId() {
35+
return id;
36+
}
37+
38+
public void setId(String id) {
39+
this.id = id;
40+
}
41+
42+
public String getName() {
43+
return name;
44+
}
45+
46+
public void setName(String name) {
47+
this.name = name;
48+
}
49+
50+
public Completion getSuggest() {
51+
return suggest;
52+
}
53+
54+
public void setSuggest(Completion suggest) {
55+
this.suggest = suggest;
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2013-2019 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+
* http://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.core.completion;
17+
18+
import org.springframework.data.elasticsearch.core.query.IndexQuery;
19+
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
/**
24+
* @author Robert Gruendler
25+
*/
26+
public class ContextCompletionEntityBuilder {
27+
28+
private ContextCompletionEntity result;
29+
30+
public ContextCompletionEntityBuilder(String id) {
31+
result = new ContextCompletionEntity(id);
32+
}
33+
34+
public ContextCompletionEntityBuilder name(String name) {
35+
result.setName(name);
36+
return this;
37+
}
38+
39+
public ContextCompletionEntityBuilder suggest(String[] input, Map<String, List<String>> contexts) {
40+
Completion suggest = new Completion(input);
41+
suggest.setContexts(contexts);
42+
43+
result.setSuggest(suggest);
44+
return this;
45+
}
46+
47+
public ContextCompletionEntity build() {
48+
return result;
49+
}
50+
51+
public IndexQuery buildIndex() {
52+
IndexQuery indexQuery = new IndexQuery();
53+
indexQuery.setId(result.getId());
54+
indexQuery.setObject(result);
55+
return indexQuery;
56+
}
57+
}

0 commit comments

Comments
 (0)