use of org.elasticsearch.search.suggest.term.TermSuggestion in project elasticsearch by elastic.
the class SuggestTests method testFilter.
public void testFilter() throws Exception {
List<Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>>> suggestions;
CompletionSuggestion completionSuggestion = new CompletionSuggestion(randomAsciiOfLength(10), 2);
PhraseSuggestion phraseSuggestion = new PhraseSuggestion(randomAsciiOfLength(10), 2);
TermSuggestion termSuggestion = new TermSuggestion(randomAsciiOfLength(10), 2, SortBy.SCORE);
suggestions = Arrays.asList(completionSuggestion, phraseSuggestion, termSuggestion);
Suggest suggest = new Suggest(suggestions);
List<PhraseSuggestion> phraseSuggestions = suggest.filter(PhraseSuggestion.class);
assertThat(phraseSuggestions.size(), equalTo(1));
assertThat(phraseSuggestions.get(0), equalTo(phraseSuggestion));
List<TermSuggestion> termSuggestions = suggest.filter(TermSuggestion.class);
assertThat(termSuggestions.size(), equalTo(1));
assertThat(termSuggestions.get(0), equalTo(termSuggestion));
List<CompletionSuggestion> completionSuggestions = suggest.filter(CompletionSuggestion.class);
assertThat(completionSuggestions.size(), equalTo(1));
assertThat(completionSuggestions.get(0), equalTo(completionSuggestion));
}
use of org.elasticsearch.search.suggest.term.TermSuggestion in project graylog2-server by Graylog2.
the class QuerySuggestionsES7 method suggest.
@Override
public SuggestionResponse suggest(SuggestionRequest req) {
final Set<String> affectedIndices = indexLookup.indexNamesForStreamsInTimeRange(req.streams(), req.timerange());
final TermSuggestionBuilder suggestionBuilder = SuggestBuilders.termSuggestion(req.field()).text(req.input()).size(req.size());
final SearchSourceBuilder search = new SearchSourceBuilder().query(QueryBuilders.prefixQuery(req.field(), req.input())).size(0).aggregation(AggregationBuilders.terms("fieldvalues").field(req.field()).size(req.size())).suggest(new SuggestBuilder().addSuggestion("corrections", suggestionBuilder));
try {
final SearchResponse result = client.singleSearch(new SearchRequest(affectedIndices.toArray(new String[] {})).source(search), "Failed to execute aggregation");
final ParsedStringTerms fieldValues = result.getAggregations().get("fieldvalues");
final List<SuggestionEntry> entries = fieldValues.getBuckets().stream().map(b -> new SuggestionEntry(b.getKeyAsString(), b.getDocCount())).collect(Collectors.toList());
if (!entries.isEmpty()) {
return SuggestionResponse.forSuggestions(req.field(), req.input(), entries, fieldValues.getSumOfOtherDocCounts());
} else {
TermSuggestion suggestion = result.getSuggest().getSuggestion("corrections");
final List<SuggestionEntry> corrections = suggestion.getEntries().stream().flatMap(e -> e.getOptions().stream()).map(o -> new SuggestionEntry(o.getText().string(), o.getFreq())).collect(Collectors.toList());
return SuggestionResponse.forSuggestions(req.field(), req.input(), corrections, null);
}
} catch (org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException exception) {
final SuggestionError err = tryResponseException(exception).orElseGet(() -> parseException(exception));
return SuggestionResponse.forError(req.field(), req.input(), err);
}
}
use of org.elasticsearch.search.suggest.term.TermSuggestion in project pancm_project by xuwujing.
the class EsHighLevelRestSearchTest method search.
/**
* search查询使用示例
*
* @throws IOException
*/
private static void search() throws IOException {
/*
* 全文查询使用示例
*/
// 查询指定的索引库
SearchRequest searchRequest = new SearchRequest("user");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 搜索字段user为pancm的数据
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("user", "pancm");
// 设置模糊查询
matchQueryBuilder.fuzziness(Fuzziness.AUTO);
// 设置前缀长度
matchQueryBuilder.prefixLength(3);
// 设置最大扩展选项来控制查询的模糊过程
matchQueryBuilder.maxExpansions(10);
/*
* QueryBuilder也可以
*/
// QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("user", "kimchy")
// .fuzziness(Fuzziness.AUTO)
// .prefixLength(3)
// .maxExpansions(10);
SearchSourceBuilder searchSourceBuilder2 = new SearchSourceBuilder();
searchSourceBuilder2.query(matchQueryBuilder);
SearchRequest searchRequest2 = new SearchRequest();
searchRequest2.source(searchSourceBuilder2);
// 同步查询
SearchResponse searchResponse2 = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse2.getHits();
// 总条数和分值
long totalHits = hits.getTotalHits();
float maxScore = hits.getMaxScore();
hits.forEach(hit -> {
String index = hit.getIndex();
String type = hit.getType();
String id = hit.getId();
float score = hit.getScore();
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
String string = hit.getSourceAsString();
System.out.println("Match查询的Map结果:" + sourceAsMap);
System.out.println("Match查询的String结果:" + string);
String documentTitle = (String) sourceAsMap.get("title");
// List<Object> users = (List<Object>) sourceAsMap.get("user");
Map<String, Object> innerObject = (Map<String, Object>) sourceAsMap.get("innerObject");
});
System.out.println("\n=================\n");
/*
* 高亮查询
*/
SearchSourceBuilder searchSourceBuilder3 = new SearchSourceBuilder();
HighlightBuilder highlightBuilder = new HighlightBuilder();
HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("title");
// 设置字段高亮字体
highlightTitle.highlighterType("user");
highlightBuilder.field(highlightTitle);
HighlightBuilder.Field highlightUser = new HighlightBuilder.Field("user");
highlightBuilder.field(highlightUser);
searchSourceBuilder3.highlighter(highlightBuilder);
SearchRequest searchRequest3 = new SearchRequest();
searchRequest3.source(searchSourceBuilder3);
// 同步查询
SearchResponse searchResponse3 = client.search(searchRequest3, RequestOptions.DEFAULT);
searchResponse3.getHits().forEach(hit -> {
Map<String, Object> map = hit.getSourceAsMap();
String string = hit.getSourceAsString();
System.out.println("Highlight查询的Map结果:" + map);
System.out.println("Highlight查询的String结果:" + string);
});
System.out.println("\n=================\n");
/**
* 聚合查询
*/
SearchSourceBuilder searchSourceBuilder4 = new SearchSourceBuilder();
// terms 就是分组统计 根据user进行分组并创建一个新的聚合user_
TermsAggregationBuilder aggregation = AggregationBuilders.terms("user_").field("user");
aggregation.subAggregation(AggregationBuilders.avg("average_age").field("age"));
searchSourceBuilder4.aggregation(aggregation);
SearchRequest searchRequest4 = new SearchRequest();
searchRequest4.source(searchSourceBuilder4);
// 同步查询
SearchResponse searchResponse4 = client.search(searchRequest4, RequestOptions.DEFAULT);
// 聚合查询返回条件
Aggregations aggregations = searchResponse4.getAggregations();
System.out.println("聚合查询");
for (Aggregation agg : aggregations) {
String type = agg.getType();
String name = agg.getName();
Terms terms = (Terms) aggregations.get(name);
for (Terms.Bucket bucket : terms.getBuckets()) {
System.out.println("条数:" + bucket.getDocCount());
System.out.println("key:" + bucket.getKey());
System.out.println("num:" + bucket.getKeyAsNumber());
Avg avg = bucket.getAggregations().get("average_age");
System.out.println("value:" + avg.getValue());
}
if (type.equals(TermsAggregationBuilder.NAME)) {
Bucket elasticBucket = ((Terms) agg).getBucketByKey("average_age");
long numberOfDocs = elasticBucket.getDocCount();
System.out.println("条数:" + numberOfDocs);
}
}
/*
* 建议查询
*/
SearchSourceBuilder searchSourceBuilder5 = new SearchSourceBuilder();
SuggestionBuilder termSuggestionBuilder = SuggestBuilders.termSuggestion("user").text("pancm");
SuggestBuilder suggestBuilder = new SuggestBuilder();
suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder);
searchSourceBuilder5.suggest(suggestBuilder);
SearchRequest searchRequest5 = new SearchRequest();
searchRequest5.source(searchSourceBuilder5);
// 同步查询
SearchResponse searchResponse5 = client.search(searchRequest5, RequestOptions.DEFAULT);
Suggest suggest = searchResponse5.getSuggest();
TermSuggestion termSuggestion = suggest.getSuggestion("suggest_user");
for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {
for (TermSuggestion.Entry.Option option : entry) {
String suggestText = option.getText().string();
System.out.println("返回结果:" + suggestText);
}
}
}
use of org.elasticsearch.search.suggest.term.TermSuggestion in project elasticsearch by elastic.
the class SuggestionTests method createTestItem.
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Suggestion<? extends Entry<? extends Option>> createTestItem(Class<? extends Suggestion> type) {
String name = randomAsciiOfLengthBetween(5, 10);
// note: size will not be rendered via "toXContent", only passed on internally on transport layer
int size = randomInt();
Supplier<Entry> entrySupplier = null;
Suggestion suggestion = null;
if (type == TermSuggestion.class) {
suggestion = new TermSuggestion(name, size, randomFrom(SortBy.values()));
entrySupplier = () -> SuggestionEntryTests.createTestItem(TermSuggestion.Entry.class);
} else if (type == PhraseSuggestion.class) {
suggestion = new PhraseSuggestion(name, size);
entrySupplier = () -> SuggestionEntryTests.createTestItem(PhraseSuggestion.Entry.class);
} else if (type == CompletionSuggestion.class) {
suggestion = new CompletionSuggestion(name, size);
entrySupplier = () -> SuggestionEntryTests.createTestItem(CompletionSuggestion.Entry.class);
} else {
throw new UnsupportedOperationException("type not supported [" + type + "]");
}
int numEntries;
if (frequently()) {
if (type == CompletionSuggestion.class) {
// CompletionSuggestion can have max. one entry
numEntries = 1;
} else {
numEntries = randomIntBetween(1, 5);
}
} else {
// also occasionally test zero entries
numEntries = 0;
}
for (int i = 0; i < numEntries; i++) {
suggestion.addTerm(entrySupplier.get());
}
return suggestion;
}
use of org.elasticsearch.search.suggest.term.TermSuggestion in project elasticsearch by elastic.
the class Suggest method readFrom.
@Override
public void readFrom(StreamInput in) throws IOException {
final int size = in.readVInt();
suggestions = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
// TODO: remove these complicated generics
Suggestion<? extends Entry<? extends Option>> suggestion;
final int type = in.readVInt();
switch(type) {
case TermSuggestion.TYPE:
suggestion = new TermSuggestion();
break;
case CompletionSuggestion.TYPE:
suggestion = new CompletionSuggestion();
break;
case // CompletionSuggestion.TYPE
2:
throw new IllegalArgumentException("Completion suggester 2.x is not supported anymore");
case PhraseSuggestion.TYPE:
suggestion = new PhraseSuggestion();
break;
default:
suggestion = new Suggestion();
break;
}
suggestion.readFrom(in);
suggestions.add(suggestion);
}
hasScoreDocs = filter(CompletionSuggestion.class).stream().anyMatch(CompletionSuggestion::hasScoreDocs);
}
Aggregations