mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-08 13:12:10 +00:00
DATAES-91 - fixed payload feature, added corresponding unit tests to cover it, fixed code format
This commit is contained in:
parent
0ad982b233
commit
1255dfa89d
@ -25,6 +25,7 @@ import java.lang.annotation.*;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@Documented
|
||||
@Inherited
|
||||
public @interface CompletionField {
|
||||
|
||||
String searchAnalyzer() default "simple";
|
||||
|
@ -29,7 +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.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;
|
||||
@ -54,10 +54,15 @@ class MappingBuilder {
|
||||
public static final String FIELD_PROPERTIES = "properties";
|
||||
public static final String FIELD_PARENT = "_parent";
|
||||
|
||||
public static final String COMPLETION_PAYLOADS = "payloads";
|
||||
public static final String COMPLETION_PRESERVE_SEPARATORS = "preserve_separators";
|
||||
public static final String COMPLETION_PRESERVE_POSITION_INCREMENTS = "preserve_position_increments";
|
||||
public static final String COMPLETION_MAX_INPUT_LENGTH = "max_input_length";
|
||||
|
||||
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_COMPLETION = "completion";
|
||||
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();
|
||||
|
||||
@ -96,11 +101,11 @@ class MappingBuilder {
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean isGeoField = isGeoField(field);
|
||||
boolean isCompletionField = isCompletionField(field);
|
||||
boolean isGeoField = isGeoField(field);
|
||||
boolean isCompletionField = isCompletionField(field);
|
||||
|
||||
Field singleField = field.getAnnotation(Field.class);
|
||||
if (!isGeoField && !isCompletionField && isEntity(field) && isAnnotated(field)) {
|
||||
if (!isGeoField && !isCompletionField && isEntity(field) && isAnnotated(field)) {
|
||||
if (singleField == null) {
|
||||
continue;
|
||||
}
|
||||
@ -113,13 +118,14 @@ class MappingBuilder {
|
||||
|
||||
MultiField multiField = field.getAnnotation(MultiField.class);
|
||||
|
||||
if (isGeoField) {
|
||||
applyGeoPointFieldMapping(xContentBuilder, field);
|
||||
}
|
||||
|
||||
if (isCompletionField) {
|
||||
applyCompletionFieldMapping(xContentBuilder, field);
|
||||
}
|
||||
if (isGeoField) {
|
||||
applyGeoPointFieldMapping(xContentBuilder, field);
|
||||
}
|
||||
|
||||
if (isCompletionField) {
|
||||
CompletionField completionField = field.getAnnotation(CompletionField.class);
|
||||
applyCompletionFieldMapping(xContentBuilder, field, completionField);
|
||||
}
|
||||
|
||||
if (isRootObject && singleField != null && isIdField(field, idFieldName)) {
|
||||
applyDefaultIdFieldMapping(xContentBuilder, field);
|
||||
@ -145,13 +151,13 @@ class MappingBuilder {
|
||||
fields.addAll(Arrays.asList(targetClass.getDeclaredFields()));
|
||||
targetClass = targetClass.getSuperclass();
|
||||
}
|
||||
while(targetClass != null && targetClass != Object.class);
|
||||
while (targetClass != null && targetClass != Object.class);
|
||||
|
||||
return fields.toArray(new java.lang.reflect.Field[fields.size()]);
|
||||
}
|
||||
|
||||
private static boolean isAnnotated(java.lang.reflect.Field field) {
|
||||
return field.getAnnotation(Field.class) != null || field.getAnnotation(MultiField.class) != null || field.getAnnotation(GeoPointField.class) != null;
|
||||
return field.getAnnotation(Field.class) != null || field.getAnnotation(MultiField.class) != null || field.getAnnotation(GeoPointField.class) != null || field.getAnnotation(CompletionField.class) != null;
|
||||
}
|
||||
|
||||
private static void applyGeoPointFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field) throws IOException {
|
||||
@ -160,12 +166,24 @@ 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 applyCompletionFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field, CompletionField annotation) throws IOException {
|
||||
xContentBuilder.startObject(field.getName());
|
||||
xContentBuilder.field(FIELD_TYPE, TYPE_VALUE_COMPLETION);
|
||||
if (annotation != null) {
|
||||
xContentBuilder.field(COMPLETION_MAX_INPUT_LENGTH, annotation.maxInputLength());
|
||||
xContentBuilder.field(COMPLETION_PAYLOADS, annotation.payloads());
|
||||
xContentBuilder.field(COMPLETION_PRESERVE_POSITION_INCREMENTS, annotation.preservePositionIncrements());
|
||||
xContentBuilder.field(COMPLETION_PRESERVE_SEPARATORS, annotation.preserveSeparators());
|
||||
if (isNotBlank(annotation.searchAnalyzer())) {
|
||||
xContentBuilder.field(FIELD_SEARCH_ANALYZER, annotation.searchAnalyzer());
|
||||
}
|
||||
if (isNotBlank(annotation.indexAnalyzer())) {
|
||||
xContentBuilder.field(FIELD_INDEX_ANALYZER, annotation.indexAnalyzer());
|
||||
}
|
||||
}
|
||||
xContentBuilder.endObject();
|
||||
}
|
||||
|
||||
private static void applyDefaultIdFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field)
|
||||
throws IOException {
|
||||
xContentBuilder.startObject(field.getName())
|
||||
@ -317,11 +335,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 isCompletionField(java.lang.reflect.Field field) {
|
||||
return field.getType() == Completion.class;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -10,41 +10,41 @@ import org.springframework.data.elasticsearch.annotations.Document;
|
||||
@Document(indexName = "test-completion-index", type = "annotated-completion-type", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class AnnotatedCompletionEntity {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
private String name;
|
||||
@Id
|
||||
private String id;
|
||||
private String name;
|
||||
|
||||
@CompletionField(payloads = true, maxInputLength = 100)
|
||||
private Completion suggest;
|
||||
@CompletionField(payloads = true, maxInputLength = 100)
|
||||
private Completion suggest;
|
||||
|
||||
private AnnotatedCompletionEntity() {
|
||||
}
|
||||
private AnnotatedCompletionEntity() {
|
||||
}
|
||||
|
||||
public AnnotatedCompletionEntity(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public AnnotatedCompletionEntity(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Completion getSuggest() {
|
||||
return suggest;
|
||||
}
|
||||
public Completion getSuggest() {
|
||||
return suggest;
|
||||
}
|
||||
|
||||
public void setSuggest(Completion suggest) {
|
||||
this.suggest = suggest;
|
||||
}
|
||||
public void setSuggest(Completion suggest) {
|
||||
this.suggest = suggest;
|
||||
}
|
||||
}
|
||||
|
@ -24,47 +24,47 @@ import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||
*/
|
||||
public class AnnotatedCompletionEntityBuilder {
|
||||
|
||||
private CompletionEntity result;
|
||||
private AnnotatedCompletionEntity result;
|
||||
|
||||
public AnnotatedCompletionEntityBuilder(String id) {
|
||||
result = new CompletionEntity(id);
|
||||
}
|
||||
public AnnotatedCompletionEntityBuilder(String id) {
|
||||
result = new AnnotatedCompletionEntity(id);
|
||||
}
|
||||
|
||||
public AnnotatedCompletionEntityBuilder name(String name) {
|
||||
result.setName(name);
|
||||
return this;
|
||||
}
|
||||
public AnnotatedCompletionEntityBuilder name(String name) {
|
||||
result.setName(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
public AnnotatedCompletionEntityBuilder suggest(String[] input) {
|
||||
return suggest(input, null, null, null);
|
||||
}
|
||||
public AnnotatedCompletionEntityBuilder suggest(String[] input) {
|
||||
return suggest(input, null, null, null);
|
||||
}
|
||||
|
||||
public AnnotatedCompletionEntityBuilder suggest(String[] input, String output) {
|
||||
return suggest(input, output, null, null);
|
||||
}
|
||||
public AnnotatedCompletionEntityBuilder suggest(String[] input, String output) {
|
||||
return suggest(input, output, null, null);
|
||||
}
|
||||
|
||||
public AnnotatedCompletionEntityBuilder suggest(String[] input, String output, Object payload) {
|
||||
return suggest(input, output, payload, null);
|
||||
}
|
||||
public AnnotatedCompletionEntityBuilder suggest(String[] input, String output, Object payload) {
|
||||
return suggest(input, output, payload, null);
|
||||
}
|
||||
|
||||
public AnnotatedCompletionEntityBuilder suggest(String[] input, String output, Object payload, Integer weight) {
|
||||
Completion suggest = new Completion(input);
|
||||
suggest.setOutput(output);
|
||||
suggest.setPayload(payload);
|
||||
suggest.setWeight(weight);
|
||||
public AnnotatedCompletionEntityBuilder suggest(String[] input, String output, Object payload, Integer weight) {
|
||||
Completion suggest = new Completion(input);
|
||||
suggest.setOutput(output);
|
||||
suggest.setPayload(payload);
|
||||
suggest.setWeight(weight);
|
||||
|
||||
result.setSuggest(suggest);
|
||||
return this;
|
||||
}
|
||||
result.setSuggest(suggest);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CompletionEntity build() {
|
||||
return result;
|
||||
}
|
||||
public AnnotatedCompletionEntity build() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
}
|
||||
|
@ -12,41 +12,41 @@ import org.springframework.data.elasticsearch.annotations.Document;
|
||||
@Document(indexName = "test-completion-index", type = "completion-annotation-type", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class CompletionAnnotatedEntity {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
private String name;
|
||||
@Id
|
||||
private String id;
|
||||
private String name;
|
||||
|
||||
@CompletionField(payloads = true)
|
||||
private Completion suggest;
|
||||
@CompletionField(payloads = true)
|
||||
private Completion suggest;
|
||||
|
||||
private CompletionAnnotatedEntity() {
|
||||
}
|
||||
private CompletionAnnotatedEntity() {
|
||||
}
|
||||
|
||||
public CompletionAnnotatedEntity(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public CompletionAnnotatedEntity(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Completion getSuggest() {
|
||||
return suggest;
|
||||
}
|
||||
public Completion getSuggest() {
|
||||
return suggest;
|
||||
}
|
||||
|
||||
public void setSuggest(Completion suggest) {
|
||||
this.suggest = suggest;
|
||||
}
|
||||
public void setSuggest(Completion suggest) {
|
||||
this.suggest = suggest;
|
||||
}
|
||||
}
|
||||
|
@ -9,40 +9,40 @@ import org.springframework.data.elasticsearch.annotations.Document;
|
||||
@Document(indexName = "test-completion-index", type = "completion-type", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class CompletionEntity {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
private String name;
|
||||
@Id
|
||||
private String id;
|
||||
private String name;
|
||||
|
||||
private Completion suggest;
|
||||
private Completion suggest;
|
||||
|
||||
private CompletionEntity() {
|
||||
}
|
||||
private CompletionEntity() {
|
||||
}
|
||||
|
||||
public CompletionEntity(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public CompletionEntity(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Completion getSuggest() {
|
||||
return suggest;
|
||||
}
|
||||
public Completion getSuggest() {
|
||||
return suggest;
|
||||
}
|
||||
|
||||
public void setSuggest(Completion suggest) {
|
||||
this.suggest = suggest;
|
||||
}
|
||||
public void setSuggest(Completion suggest) {
|
||||
this.suggest = suggest;
|
||||
}
|
||||
}
|
||||
|
@ -24,52 +24,52 @@ import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||
*/
|
||||
public class CompletionEntityAnnotatedBuilder {
|
||||
|
||||
private CompletionAnnotatedEntity result;
|
||||
private CompletionAnnotatedEntity result;
|
||||
|
||||
public CompletionEntityAnnotatedBuilder(String id) {
|
||||
result = new CompletionAnnotatedEntity(id);
|
||||
}
|
||||
|
||||
public CompletionEntityAnnotatedBuilder name(String name) {
|
||||
result.setName(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CompletionEntityAnnotatedBuilder suggest(String[] input) {
|
||||
return suggest(input, null, null, null);
|
||||
}
|
||||
|
||||
public CompletionEntityAnnotatedBuilder suggest(String[] input, String output) {
|
||||
return suggest(input, output, null, null);
|
||||
}
|
||||
|
||||
public CompletionEntityAnnotatedBuilder suggest(String[] input, String output, Object payload) {
|
||||
return suggest(input, output, payload, null);
|
||||
}
|
||||
|
||||
public CompletionEntityAnnotatedBuilder suggest(String[] input, String output, Object payload, Integer weight) {
|
||||
Completion suggest = new Completion(input);
|
||||
if (output != null) {
|
||||
suggest.setOutput(output);
|
||||
public CompletionEntityAnnotatedBuilder(String id) {
|
||||
result = new CompletionAnnotatedEntity(id);
|
||||
}
|
||||
if (payload != null) {
|
||||
suggest.setPayload(payload);
|
||||
}
|
||||
if (weight != null) {
|
||||
suggest.setWeight(weight);
|
||||
}
|
||||
result.setSuggest(suggest);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CompletionAnnotatedEntity build() {
|
||||
return result;
|
||||
}
|
||||
public CompletionEntityAnnotatedBuilder name(String name) {
|
||||
result.setName(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
public CompletionEntityAnnotatedBuilder suggest(String[] input) {
|
||||
return suggest(input, null, null, null);
|
||||
}
|
||||
|
||||
public CompletionEntityAnnotatedBuilder suggest(String[] input, String output) {
|
||||
return suggest(input, output, null, null);
|
||||
}
|
||||
|
||||
public CompletionEntityAnnotatedBuilder suggest(String[] input, String output, Object payload) {
|
||||
return suggest(input, output, payload, null);
|
||||
}
|
||||
|
||||
public CompletionEntityAnnotatedBuilder suggest(String[] input, String output, Object payload, Integer weight) {
|
||||
Completion suggest = new Completion(input);
|
||||
if (output != null) {
|
||||
suggest.setOutput(output);
|
||||
}
|
||||
if (payload != null) {
|
||||
suggest.setPayload(payload);
|
||||
}
|
||||
if (weight != null) {
|
||||
suggest.setWeight(weight);
|
||||
}
|
||||
result.setSuggest(suggest);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CompletionAnnotatedEntity build() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
}
|
||||
|
@ -22,47 +22,47 @@ import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||
*/
|
||||
public class CompletionEntityBuilder {
|
||||
|
||||
private CompletionEntity result;
|
||||
private CompletionEntity result;
|
||||
|
||||
public CompletionEntityBuilder(String id) {
|
||||
result = new CompletionEntity(id);
|
||||
}
|
||||
public CompletionEntityBuilder(String id) {
|
||||
result = new CompletionEntity(id);
|
||||
}
|
||||
|
||||
public CompletionEntityBuilder name(String name) {
|
||||
result.setName(name);
|
||||
return this;
|
||||
}
|
||||
public CompletionEntityBuilder name(String name) {
|
||||
result.setName(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CompletionEntityBuilder suggest(String[] input) {
|
||||
return suggest(input, null, null, null);
|
||||
}
|
||||
public CompletionEntityBuilder suggest(String[] input) {
|
||||
return suggest(input, null, null, null);
|
||||
}
|
||||
|
||||
public CompletionEntityBuilder suggest(String[] input, String output) {
|
||||
return suggest(input, output, null, null);
|
||||
}
|
||||
public CompletionEntityBuilder suggest(String[] input, String output) {
|
||||
return suggest(input, output, null, null);
|
||||
}
|
||||
|
||||
public CompletionEntityBuilder suggest(String[] input, String output, Object payload) {
|
||||
return suggest(input, output, payload, null);
|
||||
}
|
||||
public CompletionEntityBuilder suggest(String[] input, String output, Object payload) {
|
||||
return suggest(input, output, payload, null);
|
||||
}
|
||||
|
||||
public CompletionEntityBuilder suggest(String[] input, String output, Object payload, Integer weight) {
|
||||
Completion suggest = new Completion(input);
|
||||
suggest.setOutput(output);
|
||||
suggest.setPayload(payload);
|
||||
suggest.setWeight(weight);
|
||||
public CompletionEntityBuilder suggest(String[] input, String output, Object payload, Integer weight) {
|
||||
Completion suggest = new Completion(input);
|
||||
suggest.setOutput(output);
|
||||
suggest.setPayload(payload);
|
||||
suggest.setWeight(weight);
|
||||
|
||||
result.setSuggest(suggest);
|
||||
return this;
|
||||
}
|
||||
result.setSuggest(suggest);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CompletionEntity build() {
|
||||
return result;
|
||||
}
|
||||
public CompletionEntity build() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,13 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core.completion;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.elasticsearch.action.suggest.SuggestResponse;
|
||||
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
|
||||
import org.elasticsearch.search.suggest.completion.CompletionSuggestionFuzzyBuilder;
|
||||
@ -45,88 +52,193 @@ import static org.junit.Assert.assertEquals;
|
||||
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
|
||||
public class ElasticsearchTemplateCompletionTests {
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
private void loadCompletionObjectEntities() {
|
||||
elasticsearchTemplate.deleteIndex(CompletionEntity.class);
|
||||
elasticsearchTemplate.createIndex(CompletionEntity.class);
|
||||
elasticsearchTemplate.refresh(CompletionEntity.class, true);
|
||||
elasticsearchTemplate.putMapping(CompletionEntity.class);
|
||||
private void loadCompletionObjectEntities() {
|
||||
elasticsearchTemplate.deleteIndex(CompletionEntity.class);
|
||||
elasticsearchTemplate.createIndex(CompletionEntity.class);
|
||||
elasticsearchTemplate.refresh(CompletionEntity.class, true);
|
||||
elasticsearchTemplate.putMapping(CompletionEntity.class);
|
||||
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
indexQueries.add(new CompletionEntityBuilder("1").name("Rizwan Idrees").suggest(new String[]{"Rizwan Idrees"}).buildIndex());
|
||||
indexQueries.add(new CompletionEntityBuilder("2").name("Franck Marchand").suggest(new String[]{"Franck", "Marchand"}).buildIndex());
|
||||
indexQueries.add(new CompletionEntityBuilder("3").name("Mohsin Husen").suggest(new String[]{"Mohsin", "Husen"}, "Mohsin Husen").buildIndex());
|
||||
indexQueries.add(new CompletionEntityBuilder("4").name("Artur Konczak").suggest(new String[]{"Artur", "Konczak"}, "Artur Konczak", null, 60).buildIndex());
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
indexQueries.add(new CompletionEntityBuilder("1").name("Rizwan Idrees").suggest(new String[]{"Rizwan Idrees"}).buildIndex());
|
||||
indexQueries.add(new CompletionEntityBuilder("2").name("Franck Marchand").suggest(new String[]{"Franck", "Marchand"}).buildIndex());
|
||||
indexQueries.add(new CompletionEntityBuilder("3").name("Mohsin Husen").suggest(new String[]{"Mohsin", "Husen"}, "Mohsin Husen").buildIndex());
|
||||
indexQueries.add(new CompletionEntityBuilder("4").name("Artur Konczak").suggest(new String[]{"Artur", "Konczak"}, "Artur Konczak").buildIndex());
|
||||
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(CompletionEntity.class, true);
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(CompletionEntity.class, true);
|
||||
}
|
||||
|
||||
private void loadAnnotatedCompletionObjectEntities() {
|
||||
elasticsearchTemplate.deleteIndex(AnnotatedCompletionEntity.class);
|
||||
elasticsearchTemplate.createIndex(AnnotatedCompletionEntity.class);
|
||||
elasticsearchTemplate.refresh(AnnotatedCompletionEntity.class, true);
|
||||
elasticsearchTemplate.putMapping(AnnotatedCompletionEntity.class);
|
||||
|
||||
NonDocumentEntity nonDocumentEntity = new NonDocumentEntity();
|
||||
nonDocumentEntity.setSomeField1("foo");
|
||||
nonDocumentEntity.setSomeField2("bar");
|
||||
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
indexQueries.add(new AnnotatedCompletionEntityBuilder("1").name("Franck Marchand").suggest(new String[]{"Franck", "Marchand"}).buildIndex());
|
||||
indexQueries.add(new AnnotatedCompletionEntityBuilder("2").name("Mohsin Husen").suggest(new String[]{"Mohsin", "Husen"}, "Mohsin Husen").buildIndex());
|
||||
indexQueries.add(new AnnotatedCompletionEntityBuilder("3").name("Rizwan Idrees").suggest(new String[]{"Rizwan", "Idrees"}, "Rizwan Idrees").buildIndex());
|
||||
indexQueries.add(new AnnotatedCompletionEntityBuilder("4").name("Artur Konczak").suggest(new String[]{"Artur", "Konczak"}, "Artur Konczak").buildIndex());
|
||||
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(AnnotatedCompletionEntity.class, true);
|
||||
}
|
||||
|
||||
private void loadAnnotatedCompletionObjectEntitiesWithPayloads() {
|
||||
elasticsearchTemplate.deleteIndex(AnnotatedCompletionEntity.class);
|
||||
elasticsearchTemplate.createIndex(AnnotatedCompletionEntity.class);
|
||||
elasticsearchTemplate.refresh(AnnotatedCompletionEntity.class, true);
|
||||
elasticsearchTemplate.putMapping(AnnotatedCompletionEntity.class);
|
||||
|
||||
NonDocumentEntity nonDocumentEntity = new NonDocumentEntity();
|
||||
nonDocumentEntity.setSomeField1("Payload");
|
||||
nonDocumentEntity.setSomeField2("test");
|
||||
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
indexQueries.add(new AnnotatedCompletionEntityBuilder("1").name("Mewes Kochheim1").suggest(new String[]{"Mewes Kochheim1"}, null, Double.MAX_VALUE).buildIndex());
|
||||
indexQueries.add(new AnnotatedCompletionEntityBuilder("2").name("Mewes Kochheim2").suggest(new String[]{"Mewes Kochheim2"}, null, Long.MAX_VALUE).buildIndex());
|
||||
indexQueries.add(new AnnotatedCompletionEntityBuilder("3").name("Mewes Kochheim3").suggest(new String[]{"Mewes Kochheim3"}, null, "Payload test").buildIndex());
|
||||
indexQueries.add(new AnnotatedCompletionEntityBuilder("4").name("Mewes Kochheim4").suggest(new String[]{"Mewes Kochheim4"}, null, nonDocumentEntity).buildIndex());
|
||||
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(AnnotatedCompletionEntity.class, true);
|
||||
}
|
||||
|
||||
private void loadAnnotatedCompletionObjectEntitiesWithWeights() {
|
||||
elasticsearchTemplate.deleteIndex(AnnotatedCompletionEntity.class);
|
||||
elasticsearchTemplate.createIndex(AnnotatedCompletionEntity.class);
|
||||
elasticsearchTemplate.refresh(AnnotatedCompletionEntity.class, true);
|
||||
elasticsearchTemplate.putMapping(AnnotatedCompletionEntity.class);
|
||||
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
indexQueries.add(new AnnotatedCompletionEntityBuilder("1").name("Mewes Kochheim1").suggest(new String[]{"Mewes Kochheim1"}).buildIndex());
|
||||
indexQueries.add(new AnnotatedCompletionEntityBuilder("2").name("Mewes Kochheim2").suggest(new String[]{"Mewes Kochheim2"}, null, null, 0).buildIndex());
|
||||
indexQueries.add(new AnnotatedCompletionEntityBuilder("3").name("Mewes Kochheim3").suggest(new String[]{"Mewes Kochheim3"}, null, null, 1).buildIndex());
|
||||
indexQueries.add(new AnnotatedCompletionEntityBuilder("4").name("Mewes Kochheim4").suggest(new String[]{"Mewes Kochheim4"}, null, null, Integer.MAX_VALUE).buildIndex());
|
||||
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(AnnotatedCompletionEntity.class, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPutMappingForGivenEntity() throws Exception {
|
||||
//given
|
||||
Class entity = CompletionEntity.class;
|
||||
elasticsearchTemplate.createIndex(entity);
|
||||
|
||||
//when
|
||||
assertThat(elasticsearchTemplate.putMapping(entity), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindSuggestionsForGivenCriteriaQueryUsingCompletionEntity() {
|
||||
//given
|
||||
loadCompletionObjectEntities();
|
||||
CompletionSuggestionFuzzyBuilder completionSuggestionFuzzyBuilder = new CompletionSuggestionFuzzyBuilder("test-suggest")
|
||||
.text("m")
|
||||
.field("suggest");
|
||||
|
||||
//when
|
||||
SuggestResponse suggestResponse = elasticsearchTemplate.suggest(completionSuggestionFuzzyBuilder, CompletionEntity.class);
|
||||
CompletionSuggestion completionSuggestion = suggestResponse.getSuggest().getSuggestion("test-suggest");
|
||||
List<CompletionSuggestion.Entry.Option> options = completionSuggestion.getEntries().get(0).getOptions();
|
||||
|
||||
//then
|
||||
assertThat(options.size(), is(2));
|
||||
assertThat(options.get(0).getText().string(), isOneOf("Marchand", "Mohsin Husen"));
|
||||
assertThat(options.get(1).getText().string(), isOneOf("Marchand", "Mohsin Husen"));
|
||||
}
|
||||
|
||||
private void loadAnnotatedCompletionObjectEntities() {
|
||||
elasticsearchTemplate.deleteIndex(AnnotatedCompletionEntity.class);
|
||||
elasticsearchTemplate.createIndex(AnnotatedCompletionEntity.class);
|
||||
elasticsearchTemplate.refresh(AnnotatedCompletionEntity.class, true);
|
||||
elasticsearchTemplate.putMapping(AnnotatedCompletionEntity.class);
|
||||
@Test
|
||||
public void shouldFindSuggestionsForGivenCriteriaQueryUsingAnnotatedCompletionEntity() {
|
||||
//given
|
||||
loadAnnotatedCompletionObjectEntities();
|
||||
CompletionSuggestionFuzzyBuilder completionSuggestionFuzzyBuilder = new CompletionSuggestionFuzzyBuilder("test-suggest")
|
||||
.text("m")
|
||||
.field("suggest");
|
||||
|
||||
NonDocumentEntity nonDocumentEntity = new NonDocumentEntity();
|
||||
nonDocumentEntity.setSomeField1("foo");
|
||||
nonDocumentEntity.setSomeField2("bar");
|
||||
//when
|
||||
SuggestResponse suggestResponse = elasticsearchTemplate.suggest(completionSuggestionFuzzyBuilder, CompletionEntity.class);
|
||||
CompletionSuggestion completionSuggestion = suggestResponse.getSuggest().getSuggestion("test-suggest");
|
||||
List<CompletionSuggestion.Entry.Option> options = completionSuggestion.getEntries().get(0).getOptions();
|
||||
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
indexQueries.add(new AnnotatedCompletionEntityBuilder("1").name("Franck Marchand").suggest(new String[]{"Franck", "Marchand"}).buildIndex());
|
||||
indexQueries.add(new AnnotatedCompletionEntityBuilder("2").name("Mohsin Husen").suggest(new String[]{"Mohsin", "Husen"}, "Mohsin Husen").buildIndex());
|
||||
indexQueries.add(new AnnotatedCompletionEntityBuilder("3").name("Rizwan Idrees").suggest(new String[]{"Rizwan", "Idrees"}, "Rizwan Idrees", "Payload test").buildIndex());
|
||||
indexQueries.add(new AnnotatedCompletionEntityBuilder("4").name("Artur Konczak").suggest(new String[]{"Artur", "Konczak"}, "Artur Konczak", nonDocumentEntity, 60).buildIndex());
|
||||
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(AnnotatedCompletionEntity.class, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPutMappingForGivenEntity() throws Exception {
|
||||
//given
|
||||
Class entity = CompletionEntity.class;
|
||||
elasticsearchTemplate.createIndex(entity);
|
||||
|
||||
//when
|
||||
assertThat(elasticsearchTemplate.putMapping(entity), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindSuggestionsForGivenCriteriaQueryUsingCompletionObjectEntity() {
|
||||
//given
|
||||
loadCompletionObjectEntities();
|
||||
CompletionSuggestionFuzzyBuilder completionSuggestionFuzzyBuilder = new CompletionSuggestionFuzzyBuilder("test-suggest")
|
||||
.text("m")
|
||||
.field("suggest");
|
||||
|
||||
//when
|
||||
SuggestResponse suggestResponse = elasticsearchTemplate.suggest(completionSuggestionFuzzyBuilder, CompletionEntity.class);
|
||||
CompletionSuggestion completionSuggestion = suggestResponse.getSuggest().getSuggestion("test-suggest");
|
||||
List<CompletionSuggestion.Entry.Option> options = completionSuggestion.getEntries().get(0).getOptions();
|
||||
|
||||
//then
|
||||
assertThat(options.size(), is(2));
|
||||
//then
|
||||
assertThat(options.size(), is(2));
|
||||
assertThat(options.get(0).getText().string(), isOneOf("Marchand","Mohsin Husen"));
|
||||
}
|
||||
assertThat(options.get(1).getText().string(), isOneOf("Marchand","Mohsin Husen"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindSuggestionsForGivenCriteriaQueryUsingloadAnnotatedCompletionObjectEntity() {
|
||||
//given
|
||||
loadCompletionObjectEntities();
|
||||
CompletionSuggestionFuzzyBuilder completionSuggestionFuzzyBuilder = new CompletionSuggestionFuzzyBuilder("test-suggest")
|
||||
.text("m")
|
||||
.field("suggest");
|
||||
@Test
|
||||
public void shouldFindSuggestionsWithPayloadsForGivenCriteriaQueryUsingAnnotatedCompletionEntity() {
|
||||
//given
|
||||
loadAnnotatedCompletionObjectEntitiesWithPayloads();
|
||||
CompletionSuggestionFuzzyBuilder completionSuggestionFuzzyBuilder = new CompletionSuggestionFuzzyBuilder("test-suggest")
|
||||
.text("m")
|
||||
.field("suggest");
|
||||
|
||||
//when
|
||||
SuggestResponse suggestResponse = elasticsearchTemplate.suggest(completionSuggestionFuzzyBuilder, CompletionEntity.class);
|
||||
CompletionSuggestion completionSuggestion = suggestResponse.getSuggest().getSuggestion("test-suggest");
|
||||
List<CompletionSuggestion.Entry.Option> options = completionSuggestion.getEntries().get(0).getOptions();
|
||||
//when
|
||||
SuggestResponse suggestResponse = elasticsearchTemplate.suggest(completionSuggestionFuzzyBuilder, CompletionEntity.class);
|
||||
CompletionSuggestion completionSuggestion = suggestResponse.getSuggest().getSuggestion("test-suggest");
|
||||
List<CompletionSuggestion.Entry.Option> options = completionSuggestion.getEntries().get(0).getOptions();
|
||||
|
||||
//then
|
||||
assertThat(options.size(), is(2));
|
||||
assertThat(options.get(0).getText().string(), isOneOf("Marchand","Mohsin Husen"));
|
||||
}
|
||||
//then
|
||||
assertThat(options.size(), is(4));
|
||||
for (CompletionSuggestion.Entry.Option option : options) {
|
||||
if (option.getText().string().equals("Mewes Kochheim1")) {
|
||||
assertEquals(Double.MAX_VALUE, option.getPayloadAsDouble(), 0);
|
||||
}
|
||||
else if (option.getText().string().equals("Mewes Kochheim2")) {
|
||||
assertEquals(Long.MAX_VALUE, option.getPayloadAsLong());
|
||||
}
|
||||
else if (option.getText().string().equals("Mewes Kochheim3")) {
|
||||
assertEquals("Payload test", option.getPayloadAsString());
|
||||
}
|
||||
else if (option.getText().string().equals("Mewes Kochheim4")) {
|
||||
assertEquals("Payload", option.getPayloadAsMap().get("someField1"));
|
||||
assertEquals("test", option.getPayloadAsMap().get("someField2"));
|
||||
} else {
|
||||
fail("Unexpected option");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindSuggestionsWithWeightsForGivenCriteriaQueryUsingAnnotatedCompletionEntity() {
|
||||
//given
|
||||
loadAnnotatedCompletionObjectEntitiesWithWeights();
|
||||
CompletionSuggestionFuzzyBuilder completionSuggestionFuzzyBuilder = new CompletionSuggestionFuzzyBuilder("test-suggest")
|
||||
.text("m")
|
||||
.field("suggest");
|
||||
|
||||
//when
|
||||
SuggestResponse suggestResponse = elasticsearchTemplate.suggest(completionSuggestionFuzzyBuilder, CompletionEntity.class);
|
||||
CompletionSuggestion completionSuggestion = suggestResponse.getSuggest().getSuggestion("test-suggest");
|
||||
List<CompletionSuggestion.Entry.Option> options = completionSuggestion.getEntries().get(0).getOptions();
|
||||
|
||||
//then
|
||||
assertThat(options.size(), is(4));
|
||||
for (CompletionSuggestion.Entry.Option option : options) {
|
||||
if (option.getText().string().equals("Mewes Kochheim1")) {
|
||||
assertEquals(4, option.getScore(), 0);
|
||||
}
|
||||
else if (option.getText().string().equals("Mewes Kochheim2")) {
|
||||
assertEquals(0, option.getScore(), 0);
|
||||
}
|
||||
else if (option.getText().string().equals("Mewes Kochheim3")) {
|
||||
assertEquals(1, option.getScore(), 0);
|
||||
}
|
||||
else if (option.getText().string().equals("Mewes Kochheim4")) {
|
||||
assertEquals(Integer.MAX_VALUE, option.getScore(), 0);
|
||||
} else {
|
||||
fail("Unexpected option");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user