mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-08 21:22:12 +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";
|
||||
|
@ -54,6 +54,11 @@ 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";
|
||||
@ -118,7 +123,8 @@ class MappingBuilder {
|
||||
}
|
||||
|
||||
if (isCompletionField) {
|
||||
applyCompletionFieldMapping(xContentBuilder, field);
|
||||
CompletionField completionField = field.getAnnotation(CompletionField.class);
|
||||
applyCompletionFieldMapping(xContentBuilder, field, completionField);
|
||||
}
|
||||
|
||||
if (isRootObject && singleField != null && isIdField(field, idFieldName)) {
|
||||
@ -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,10 +166,22 @@ class MappingBuilder {
|
||||
.endObject();
|
||||
}
|
||||
|
||||
private static void applyCompletionFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field) throws IOException {
|
||||
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)
|
||||
.endObject();
|
||||
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)
|
||||
|
@ -24,10 +24,10 @@ 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);
|
||||
result = new AnnotatedCompletionEntity(id);
|
||||
}
|
||||
|
||||
public AnnotatedCompletionEntityBuilder name(String name) {
|
||||
@ -57,7 +57,7 @@ public class AnnotatedCompletionEntityBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public CompletionEntity build() {
|
||||
public AnnotatedCompletionEntity build() {
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
@ -58,7 +65,7 @@ public class ElasticsearchTemplateCompletionTests {
|
||||
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());
|
||||
indexQueries.add(new CompletionEntityBuilder("4").name("Artur Konczak").suggest(new String[]{"Artur", "Konczak"}, "Artur Konczak").buildIndex());
|
||||
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(CompletionEntity.class, true);
|
||||
@ -77,8 +84,44 @@ public class ElasticsearchTemplateCompletionTests {
|
||||
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());
|
||||
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);
|
||||
@ -95,7 +138,7 @@ public class ElasticsearchTemplateCompletionTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindSuggestionsForGivenCriteriaQueryUsingCompletionObjectEntity() {
|
||||
public void shouldFindSuggestionsForGivenCriteriaQueryUsingCompletionEntity() {
|
||||
//given
|
||||
loadCompletionObjectEntities();
|
||||
CompletionSuggestionFuzzyBuilder completionSuggestionFuzzyBuilder = new CompletionSuggestionFuzzyBuilder("test-suggest")
|
||||
@ -109,13 +152,14 @@ public class ElasticsearchTemplateCompletionTests {
|
||||
|
||||
//then
|
||||
assertThat(options.size(), is(2));
|
||||
assertThat(options.get(0).getText().string(), isOneOf("Marchand","Mohsin Husen"));
|
||||
assertThat(options.get(0).getText().string(), isOneOf("Marchand", "Mohsin Husen"));
|
||||
assertThat(options.get(1).getText().string(), isOneOf("Marchand", "Mohsin Husen"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindSuggestionsForGivenCriteriaQueryUsingloadAnnotatedCompletionObjectEntity() {
|
||||
public void shouldFindSuggestionsForGivenCriteriaQueryUsingAnnotatedCompletionEntity() {
|
||||
//given
|
||||
loadCompletionObjectEntities();
|
||||
loadAnnotatedCompletionObjectEntities();
|
||||
CompletionSuggestionFuzzyBuilder completionSuggestionFuzzyBuilder = new CompletionSuggestionFuzzyBuilder("test-suggest")
|
||||
.text("m")
|
||||
.field("suggest");
|
||||
@ -128,5 +172,73 @@ public class ElasticsearchTemplateCompletionTests {
|
||||
//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 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();
|
||||
|
||||
//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