org.elasticsearch.search.suggest.term.TermSuggestionBuilder Java Examples

The following examples show how to use org.elasticsearch.search.suggest.term.TermSuggestionBuilder. 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: TransportClient.java    From elasticsearch-jest-example with MIT License 5 votes vote down vote up
private static void suggest(){
		Client client = createTransportClient();
		
//		CompletionSuggestionBuilder completionSuggestion = new CompletionSuggestionBuilder("suggestions");
//		completionSuggestion.field("text");
//		completionSuggestion.text("园");
//		completionSuggestion.size(10);
//		
//		SuggestRequestBuilder suggestRequestBuilder = client.prepareSuggest("article");
//		suggestRequestBuilder.addSuggestion(completionSuggestion);
//		SuggestResponse suggestResponse = suggestRequestBuilder.execute().actionGet();
//		
//		Suggestion<? extends Entry<? extends Option>> suggestion = suggestResponse.getSuggest().getSuggestion("suggestions");
//		for(Entry<? extends Option> entry:suggestion){
//			for (Option option : entry) {
//				System.out.println(option.getText().string());
//			}
//		}
		
		TermSuggestionBuilder termSuggestionBuilder = new TermSuggestionBuilder("suggest"); 
		termSuggestionBuilder.text("编程");
		termSuggestionBuilder.field("desc");
		TermSuggestion termSuggestion = client.prepareSuggest("book")
				.addSuggestion(termSuggestionBuilder)
				.execute()
				.actionGet()
				.getSuggest()
				.getSuggestion("suggest");
		Suggestion<? extends Entry<? extends Option>> suggestion = termSuggestion;
		for(Entry<? extends Option> entry:suggestion){
			for (Option option : entry) {
				System.out.println(option.getText().string());
			}
		}
		
	}
 
Example #2
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 #3
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 #4
Source File: SuggestBuilders.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a term suggestion lookup query with the provided <code>name</code>
 *
 * @param name The suggestion name
 * @return a {@link org.elasticsearch.search.suggest.term.TermSuggestionBuilder}
 * instance
 */
public static TermSuggestionBuilder termSuggestion(String name) {
    return new TermSuggestionBuilder(name);
}