DATAES-91 - completion suggester support
This commit is contained in:
committed by
Artur Konczak
parent
0e37c5d36f
commit
dfa0823c9a
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2013 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
|
||||
*
|
||||
* http://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 java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* Based on the reference doc - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
|
||||
*
|
||||
* @author Mewes Kochheim
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@Documented
|
||||
public @interface CompletionField {
|
||||
|
||||
String searchAnalyzer() default "simple";
|
||||
|
||||
String indexAnalyzer() default "simple";
|
||||
|
||||
boolean payloads() default false;
|
||||
|
||||
boolean preserveSeparators() default true;
|
||||
|
||||
boolean preservePositionIncrements() default true;
|
||||
|
||||
int maxInputLength() default 50;
|
||||
}
|
||||
@@ -51,6 +51,9 @@ import org.elasticsearch.action.mlt.MoreLikeThisRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.search.SearchType;
|
||||
import org.elasticsearch.action.suggest.SuggestRequest;
|
||||
import org.elasticsearch.action.suggest.SuggestRequestBuilder;
|
||||
import org.elasticsearch.action.suggest.SuggestResponse;
|
||||
import org.elasticsearch.action.update.UpdateRequestBuilder;
|
||||
import org.elasticsearch.action.update.UpdateResponse;
|
||||
import org.elasticsearch.client.Client;
|
||||
@@ -60,6 +63,7 @@ import org.elasticsearch.cluster.metadata.MappingMetaData;
|
||||
import org.elasticsearch.common.collect.ImmutableOpenMap;
|
||||
import org.elasticsearch.common.collect.MapBuilder;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.index.query.FilterBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
@@ -70,6 +74,7 @@ import org.elasticsearch.search.facet.FacetBuilder;
|
||||
import org.elasticsearch.search.highlight.HighlightBuilder;
|
||||
import org.elasticsearch.search.sort.SortBuilder;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.elasticsearch.search.suggest.SuggestBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
@@ -976,4 +981,14 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
||||
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
public SuggestResponse suggest(SuggestBuilder.SuggestionBuilder<?> suggestion, String... indices) {
|
||||
SuggestRequestBuilder suggestRequestBuilder = client.prepareSuggest(indices);
|
||||
suggestRequestBuilder.addSuggestion(suggestion);
|
||||
return suggestRequestBuilder.execute().actionGet();
|
||||
}
|
||||
|
||||
public SuggestResponse suggest(SuggestBuilder.SuggestionBuilder<?> suggestion, Class clazz) {
|
||||
return suggest(suggestion, retrieveIndexNameFromPersistentEntity(clazz));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.springframework.core.GenericCollectionTypeResolver;
|
||||
import org.springframework.data.annotation.Transient;
|
||||
import org.springframework.data.elasticsearch.annotations.*;
|
||||
import org.springframework.data.elasticsearch.core.completion.Completion;
|
||||
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
|
||||
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
@@ -55,7 +56,8 @@ class MappingBuilder {
|
||||
|
||||
public static final String INDEX_VALUE_NOT_ANALYZED = "not_analyzed";
|
||||
public static final String TYPE_VALUE_STRING = "string";
|
||||
public static final String TYPE_VALUE_GEO_POINT = "geo_point";
|
||||
public static final String TYPE_VALUE_GEO_POINT = "geo_point";
|
||||
public static final String TYPE_VALUE_COMPLETION = "completion";
|
||||
|
||||
private static SimpleTypeHolder SIMPLE_TYPE_HOLDER = new SimpleTypeHolder();
|
||||
|
||||
@@ -94,10 +96,11 @@ class MappingBuilder {
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean isGeoField = isGeoField(field);
|
||||
boolean isGeoField = isGeoField(field);
|
||||
boolean isCompletionField = isCompletionField(field);
|
||||
|
||||
Field singleField = field.getAnnotation(Field.class);
|
||||
if (!isGeoField && isEntity(field) && isAnnotated(field)) {
|
||||
if (!isGeoField && !isCompletionField && isEntity(field) && isAnnotated(field)) {
|
||||
if (singleField == null) {
|
||||
continue;
|
||||
}
|
||||
@@ -110,9 +113,13 @@ class MappingBuilder {
|
||||
|
||||
MultiField multiField = field.getAnnotation(MultiField.class);
|
||||
|
||||
if (isGeoField) {
|
||||
applyGeoPointFieldMapping(xContentBuilder, field);
|
||||
}
|
||||
if (isGeoField) {
|
||||
applyGeoPointFieldMapping(xContentBuilder, field);
|
||||
}
|
||||
|
||||
if (isCompletionField) {
|
||||
applyCompletionFieldMapping(xContentBuilder, field);
|
||||
}
|
||||
|
||||
if (isRootObject && singleField != null && isIdField(field, idFieldName)) {
|
||||
applyDefaultIdFieldMapping(xContentBuilder, field);
|
||||
@@ -153,6 +160,12 @@ class MappingBuilder {
|
||||
.endObject();
|
||||
}
|
||||
|
||||
private static void applyCompletionFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field) throws IOException {
|
||||
xContentBuilder.startObject(field.getName());
|
||||
xContentBuilder.field(FIELD_TYPE, TYPE_VALUE_COMPLETION)
|
||||
.endObject();
|
||||
}
|
||||
|
||||
private static void applyDefaultIdFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field)
|
||||
throws IOException {
|
||||
xContentBuilder.startObject(field.getName())
|
||||
@@ -304,7 +317,11 @@ class MappingBuilder {
|
||||
return fieldAnnotation != null && (FieldType.Nested == fieldAnnotation.type() || FieldType.Object == fieldAnnotation.type());
|
||||
}
|
||||
|
||||
private static boolean isGeoField(java.lang.reflect.Field field) {
|
||||
return field.getType() == GeoPoint.class || field.getAnnotation(GeoPointField.class) != null;
|
||||
}
|
||||
}
|
||||
private static boolean isGeoField(java.lang.reflect.Field field) {
|
||||
return field.getType() == GeoPoint.class || field.getAnnotation(GeoPointField.class) != null;
|
||||
}
|
||||
|
||||
private static boolean isCompletionField(java.lang.reflect.Field field) {
|
||||
return field.getType() == Completion.class;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
diff a/src/main/java/org/springframework/data/elasticsearch/core/MappingBuilder.java b/src/main/java/org/springframework/data/elasticsearch/core/MappingBuilder.java (rejected hunks)
|
||||
@@ -29,6 +29,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.springframework.core.GenericCollectionTypeResolver;
|
||||
import org.springframework.data.annotation.Transient;
|
||||
import org.springframework.data.elasticsearch.annotations.*;
|
||||
+import org.springframework.data.elasticsearch.core.completion.Completion;
|
||||
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
|
||||
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
@@ -55,7 +56,8 @@ class MappingBuilder {
|
||||
|
||||
public static final String INDEX_VALUE_NOT_ANALYZED = "not_analyzed";
|
||||
public static final String TYPE_VALUE_STRING = "string";
|
||||
- public static final String TYPE_VALUE_GEO_POINT = "geo_point";
|
||||
+ public static final String TYPE_VALUE_GEO_POINT = "geo_point";
|
||||
+ public static final String TYPE_VALUE_COMPLETION = "completion";
|
||||
|
||||
private static SimpleTypeHolder SIMPLE_TYPE_HOLDER = new SimpleTypeHolder();
|
||||
|
||||
@@ -94,10 +96,11 @@ class MappingBuilder {
|
||||
continue;
|
||||
}
|
||||
|
||||
- boolean isGeoField = isGeoField(field);
|
||||
+ boolean isGeoField = isGeoField(field);
|
||||
+ boolean isCompletionField = isCompletionField(field);
|
||||
|
||||
Field singleField = field.getAnnotation(Field.class);
|
||||
- if (!isGeoField && isEntity(field) && isAnnotated(field)) {
|
||||
+ if (!isGeoField && !isCompletionField && isEntity(field) && isAnnotated(field)) {
|
||||
if (singleField == null) {
|
||||
continue;
|
||||
}
|
||||
@@ -110,9 +113,13 @@ class MappingBuilder {
|
||||
|
||||
MultiField multiField = field.getAnnotation(MultiField.class);
|
||||
|
||||
- if (isGeoField) {
|
||||
- applyGeoPointFieldMapping(xContentBuilder, field);
|
||||
- }
|
||||
+ if (isGeoField) {
|
||||
+ applyGeoPointFieldMapping(xContentBuilder, field);
|
||||
+ }
|
||||
+
|
||||
+ if (isCompletionField) {
|
||||
+ applyCompletionFieldMapping(xContentBuilder, field);
|
||||
+ }
|
||||
|
||||
if (isRootObject && singleField != null && isIdField(field, idFieldName)) {
|
||||
applyDefaultIdFieldMapping(xContentBuilder, field);
|
||||
@@ -153,6 +160,12 @@ class MappingBuilder {
|
||||
.endObject();
|
||||
}
|
||||
|
||||
+ private static void applyCompletionFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field) throws IOException {
|
||||
+ xContentBuilder.startObject(field.getName());
|
||||
+ xContentBuilder.field(FIELD_TYPE, TYPE_VALUE_COMPLETION)
|
||||
+ .endObject();
|
||||
+ }
|
||||
+
|
||||
private static void applyDefaultIdFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field)
|
||||
throws IOException {
|
||||
xContentBuilder.startObject(field.getName())
|
||||
@@ -304,7 +317,11 @@ class MappingBuilder {
|
||||
return fieldAnnotation != null && (FieldType.Nested == fieldAnnotation.type() || FieldType.Object == fieldAnnotation.type());
|
||||
}
|
||||
|
||||
- private static boolean isGeoField(java.lang.reflect.Field field) {
|
||||
- return field.getType() == GeoPoint.class || field.getAnnotation(GeoPointField.class) != null;
|
||||
- }
|
||||
+ private static boolean isGeoField(java.lang.reflect.Field field) {
|
||||
+ return field.getType() == GeoPoint.class || field.getAnnotation(GeoPointField.class) != null;
|
||||
+ }
|
||||
+
|
||||
+ private static boolean isCompletionField(java.lang.reflect.Field field) {
|
||||
+ return field.getType() == Completion.class;
|
||||
+ }
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package org.springframework.data.elasticsearch.core.completion;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
|
||||
/**
|
||||
* Based on the reference doc - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
|
||||
*
|
||||
* @author Mewes Kochheim
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public class Completion {
|
||||
|
||||
private String[] input;
|
||||
private String output;
|
||||
private Integer weight;
|
||||
private Object payload;
|
||||
|
||||
private Completion() {
|
||||
//required by mapper to instantiate object
|
||||
}
|
||||
|
||||
public Completion(String[] input) {
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
public String[] getInput() {
|
||||
return input;
|
||||
}
|
||||
|
||||
public void setInput(String[] input) {
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
public String getOutput() {
|
||||
return output;
|
||||
}
|
||||
|
||||
public void setOutput(String output) {
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
public Object getPayload() {
|
||||
return payload;
|
||||
}
|
||||
|
||||
public void setPayload(Object payload) {
|
||||
this.payload = payload;
|
||||
}
|
||||
|
||||
public Integer getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(Integer weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user