本文整理汇总了Java中org.elasticsearch.search.suggest.SuggestBuilder类的典型用法代码示例。如果您正苦于以下问题:Java SuggestBuilder类的具体用法?Java SuggestBuilder怎么用?Java SuggestBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SuggestBuilder类属于org.elasticsearch.search.suggest包,在下文中一共展示了SuggestBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testMalformedJson
import org.elasticsearch.search.suggest.SuggestBuilder; //导入依赖的package包/类
public void testMalformedJson() {
final String field = RandomStrings.randomAsciiOfLength(random(), 10).toLowerCase(Locale.ROOT);
String suggest = "{\n" +
" \"bad-payload\" : {\n" +
" \"text\" : \"the amsterdma meetpu\",\n" +
" \"term\" : {\n" +
" \"field\" : { \"" + field + "\" : \"bad-object\" }\n" +
" }\n" +
" }\n" +
"}";
try (XContentParser parser = createParser(JsonXContent.jsonXContent, suggest)) {
final SuggestBuilder suggestBuilder = SuggestBuilder.fromXContent(parser);
fail("Should not have been able to create SuggestBuilder from malformed JSON: " + suggestBuilder);
} catch (Exception e) {
assertThat(e.getMessage(), containsString("parsing failed"));
}
}
示例2: suggest
import org.elasticsearch.search.suggest.SuggestBuilder; //导入依赖的package包/类
public List<String> suggest(String prefix) {
CompletionSuggestionBuilder suggestionBuilder = new CompletionSuggestionBuilder("name_suggest")
.prefix(prefix);
SearchResponse response = getConnection().getClient().prepareSearch(getIndex())
.setTypes(getType())
.suggest(new SuggestBuilder().addSuggestion("suggestion", suggestionBuilder))
.setSize(100)
.setFetchSource(true)
.setExplain(false)
.execute()
.actionGet();
return response.getSuggest().filter(CompletionSuggestion.class).stream()
.flatMap(s -> s.getOptions().stream())
.sorted(Comparator.comparingDouble(Suggest.Suggestion.Entry.Option::getScore))
.map(Suggest.Suggestion.Entry.Option::getText)
.map(Text::toString)
.collect(Collectors.toList());
}
示例3: ShellScope
import org.elasticsearch.search.suggest.SuggestBuilder; //导入依赖的package包/类
/**
* 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);
}
示例4: addSuggestions
import org.elasticsearch.search.suggest.SuggestBuilder; //导入依赖的package包/类
private SearchRequestBuilder addSuggestions(SearchRequestBuilder request, int i) {
final SuggestBuilder suggestBuilder = new SuggestBuilder();
for (int s = 0; s < randomIntBetween(2, 10); s++) {
if (randomBoolean()) {
suggestBuilder.addSuggestion("s" + s, new PhraseSuggestionBuilder("f").text("test" + i + " test" + (i - 1)));
} else {
suggestBuilder.addSuggestion("s" + s, new TermSuggestionBuilder("f").text("test" + i));
}
}
return request.suggest(suggestBuilder);
}
示例5: addTermSuggest
import org.elasticsearch.search.suggest.SuggestBuilder; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public <T extends SearchCriteria> T addTermSuggest(String field, String term) {
Assert.notNull(field, "parameter field is NULL.");
Assert.notNull(term, "parameter term is NULL.");
TermSuggestionBuilder builder = SuggestBuilder.termSuggestion(field);
builder.field(field).text(term);
suggests.add(builder);
return (T) this;
}
示例6: suggestTerm
import org.elasticsearch.search.suggest.SuggestBuilder; //导入依赖的package包/类
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();
}
示例7: suggestPhrase
import org.elasticsearch.search.suggest.SuggestBuilder; //导入依赖的package包/类
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();
}
示例8: suggest
import org.elasticsearch.search.suggest.SuggestBuilder; //导入依赖的package包/类
/**
* Delegates to {@link org.elasticsearch.search.builder.SearchSourceBuilder#suggest(SuggestBuilder)}
*/
public NoopSearchRequestBuilder suggest(SuggestBuilder suggestBuilder) {
sourceBuilder().suggest(suggestBuilder);
return this;
}
示例9: suggest
import org.elasticsearch.search.suggest.SuggestBuilder; //导入依赖的package包/类
public SearchSourceBuilder suggest(SuggestBuilder suggestBuilder) {
this.suggestBuilder = suggestBuilder;
return this;
}
示例10: suggest
import org.elasticsearch.search.suggest.SuggestBuilder; //导入依赖的package包/类
/**
* Delegates to {@link SearchSourceBuilder#suggest(SuggestBuilder)}
*/
public SearchRequestBuilder suggest(SuggestBuilder suggestBuilder) {
sourceBuilder().suggest(suggestBuilder);
return this;
}
示例11: addSuggestion
import org.elasticsearch.search.suggest.SuggestBuilder; //导入依赖的package包/类
/**
* 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;
}
示例12: suggestBuilder
import org.elasticsearch.search.suggest.SuggestBuilder; //导入依赖的package包/类
private SuggestBuilder suggestBuilder() {
return sourceBuilder().suggest();
}
示例13: findSuggestionsFor
import org.elasticsearch.search.suggest.SuggestBuilder; //导入依赖的package包/类
public List<SuggestOption> findSuggestionsFor(String suggestRequest, String index, String within) {
CompletionSuggestionBuilder completionSuggestionBuilder = SuggestBuilders.completionSuggestion("suggest")
.prefix(suggestRequest).size(AnnotationSearchConstants.MAX_NUMBER_OF_HITS_RETURNED);
String decodedWithinUrl = null;
if (null != within) {
decodedWithinUrl = annotationUtils.decodeWithinUrl(within);
if (null != decodedWithinUrl) {
LOG.info("decodedWithinUrl :" + decodedWithinUrl);
Map<String, List<? extends ToXContent>> contextsMap = new HashMap<>();
List<CategoryQueryContext> contexts = new ArrayList<>(1);
contexts.add(CategoryQueryContext.builder().setCategory(decodedWithinUrl).build());
contextsMap.put(AnnotationSearchConstants.CONTEXT_MANIFEST_NAME, contexts);
completionSuggestionBuilder.contexts(contextsMap);
}
}
completionSuggestionBuilder.size(AnnotationSearchConstants.MAX_NUMBER_OF_HITS_RETURNED);
LOG.info(completionSuggestionBuilder.toString());
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
searchRequestBuilder
.suggest(new SuggestBuilder().addSuggestion("annotation_suggest", completionSuggestionBuilder));
searchRequestBuilder.setSize(0);
searchRequestBuilder.setFetchSource(false);
LOG.info("doSearch query " + searchRequestBuilder.toString());
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
CompletionSuggestion compSuggestion = searchResponse.getSuggest().getSuggestion("annotation_suggest");
List<SuggestOption> options = new ArrayList<>();
Set<String> suggestOptionSet = new TreeSet<>();
if (null != compSuggestion) {
List<CompletionSuggestion.Entry> entryList = compSuggestion.getEntries();
if (entryList != null) {
CompletionSuggestion.Entry entry = entryList.get(0);
List<CompletionSuggestion.Entry.Option> csEntryOptions = entry.getOptions();
if (null != csEntryOptions && !csEntryOptions.isEmpty()) {
Iterator<? extends CompletionSuggestion.Entry.Option> iter = csEntryOptions.iterator();
while (iter.hasNext()) {
CompletionSuggestion.Entry.Option next = iter.next();
suggestOptionSet.add(next.getText().string());
}
}
}
}
if (null != suggestOptionSet) {
for (String setOption : suggestOptionSet) {
SuggestOption option = new SuggestOption(setOption);
options.add(option);
LOG.info("option " + option.getText());
}
}
return options;
}
示例14: getAutoSuggestions
import org.elasticsearch.search.suggest.SuggestBuilder; //导入依赖的package包/类
@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;
}
示例15: getAutoSuggestions
import org.elasticsearch.search.suggest.SuggestBuilder; //导入依赖的package包/类
@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;
}