Search in sources :

Example 1 with PhraseSuggestion

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

Aggregations

ArrayList (java.util.ArrayList)1 CompletionSuggestion (org.springframework.data.elasticsearch.core.suggest.response.CompletionSuggestion)1 PhraseSuggestion (org.springframework.data.elasticsearch.core.suggest.response.PhraseSuggestion)1 Suggest (org.springframework.data.elasticsearch.core.suggest.response.Suggest)1 TermSuggestion (org.springframework.data.elasticsearch.core.suggest.response.TermSuggestion)1 Nullable (org.springframework.lang.Nullable)1