org.elasticsearch.search.suggest.SuggestBuilder Java Examples
The following examples show how to use
org.elasticsearch.search.suggest.SuggestBuilder.
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: ShellScope.java From elasticshell with Apache License 2.0 | 6 votes |
/** * Creates a new <code>ShellScope</code> given the actual scope object * @param scope the actual scope object that depends on the engine in use */ ShellScope(Scope scope, ResourceRegistry resourceRegistry) { this.scope = scope; this.resourceRegistry = resourceRegistry; registerJavaClass(Requests.class); registerJavaClass(SearchSourceBuilder.class); registerJavaClass(QueryBuilders.class); registerJavaClass(FilterBuilders.class); registerJavaClass(SortBuilders.class); registerJavaClass(FacetBuilders.class); registerJavaClass(RescoreBuilder.class); registerJavaClass(SuggestBuilder.class); registerJavaClass(AliasAction.class); registerJavaClass(HttpParameters.class); registerJavaClass(AllocateAllocationCommand.class); registerJavaClass(CancelAllocationCommand.class); registerJavaClass(MoveAllocationCommand.class); registerJavaClass(ShardId.class); }
Example #2
Source File: EsSearchRequestBuilder.java From occurrence with Apache License 2.0 | 6 votes |
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 #3
Source File: ElasticSearchTypeImpl.java From core-ng-project with Apache License 2.0 | 5 votes |
@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 #4
Source File: ElasticQueryBuilder.java From vind with Apache License 2.0 | 5 votes |
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 #5
Source File: InternalIndexClient.java From elasticshell with Apache License 2.0 | 5 votes |
public JsonOutput suggestTerm(String suggestText, String... fields) { SuggestRequestBuilder<JsonInput, JsonOutput> suggestRequestBuilder = suggestBuilder().suggestText(suggestText); for (String field : fields) { suggestRequestBuilder.addSuggestion(SuggestBuilder.termSuggestion(field).field(field)); } return suggestRequestBuilder.execute(); }
Example #6
Source File: InternalIndexClient.java From elasticshell with Apache License 2.0 | 5 votes |
public JsonOutput suggestPhrase(String suggestText, String... fields) { SuggestRequestBuilder<JsonInput, JsonOutput> suggestRequestBuilder = suggestBuilder().suggestText(suggestText); for (String field : fields) { suggestRequestBuilder.addSuggestion(SuggestBuilder.phraseSuggestion(field).field(field)); } return suggestRequestBuilder.execute(); }
Example #7
Source File: AbstractClient.java From elasticshell with Apache License 2.0 | 5 votes |
public JsonOutput suggestTerm(String suggestText, String... fields) { SuggestRequestBuilder<JsonInput, JsonOutput> suggestRequestBuilder = suggestBuilder().suggestText(suggestText); for (String field : fields) { suggestRequestBuilder.addSuggestion(SuggestBuilder.termSuggestion(field).field(field)); } return suggestRequestBuilder.execute(); }
Example #8
Source File: AbstractClient.java From elasticshell with Apache License 2.0 | 5 votes |
public JsonOutput suggestPhrase(String suggestText, String... fields) { SuggestRequestBuilder<JsonInput, JsonOutput> suggestRequestBuilder = suggestBuilder().suggestText(suggestText); for (String field : fields) { suggestRequestBuilder.addSuggestion(SuggestBuilder.phraseSuggestion(field).field(field)); } return suggestRequestBuilder.execute(); }
Example #9
Source File: SearchRequestBuilder.java From Elasticsearch with Apache License 2.0 | 4 votes |
private SuggestBuilder suggestBuilder() { return sourceBuilder().suggest(); }
Example #10
Source File: ProductQueryServiceImpl.java From elasticsearch-tutorial with MIT License | 4 votes |
@Override public List<AutoSuggestionEntry> getAutoSuggestions(ElasticSearchIndexConfig config, String queryString) { TermSuggestionBuilder suggesBuilder = SuggestBuilder.termSuggestion(SearchFacetName.AUTO_SUGGESTION.getCode()) .field(SearchDocumentFieldName.KEYWORDS.getFieldName()) .analyzer(config.getAutoSuggestionAnalyzerName()) .size(20) .text(queryString) // .suggestMode("always") // .stringDistance("ngram") ; // CompletionSuggestionBuilder suggesBuilder = new CompletionSuggestionBuilder(SearchFacetName.AUTO_SUGGESTION.getCode()) // .field(SearchDocumentFieldName.KEYWORDS.getFieldName()) // .analyzer(config.getAutoSuggestionAnalyzerName()) // .size(20) // .text(queryString) //// .stringDistance("ngram") // ; // PhraseSuggestionBuilder suggesBuilder = SuggestBuilder.phraseSuggestion(SearchFacetName.AUTO_SUGGESTION.getCode()) // .field(SearchDocumentFieldName.TITLE.getFieldName()) // .analyzer(config.getAutoSuggestionAnalyzerName()) // .size(10) // .text(queryString) // ; SuggestRequestBuilder addSuggestion = searchClientService.getClient().prepareSuggest(config.getIndexAliasName()) .addSuggestion(suggesBuilder); try { logger.debug("Auto Suggestion request is {}", suggesBuilder.toXContent(jsonBuilder().startObject(), null).prettyPrint().string()); } catch (IOException e) { //Do nothing logger.error("Error in to string", e); } SuggestResponse suggestResponse = addSuggestion.get(); logger.debug("Auto Suggestion response is {}", suggestResponse); List<AutoSuggestionEntry> suggestions = new ArrayList<AutoSuggestionEntry>(); if(suggestResponse !=null && suggestResponse.getSuggest() !=null && suggestResponse.getSuggest().getSuggestion(SearchFacetName.AUTO_SUGGESTION.getCode()) !=null) { for (org.elasticsearch.search.suggest.Suggest.Suggestion.Entry<? extends Option> suggestEntry : suggestResponse.getSuggest().getSuggestion(SearchFacetName.AUTO_SUGGESTION.getCode()).getEntries()) { for (Option option : suggestEntry.getOptions()) { int count = ((TermSuggestion.Entry.Option) option).getFreq(); AutoSuggestionEntry autoSuggestionEntry = new AutoSuggestionEntry(option.getText().string(), count); suggestions.add(autoSuggestionEntry); } } } return suggestions; }
Example #11
Source File: ProductQueryServiceImpl.java From searchanalytics-bigdata with MIT License | 4 votes |
@Override public List<AutoSuggestionEntry> getAutoSuggestions( final ElasticSearchIndexConfig config, final String queryString) { final TermSuggestionBuilder suggesBuilder = SuggestBuilder .termSuggestion(SearchFacetName.AUTO_SUGGESTION.getCode()) .field(SearchDocumentFieldName.KEYWORDS.getFieldName()) .analyzer(config.getAutoSuggestionAnalyzerName()).size(20) .text(queryString) // .suggestMode("always") // .stringDistance("ngram") ; // CompletionSuggestionBuilder suggesBuilder = new // CompletionSuggestionBuilder(SearchFacetName.AUTO_SUGGESTION.getCode()) // .field(SearchDocumentFieldName.KEYWORDS.getFieldName()) // .analyzer(config.getAutoSuggestionAnalyzerName()) // .size(20) // .text(queryString) // // .stringDistance("ngram") // ; // PhraseSuggestionBuilder suggesBuilder = // SuggestBuilder.phraseSuggestion(SearchFacetName.AUTO_SUGGESTION.getCode()) // .field(SearchDocumentFieldName.TITLE.getFieldName()) // .analyzer(config.getAutoSuggestionAnalyzerName()) // .size(10) // .text(queryString) // ; final SuggestRequestBuilder addSuggestion = searchClientService .getClient().prepareSuggest(config.getIndexAliasName()) .addSuggestion(suggesBuilder); try { logger.debug("Auto Suggestion request is {}", suggesBuilder .toXContent(jsonBuilder().startObject(), null) .prettyPrint().string()); } catch (final IOException e) { // Do nothing logger.error("Error in to string", e); } final SuggestResponse suggestResponse = addSuggestion.get(); logger.debug("Auto Suggestion response is {}", suggestResponse); final List<AutoSuggestionEntry> suggestions = new ArrayList<AutoSuggestionEntry>(); if (suggestResponse != null && suggestResponse.getSuggest() != null && suggestResponse.getSuggest().getSuggestion( SearchFacetName.AUTO_SUGGESTION.getCode()) != null) { for (final org.elasticsearch.search.suggest.Suggest.Suggestion.Entry<? extends Option> suggestEntry : suggestResponse .getSuggest() .getSuggestion(SearchFacetName.AUTO_SUGGESTION.getCode()) .getEntries()) { for (final Option option : suggestEntry.getOptions()) { final int count = ((TermSuggestion.Entry.Option) option) .getFreq(); final AutoSuggestionEntry autoSuggestionEntry = new AutoSuggestionEntry( option.getText().string(), count); suggestions.add(autoSuggestionEntry); } } } return suggestions; }
Example #12
Source File: SuggestRequestBuilder.java From elasticshell with Apache License 2.0 | 4 votes |
public <T> SuggestRequestBuilder<JsonInput, JsonOutput> addSuggestion(SuggestBuilder.SuggestionBuilder<T> suggestion) { suggest.addSuggestion(suggestion); return this; }
Example #13
Source File: SearchRequestBuilder.java From elasticshell with Apache License 2.0 | 4 votes |
public SearchRequestBuilder<JsonInput, JsonOutput> addSuggestionBuilder(SuggestBuilder.SuggestionBuilder<?> suggestion) { suggestBuilder().addSuggestion(suggestion); return this; }
Example #14
Source File: SearchRequestBuilder.java From elasticshell with Apache License 2.0 | 4 votes |
private SuggestBuilder suggestBuilder() { return sourceBuilder().suggest(); }
Example #15
Source File: SearchRequestBuilder.java From Elasticsearch with Apache License 2.0 | 4 votes |
/** * Delegates to {@link org.elasticsearch.search.suggest.SuggestBuilder#addSuggestion(org.elasticsearch.search.suggest.SuggestBuilder.SuggestionBuilder)}. */ public SearchRequestBuilder addSuggestion(SuggestBuilder.SuggestionBuilder<?> suggestion) { suggestBuilder().addSuggestion(suggestion); return this; }
Example #16
Source File: SuggestRequest.java From Elasticsearch with Apache License 2.0 | 2 votes |
/** * set a new source using a {@link org.elasticsearch.search.suggest.SuggestBuilder} * for phrase and term suggestion lookup */ public SuggestRequest suggest(SuggestBuilder suggestBuilder) { return suggest(suggestBuilder.buildAsBytes(Requests.CONTENT_TYPE)); }
Example #17
Source File: SuggestRequest.java From Elasticsearch with Apache License 2.0 | 2 votes |
/** * set a new source using a {@link org.elasticsearch.search.suggest.SuggestBuilder.SuggestionBuilder} * for completion suggestion lookup */ public SuggestRequest suggest(SuggestBuilder.SuggestionBuilder suggestionBuilder) { return suggest(suggestionBuilder.buildAsBytes(Requests.CONTENT_TYPE)); }