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;
}
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);
}
}
Aggregations