Search in sources :

Example 1 with SuggestionContext

use of org.elasticsearch.search.suggest.SuggestionSearchContext.SuggestionContext in project elasticsearch by elastic.

the class SuggestBuilder method build.

public SuggestionSearchContext build(QueryShardContext context) throws IOException {
    SuggestionSearchContext suggestionSearchContext = new SuggestionSearchContext();
    for (Entry<String, SuggestionBuilder<?>> suggestion : suggestions.entrySet()) {
        SuggestionContext suggestionContext = suggestion.getValue().build(context);
        if (suggestionContext.getText() == null) {
            if (globalText == null) {
                throw new IllegalArgumentException("The required text option is missing");
            }
            suggestionContext.setText(BytesRefs.toBytesRef(globalText));
        }
        suggestionSearchContext.addSuggestion(suggestion.getKey(), suggestionContext);
    }
    return suggestionSearchContext;
}
Also used : SuggestionContext(org.elasticsearch.search.suggest.SuggestionSearchContext.SuggestionContext)

Example 2 with SuggestionContext

use of org.elasticsearch.search.suggest.SuggestionSearchContext.SuggestionContext in project elasticsearch by elastic.

the class SuggestPhase method execute.

@Override
public void execute(SearchContext context) {
    final SuggestionSearchContext suggest = context.suggest();
    if (suggest == null) {
        return;
    }
    try {
        CharsRefBuilder spare = new CharsRefBuilder();
        final List<Suggestion<? extends Entry<? extends Option>>> suggestions = new ArrayList<>(suggest.suggestions().size());
        for (Map.Entry<String, SuggestionSearchContext.SuggestionContext> entry : suggest.suggestions().entrySet()) {
            SuggestionSearchContext.SuggestionContext suggestion = entry.getValue();
            Suggester<SuggestionContext> suggester = suggestion.getSuggester();
            Suggestion<? extends Entry<? extends Option>> result = suggester.execute(entry.getKey(), suggestion, context.searcher(), spare);
            if (result != null) {
                assert entry.getKey().equals(result.name);
                suggestions.add(result);
            }
        }
        context.queryResult().suggest(new Suggest(suggestions));
    } catch (IOException e) {
        throw new ElasticsearchException("I/O exception during suggest phase", e);
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) SuggestionContext(org.elasticsearch.search.suggest.SuggestionSearchContext.SuggestionContext) Suggestion(org.elasticsearch.search.suggest.Suggest.Suggestion) Entry(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry) SuggestionContext(org.elasticsearch.search.suggest.SuggestionSearchContext.SuggestionContext) Option(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option) CharsRefBuilder(org.apache.lucene.util.CharsRefBuilder) Map(java.util.Map)

Aggregations

SuggestionContext (org.elasticsearch.search.suggest.SuggestionSearchContext.SuggestionContext)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 CharsRefBuilder (org.apache.lucene.util.CharsRefBuilder)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 Suggestion (org.elasticsearch.search.suggest.Suggest.Suggestion)1 Entry (org.elasticsearch.search.suggest.Suggest.Suggestion.Entry)1 Option (org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option)1