Java Code Examples for org.elasticsearch.search.builder.SearchSourceBuilder#suggest()

The following examples show how to use org.elasticsearch.search.builder.SearchSourceBuilder#suggest() . 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;
    }