org.elasticsearch.search.suggest.SuggestBuilders Java Examples

The following examples show how to use org.elasticsearch.search.suggest.SuggestBuilders. 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: EsSearchRequestBuilder.java    From occurrence with Apache License 2.0 6 votes vote down vote up
static SearchRequest buildSuggestQuery(
    String prefix, OccurrenceSearchParameter parameter, Integer limit, String index) {
  SearchRequest request = new SearchRequest();
  request.indices(index);

  SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  request.source(searchSourceBuilder);

  OccurrenceEsField esField = SEARCH_TO_ES_MAPPING.get(parameter);

  // create suggest query
  searchSourceBuilder.suggest(
      new SuggestBuilder()
          .addSuggestion(
              esField.getFieldName(),
              SuggestBuilders.completionSuggestion(esField.getFieldName() + ".suggest")
                  .prefix(prefix)
                  .size(limit != null ? limit : SearchConstants.DEFAULT_SUGGEST_LIMIT)
                  .skipDuplicates(true)));

  // add source field
  searchSourceBuilder.fetchSource(esField.getFieldName(), null);

  return request;
}
 
Example #2
Source File: ElasticQueryBuilder.java    From vind with Apache License 2.0 5 votes vote down vote up
public static SearchSourceBuilder buildSuggestionQuery(ExecutableSuggestionSearch search, DocumentFactory factory) {

        final String searchContext = search.getSearchContext();

        final SearchSourceBuilder searchSource = new SearchSourceBuilder()
                .size(0);

        final BoolQueryBuilder filterSuggestions = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchAllQuery())
                .filter(buildFilterQuery(search.getFilter(), factory, searchContext));

        searchSource.query(filterSuggestions);

//        if(search.getTimeZone() != null) {
//            query.set(CommonParams.TZ,search.getTimeZone());
//        }

        final List<String> suggestionFieldNames =
                Lists.newArrayList(getSuggestionFieldNames(search, factory, searchContext));

        suggestionFieldNames.stream()
                .map(field -> AggregationBuilders
                        .terms(FieldUtil.getSourceFieldName(field.replaceAll(".suggestion", ""), searchContext))
                        .field(field)
                        .includeExclude(
                                new IncludeExclude(Suggester.getSuggestionRegex(search.getInput()), null))
                )
                .forEach(searchSource::aggregation);

        final SuggestBuilder suggestBuilder = new SuggestBuilder();
        suggestBuilder.setGlobalText(search.getInput());
        suggestionFieldNames
                .forEach(fieldName -> suggestBuilder
                        .addSuggestion(
                                FieldUtil.getSourceFieldName(fieldName.replaceAll(".suggestion", ""), searchContext),
                                SuggestBuilders.termSuggestion(fieldName.concat("_experimental")).prefixLength(0)));

        searchSource.suggest(suggestBuilder);
        return searchSource;
    }
 
Example #3
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);
    }
}