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

The following examples show how to use org.elasticsearch.search.suggest.completion.CompletionSuggestion. 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: ElasticSearchServiceMapper.java    From vertx-elasticsearch-service with Apache License 2.0 6 votes vote down vote up
private static Suggestion mapToSuggestion(Suggest.Suggestion<?> esSuggestion) {
    final Suggestion suggestion = new Suggestion()
            .setName(esSuggestion.getName())
            .setSize(esSuggestion.getEntries().size())
            .setEntries(
                    esSuggestion.getEntries().stream()
                            .map(e -> mapToSuggestionEntry(e))
                            .collect(Collectors.toList())
            );

    if (esSuggestion instanceof CompletionSuggestion) {
        suggestion.setSuggestionType(SuggestionType.COMPLETION);
    } else if (esSuggestion instanceof PhraseSuggestion) {
        suggestion.setSuggestionType(SuggestionType.PHRASE);
    } else if (esSuggestion instanceof TermSuggestion) {
        suggestion.setSuggestionType(SuggestionType.TERM);
    } else {
        throw new RuntimeException("SuggestionType " + esSuggestion.getClass().getCanonicalName() + " unknown");
    }

    return suggestion;
}
 
Example #2
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 #3
Source File: EsResponseParser.java    From occurrence with Apache License 2.0 5 votes vote down vote up
public static List<String> buildSuggestResponse(org.elasticsearch.action.search.SearchResponse esResponse,
                                                OccurrenceSearchParameter parameter) {

  String fieldName = SEARCH_TO_ES_MAPPING.get(parameter).getFieldName();

  return esResponse.getSuggest().getSuggestion(fieldName).getEntries().stream()
          .flatMap(e -> ((CompletionSuggestion.Entry) e).getOptions().stream())
          .map(CompletionSuggestion.Entry.Option::getHit)
          .map(hit -> hit.getSourceAsMap().get(fieldName))
          .filter(Objects::nonNull)
          .map(String::valueOf)
          .collect(Collectors.toList());
}