org.elasticsearch.action.suggest.SuggestResponse Java Examples

The following examples show how to use org.elasticsearch.action.suggest.SuggestResponse. 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: EsSearch.java    From es-service-parent with Apache License 2.0 5 votes vote down vote up
/**
 * 无奈es的版本太低了,无法在建议器中做过滤操作(笔者es版本1.1),在更高版本的es中可以通过Context Suggester做过滤
 * 
 * @param indices
 * @param suggestQuery
 * @return
 */
@SuppressWarnings({ "unchecked" })
private static List<Map<String, Object>> getCompletionSuggest(String indices,
        SuggestQuery suggestQuery) {
    // 查询体
    CompletionSuggestionFuzzyBuilder suggestionsBuilder = new CompletionSuggestionFuzzyBuilder(
            suggestQuery.suggestName);
    suggestionsBuilder.setFuzziness(Fuzziness.build(suggestQuery.getFuzziness()));
    suggestionsBuilder.text(suggestQuery.getText());
    suggestionsBuilder.field(suggestQuery.getField());
    suggestionsBuilder.size(suggestQuery.getSize());

    SuggestRequestBuilder suggestRequestBuilder = ESClient.getClient()
            .prepareSuggest(indices.split(",")).addSuggestion(suggestionsBuilder);
    SuggestResponse resp = suggestRequestBuilder.execute().actionGet();
    // 查询结果
    List<? extends Entry<? extends Option>> entries = (List<? extends Entry<? extends Option>>) resp
            .getSuggest().getSuggestion(suggestQuery.suggestName).getEntries();
    if (entries == null) {
        return Lists.newArrayList();
    }
    List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
    for (Entry<? extends Option> e : entries) {
        for (Option option : e) {
            Map<String, Object> map_payload = option.getPayloadAsMap();
            map_payload.put("NAME", option.getText().toString());
            result.add(map_payload);
        }
    }

    // 调试模式
    if (Constants.isDebug) {
        logger.info(suggestRequestBuilder.toString());
        logger.info(suggestionsBuilder.toString());
        logger.info(JsonUtil.toJson(result));
    }
    return result;
}
 
Example #2
Source File: SimpleSuggestionBuilder.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public <R> R get(ElasticsearchTemplate elasticsearchTemplate, Function<SuggestResponse, R> mapper){
	Assert.notNull(this.suggestion);
	Assert.notEmpty(indices);
	SuggestResponse response = elasticsearchTemplate.suggest(suggestion, this.indices.toArray(new String[0]));
	
	return mapper.apply(response);
}
 
Example #3
Source File: SuggestRequestBuilder.java    From elasticshell with Apache License 2.0 5 votes vote down vote up
@Override
protected XContentBuilder toXContent(SuggestRequest request, SuggestResponse response, XContentBuilder builder) throws IOException {
    builder.startObject();
    buildBroadcastShardsHeader(builder, response);
    Suggest suggest = response.getSuggest();
    if (suggest != null) {
        suggest.toXContent(builder, ToXContent.EMPTY_PARAMS);
    }
    builder.endObject();
    return builder;
}
 
Example #4
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ActionFuture<SuggestResponse> suggest(final SuggestRequest request) {
    return execute(SuggestAction.INSTANCE, request);
}
 
Example #5
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void suggest(final SuggestRequest request, final ActionListener<SuggestResponse> listener) {
    execute(SuggestAction.INSTANCE, request, listener);
}
 
Example #6
Source File: SimpleSearchQueryBuilder.java    From onetwo with Apache License 2.0 4 votes vote down vote up
public SuggestResponse querySuggest(ElasticsearchTemplate elasticsearchTemplate, SuggestionBuilder<?> suggestion){
	SuggestResponse reponse = elasticsearchTemplate.suggest(suggestion, this.indices.toArray(new String[0]));
	return reponse;
}
 
Example #7
Source File: ProductQueryServiceImpl.java    From elasticsearch-tutorial with MIT License 4 votes vote down vote up
@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 #8
Source File: ProductQueryServiceImpl.java    From searchanalytics-bigdata with MIT License 4 votes vote down vote up
@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 #9
Source File: SuggestRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
@Override
protected ActionFuture<SuggestResponse> doExecute(SuggestRequest request) {
    return client.suggest(request);
}
 
Example #10
Source File: Client.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Suggestion matching a specific phrase.
 *
 * @param request The suggest request
 * @return The result future
 * @see Requests#suggestRequest(String...)
 */
ActionFuture<SuggestResponse> suggest(SuggestRequest request);
 
Example #11
Source File: Client.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Suggestions matching a specific phrase.
 *
 * @param request  The suggest request
 * @param listener A listener to be notified of the result
 * @see Requests#suggestRequest(String...)
 */
void suggest(SuggestRequest request, ActionListener<SuggestResponse> listener);