org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder Java Examples

The following examples show how to use org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ElasticSearchTypeImpl.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> complete(CompleteRequest request) {
    var watch = new StopWatch();
    long esTook = 0;
    String index = request.index == null ? this.index : request.index;
    int options = 0;
    try {
        var suggest = new SuggestBuilder().setGlobalText(request.prefix);
        for (String field : request.fields) {
            CompletionSuggestionBuilder suggestion = SuggestBuilders.completionSuggestion(field).skipDuplicates(true);
            if (request.limit != null) suggestion.size(request.limit);
            suggest.addSuggestion("completion:" + field, suggestion);
        }

        var searchRequest = searchRequest(index);
        searchRequest.source().fetchSource(false).suggest(suggest);

        org.elasticsearch.action.search.SearchResponse response = search(searchRequest);
        esTook = response.getTook().nanos();

        List<String> suggestions = response.getSuggest().filter(CompletionSuggestion.class).stream()
                                           .map(CompletionSuggestion::getOptions).flatMap(Collection::stream).map(option -> option.getText().string())
                                           .distinct()
                                           .collect(Collectors.toList());
        options = suggestions.size();
        return suggestions;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        long elapsed = watch.elapsed();
        ActionLogContext.track("elasticsearch", elapsed, options, 0);
        logger.debug("complete, options={}, esTook={}, elapsed={}", options, esTook, elapsed);
        checkSlowOperation(elapsed);
    }
}
 
Example #2
Source File: SuggestBuilders.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a completion suggestion lookup query with the provided <code>name</code>
 *
 * @param name The suggestion name
 * @return a {@link org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder}
 * instance
 */
public static CompletionSuggestionBuilder completionSuggestion(String name) {
    return new CompletionSuggestionBuilder(name);
}