Search in sources :

Example 1 with Suggest

use of org.springframework.data.elasticsearch.core.suggest.response.Suggest 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 Suggest

use of org.springframework.data.elasticsearch.core.suggest.response.Suggest in project spring-data-elasticsearch by spring-projects.

the class SearchDocumentResponse method from.

/**
 * creates a {@link SearchDocumentResponse} from {@link SearchHits} with the given scrollId aggregations and suggest
 *
 * @param searchHits the {@link SearchHits} to process
 * @param scrollId scrollId
 * @param aggregations aggregations
 * @param suggestES the suggestion response from Elasticsearch
 * @param entityCreator function to create an entity from a {@link SearchDocument}
 * @param <T> entity type
 * @return the {@link SearchDocumentResponse}
 * @since 4.3
 */
public static <T> SearchDocumentResponse from(SearchHits searchHits, @Nullable String scrollId, @Nullable Aggregations aggregations, @Nullable org.elasticsearch.search.suggest.Suggest suggestES, EntityCreator<T> entityCreator) {
    TotalHits responseTotalHits = searchHits.getTotalHits();
    long totalHits;
    String totalHitsRelation;
    if (responseTotalHits != null) {
        totalHits = responseTotalHits.value;
        totalHitsRelation = responseTotalHits.relation.name();
    } else {
        totalHits = searchHits.getHits().length;
        totalHitsRelation = "OFF";
    }
    float maxScore = searchHits.getMaxScore();
    List<SearchDocument> searchDocuments = new ArrayList<>();
    for (SearchHit searchHit : searchHits) {
        if (searchHit != null) {
            searchDocuments.add(DocumentAdapters.from(searchHit));
        }
    }
    Suggest suggest = suggestFrom(suggestES, entityCreator);
    return new SearchDocumentResponse(totalHits, totalHitsRelation, maxScore, scrollId, searchDocuments, aggregations, suggest);
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) Suggest(org.springframework.data.elasticsearch.core.suggest.response.Suggest)

Example 3 with Suggest

use of org.springframework.data.elasticsearch.core.suggest.response.Suggest 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 4 with Suggest

use of org.springframework.data.elasticsearch.core.suggest.response.Suggest 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 5 with Suggest

use of org.springframework.data.elasticsearch.core.suggest.response.Suggest in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchTemplate method suggest.

@Override
public Mono<Suggest> suggest(Query query, Class<?> entityType, IndexCoordinates index) {
    Assert.notNull(query, "query must not be null");
    Assert.notNull(entityType, "entityType must not be null");
    Assert.notNull(index, "index must not be null");
    return doFindForResponse(query, entityType, index).mapNotNull(searchDocumentResponse -> {
        Suggest suggest = searchDocumentResponse.getSuggest();
        SearchHitMapping.mappingFor(entityType, converter).mapHitsInCompletionSuggestion(suggest);
        return suggest;
    });
}
Also used : Suggest(org.springframework.data.elasticsearch.core.suggest.response.Suggest)

Aggregations

Suggest (org.springframework.data.elasticsearch.core.suggest.response.Suggest)7 CompletionSuggestion (org.springframework.data.elasticsearch.core.suggest.response.CompletionSuggestion)4 ArrayList (java.util.ArrayList)3 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 TotalHits (org.apache.lucene.search.TotalHits)1 SearchHit (org.elasticsearch.search.SearchHit)1 SearchDocument (org.springframework.data.elasticsearch.core.document.SearchDocument)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