use of org.springframework.data.elasticsearch.core.suggest.response.CompletionSuggestion in project spring-data-elasticsearch by spring-projects.
the class SearchDocumentResponse method suggestFrom.
@Nullable
private static <T> Suggest suggestFrom(@Nullable org.elasticsearch.search.suggest.Suggest suggestES, EntityCreator<T> entityCreator) {
if (suggestES == null) {
return null;
}
List<Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>>> suggestions = new ArrayList<>();
for (org.elasticsearch.search.suggest.Suggest.Suggestion<? extends org.elasticsearch.search.suggest.Suggest.Suggestion.Entry<? extends org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option>> suggestionES : suggestES) {
if (suggestionES instanceof org.elasticsearch.search.suggest.term.TermSuggestion) {
org.elasticsearch.search.suggest.term.TermSuggestion termSuggestionES = (org.elasticsearch.search.suggest.term.TermSuggestion) suggestionES;
List<TermSuggestion.Entry> entries = new ArrayList<>();
for (org.elasticsearch.search.suggest.term.TermSuggestion.Entry entryES : termSuggestionES) {
List<TermSuggestion.Entry.Option> options = new ArrayList<>();
for (org.elasticsearch.search.suggest.term.TermSuggestion.Entry.Option optionES : entryES) {
options.add(new TermSuggestion.Entry.Option(textToString(optionES.getText()), textToString(optionES.getHighlighted()), optionES.getScore(), optionES.collateMatch(), optionES.getFreq()));
}
entries.add(new TermSuggestion.Entry(textToString(entryES.getText()), entryES.getOffset(), entryES.getLength(), options));
}
suggestions.add(new TermSuggestion(termSuggestionES.getName(), termSuggestionES.getSize(), entries, suggestFrom(termSuggestionES.getSort())));
}
if (suggestionES instanceof org.elasticsearch.search.suggest.phrase.PhraseSuggestion) {
org.elasticsearch.search.suggest.phrase.PhraseSuggestion phraseSuggestionES = (org.elasticsearch.search.suggest.phrase.PhraseSuggestion) suggestionES;
List<PhraseSuggestion.Entry> entries = new ArrayList<>();
for (org.elasticsearch.search.suggest.phrase.PhraseSuggestion.Entry entryES : phraseSuggestionES) {
List<PhraseSuggestion.Entry.Option> options = new ArrayList<>();
for (org.elasticsearch.search.suggest.phrase.PhraseSuggestion.Entry.Option optionES : entryES) {
options.add(new PhraseSuggestion.Entry.Option(textToString(optionES.getText()), textToString(optionES.getHighlighted()), optionES.getScore(), optionES.collateMatch()));
}
entries.add(new PhraseSuggestion.Entry(textToString(entryES.getText()), entryES.getOffset(), entryES.getLength(), options, entryES.getCutoffScore()));
}
suggestions.add(new PhraseSuggestion(phraseSuggestionES.getName(), phraseSuggestionES.getSize(), entries));
}
if (suggestionES instanceof org.elasticsearch.search.suggest.completion.CompletionSuggestion) {
org.elasticsearch.search.suggest.completion.CompletionSuggestion completionSuggestionES = (org.elasticsearch.search.suggest.completion.CompletionSuggestion) suggestionES;
List<CompletionSuggestion.Entry<T>> entries = new ArrayList<>();
for (org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry entryES : completionSuggestionES) {
List<CompletionSuggestion.Entry.Option<T>> options = new ArrayList<>();
for (org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option optionES : entryES) {
SearchDocument searchDocument = optionES.getHit() != null ? DocumentAdapters.from(optionES.getHit()) : null;
T hitEntity = null;
if (searchDocument != null) {
try {
hitEntity = entityCreator.apply(searchDocument).get();
} catch (Exception e) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Error creating entity from SearchDocument");
}
}
}
options.add(new CompletionSuggestion.Entry.Option<T>(textToString(optionES.getText()), textToString(optionES.getHighlighted()), optionES.getScore(), optionES.collateMatch(), optionES.getContexts(), scoreDocFrom(optionES.getDoc()), searchDocument, hitEntity));
}
entries.add(new CompletionSuggestion.Entry<T>(textToString(entryES.getText()), entryES.getOffset(), entryES.getLength(), options));
}
suggestions.add(new CompletionSuggestion<T>(completionSuggestionES.getName(), completionSuggestionES.getSize(), entries));
}
}
return new Suggest(suggestions, suggestES.hasScoreDocs());
}
use of org.springframework.data.elasticsearch.core.suggest.response.CompletionSuggestion in project spring-data-elasticsearch by spring-projects.
the class CompletionIntegrationTests method shouldFindSuggestionsForGivenCriteriaQueryUsingAnnotatedCompletionEntity.
@Test
public void shouldFindSuggestionsForGivenCriteriaQueryUsingAnnotatedCompletionEntity() {
loadAnnotatedCompletionObjectEntities();
NativeSearchQuery query = new NativeSearchQueryBuilder().withSuggestBuilder(new SuggestBuilder().addSuggestion("test-suggest", SuggestBuilders.completionSuggestion("suggest").prefix("m", Fuzziness.AUTO))).build();
SearchHits<AnnotatedCompletionEntity> searchHits = operations.search(query, AnnotatedCompletionEntity.class);
assertThat(searchHits.hasSuggest()).isTrue();
Suggest suggest = searchHits.getSuggest();
// noinspection ConstantConditions
Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> suggestion = suggest.getSuggestion("test-suggest");
assertThat(suggestion).isNotNull();
assertThat(suggestion).isInstanceOf(CompletionSuggestion.class);
// noinspection unchecked
List<CompletionSuggestion.Entry.Option<AnnotatedCompletionEntity>> options = ((CompletionSuggestion<AnnotatedCompletionEntity>) suggestion).getEntries().get(0).getOptions();
assertThat(options).hasSize(2);
assertThat(options.get(0).getText()).isIn("Marchand", "Mohsin");
assertThat(options.get(1).getText()).isIn("Marchand", "Mohsin");
}
use of org.springframework.data.elasticsearch.core.suggest.response.CompletionSuggestion in project spring-data-elasticsearch by spring-projects.
the class CompletionIntegrationTests method shouldFindSuggestionsWithWeightsForGivenCriteriaQueryUsingAnnotatedCompletionEntity.
@Test
public void shouldFindSuggestionsWithWeightsForGivenCriteriaQueryUsingAnnotatedCompletionEntity() {
loadAnnotatedCompletionObjectEntitiesWithWeights();
NativeSearchQuery query = new NativeSearchQueryBuilder().withSuggestBuilder(new SuggestBuilder().addSuggestion("test-suggest", SuggestBuilders.completionSuggestion("suggest").prefix("m", Fuzziness.AUTO))).build();
SearchHits<AnnotatedCompletionEntity> searchHits = operations.search(query, AnnotatedCompletionEntity.class);
assertThat(searchHits.hasSuggest()).isTrue();
Suggest suggest = searchHits.getSuggest();
// noinspection ConstantConditions
Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> suggestion = suggest.getSuggestion("test-suggest");
assertThat(suggestion).isNotNull();
assertThat(suggestion).isInstanceOf(CompletionSuggestion.class);
// noinspection unchecked
List<CompletionSuggestion.Entry.Option<AnnotatedCompletionEntity>> options = ((CompletionSuggestion<AnnotatedCompletionEntity>) suggestion).getEntries().get(0).getOptions();
assertThat(options).hasSize(4);
for (CompletionSuggestion.Entry.Option<AnnotatedCompletionEntity> option : options) {
switch(option.getText()) {
case "Mewes Kochheim1":
assertThat(option.getScore()).isEqualTo(4);
break;
case "Mewes Kochheim2":
assertThat(option.getScore()).isEqualTo(1);
break;
case "Mewes Kochheim3":
assertThat(option.getScore()).isEqualTo(2);
break;
case "Mewes Kochheim4":
assertThat(option.getScore()).isEqualTo(Integer.MAX_VALUE);
break;
default:
fail("Unexpected option");
break;
}
}
}
use of org.springframework.data.elasticsearch.core.suggest.response.CompletionSuggestion in project spring-data-elasticsearch by spring-projects.
the class CompletionIntegrationTests method shouldFindSuggestionsForGivenCriteriaQueryUsingCompletionEntity.
@Test
public void shouldFindSuggestionsForGivenCriteriaQueryUsingCompletionEntity() {
loadCompletionObjectEntities();
NativeSearchQuery query = new NativeSearchQueryBuilder().withSuggestBuilder(new SuggestBuilder().addSuggestion("test-suggest", SuggestBuilders.completionSuggestion("suggest").prefix("m", Fuzziness.AUTO))).build();
SearchHits<CompletionEntity> searchHits = operations.search(query, CompletionEntity.class);
assertThat(searchHits.hasSuggest()).isTrue();
Suggest suggest = searchHits.getSuggest();
// noinspection ConstantConditions
Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> suggestion = suggest.getSuggestion("test-suggest");
assertThat(suggestion).isNotNull();
assertThat(suggestion).isInstanceOf(CompletionSuggestion.class);
// noinspection unchecked
List<CompletionSuggestion.Entry.Option<AnnotatedCompletionEntity>> options = ((CompletionSuggestion<AnnotatedCompletionEntity>) suggestion).getEntries().get(0).getOptions();
assertThat(options).hasSize(2);
assertThat(options.get(0).getText()).isIn("Marchand", "Mohsin");
assertThat(options.get(1).getText()).isIn("Marchand", "Mohsin");
}
Aggregations