use of core.framework.search.CompleteRequest in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method complete.
@Override
public List<String> complete(CompleteRequest request) {
var watch = new StopWatch();
long esTook = 0;
String index = request.index == null ? this.index : request.index;
int options = 0;
try {
var suggest = Suggester.of(builder -> {
builder.text(request.prefix);
for (String field : request.fields) {
builder.suggesters(field, s -> s.completion(c -> c.field(field).skipDuplicates(Boolean.TRUE).size(request.limit)));
}
return builder;
});
var response = elasticSearch.client.search(builder -> builder.index(index).suggest(suggest).source(s -> s.fetch(Boolean.FALSE)), documentClass);
esTook = response.took() * 1_000_000;
List<String> suggestions = response.suggest().values().stream().flatMap(Collection::stream).flatMap(suggestion -> suggestion.completion().options().stream()).map(CompletionSuggestOption::text).distinct().collect(Collectors.toList());
options = suggestions.size();
return suggestions;
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (ElasticsearchException e) {
throw elasticSearch.searchException(e);
} finally {
long elapsed = watch.elapsed();
ActionLogContext.track("elasticsearch", elapsed, options, 0);
logger.debug("complete, options={}, esTook={}, elapsed={}", options, esTook, elapsed);
checkSlowOperation(elapsed);
}
}
Aggregations