Search in sources :

Example 1 with CompletionSuggestion

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());
}
Also used : ArrayList(java.util.ArrayList) Suggest(org.springframework.data.elasticsearch.core.suggest.response.Suggest) CompletionSuggestion(org.springframework.data.elasticsearch.core.suggest.response.CompletionSuggestion) PhraseSuggestion(org.springframework.data.elasticsearch.core.suggest.response.PhraseSuggestion) TermSuggestion(org.springframework.data.elasticsearch.core.suggest.response.TermSuggestion) TermSuggestion(org.springframework.data.elasticsearch.core.suggest.response.TermSuggestion) PhraseSuggestion(org.springframework.data.elasticsearch.core.suggest.response.PhraseSuggestion) CompletionSuggestion(org.springframework.data.elasticsearch.core.suggest.response.CompletionSuggestion) Nullable(org.springframework.lang.Nullable)

Example 2 with CompletionSuggestion

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");
}
Also used : CompletionSuggestion(org.springframework.data.elasticsearch.core.suggest.response.CompletionSuggestion) CompletionSuggestion(org.springframework.data.elasticsearch.core.suggest.response.CompletionSuggestion) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) Suggest(org.springframework.data.elasticsearch.core.suggest.response.Suggest) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 3 with CompletionSuggestion

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;
        }
    }
}
Also used : CompletionSuggestion(org.springframework.data.elasticsearch.core.suggest.response.CompletionSuggestion) Suggest(org.springframework.data.elasticsearch.core.suggest.response.Suggest) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) CompletionSuggestion(org.springframework.data.elasticsearch.core.suggest.response.CompletionSuggestion) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 4 with CompletionSuggestion

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");
}
Also used : CompletionSuggestion(org.springframework.data.elasticsearch.core.suggest.response.CompletionSuggestion) CompletionSuggestion(org.springframework.data.elasticsearch.core.suggest.response.CompletionSuggestion) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) Suggest(org.springframework.data.elasticsearch.core.suggest.response.Suggest) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Aggregations

CompletionSuggestion (org.springframework.data.elasticsearch.core.suggest.response.CompletionSuggestion)4 Suggest (org.springframework.data.elasticsearch.core.suggest.response.Suggest)4 SuggestBuilder (org.elasticsearch.search.suggest.SuggestBuilder)3 Test (org.junit.jupiter.api.Test)3 NativeSearchQuery (org.springframework.data.elasticsearch.core.query.NativeSearchQuery)3 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)3 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)3 ArrayList (java.util.ArrayList)1 PhraseSuggestion (org.springframework.data.elasticsearch.core.suggest.response.PhraseSuggestion)1 TermSuggestion (org.springframework.data.elasticsearch.core.suggest.response.TermSuggestion)1 Nullable (org.springframework.lang.Nullable)1