org.elasticsearch.search.suggest.term.TermSuggestion Java Examples
The following examples show how to use
org.elasticsearch.search.suggest.term.TermSuggestion.
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: ElasticSearchServiceMapper.java From vertx-elasticsearch-service with Apache License 2.0 | 6 votes |
private static Suggestion mapToSuggestion(Suggest.Suggestion<?> esSuggestion) { final Suggestion suggestion = new Suggestion() .setName(esSuggestion.getName()) .setSize(esSuggestion.getEntries().size()) .setEntries( esSuggestion.getEntries().stream() .map(e -> mapToSuggestionEntry(e)) .collect(Collectors.toList()) ); if (esSuggestion instanceof CompletionSuggestion) { suggestion.setSuggestionType(SuggestionType.COMPLETION); } else if (esSuggestion instanceof PhraseSuggestion) { suggestion.setSuggestionType(SuggestionType.PHRASE); } else if (esSuggestion instanceof TermSuggestion) { suggestion.setSuggestionType(SuggestionType.TERM); } else { throw new RuntimeException("SuggestionType " + esSuggestion.getClass().getCanonicalName() + " unknown"); } return suggestion; }
Example #2
Source File: TransportClient.java From elasticsearch-jest-example with MIT License | 5 votes |
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 #3
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 #4
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; }