use of co.elastic.clients.elasticsearch.core.search.Hit in project syncope by apache.
the class ElasticsearchAnySearchDAO method doSearch.
@Override
protected <T extends Any<?>> List<T> doSearch(final Set<String> adminRealms, final SearchCond cond, final int page, final int itemsPerPage, final List<OrderByClause> orderBy, final AnyTypeKind kind) {
SearchRequest request = new SearchRequest.Builder().index(ElasticsearchUtils.getContextDomainName(AuthContextUtils.getDomain(), kind)).searchType(SearchType.QueryThenFetch).query(getQuery(adminRealms, cond, kind)).from(itemsPerPage * (page <= 0 ? 0 : page - 1)).size(itemsPerPage < 0 ? elasticsearchUtils.getIndexMaxResultWindow() : itemsPerPage).sort(sortBuilders(kind, orderBy)).build();
@SuppressWarnings("rawtypes") List<Hit<Map>> esResult = null;
try {
esResult = client.search(request, Map.class).hits().hits();
} catch (Exception e) {
LOG.error("While searching in Elasticsearch", e);
}
return CollectionUtils.isEmpty(esResult) ? List.of() : buildResult(esResult.stream().map(Hit::id).collect(Collectors.toList()), kind);
}
use of co.elastic.clients.elasticsearch.core.search.Hit in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method forEach.
@Override
public void forEach(ForEach<T> forEach) {
var watch = new StopWatch();
long start = System.nanoTime();
long esClientTook = 0;
long esServerTook = 0;
validate(forEach);
Time keepAlive = Time.of(t -> t.time(forEach.scrollTimeout.toNanos() + "nanos"));
String index = forEach.index == null ? this.index : forEach.index;
int totalHits = 0;
try {
var response = elasticSearch.client.search(builder -> builder.index(index).scroll(keepAlive).query(forEach.query).sort(s -> s.field(f -> f.field("_doc"))).size(forEach.limit), documentClass);
var holder = new ScrollIdHolder();
holder.scrollId = response.scrollId();
while (true) {
esServerTook += response.took() * 1_000_000;
var hits = response.hits().hits();
esClientTook += System.nanoTime() - start;
if (hits.isEmpty())
break;
totalHits += hits.size();
for (var hit : hits) {
forEach.consumer.accept(hit.source());
}
start = System.nanoTime();
response = elasticSearch.client.scroll(builder -> builder.scrollId(holder.scrollId).scroll(keepAlive), documentClass);
holder.scrollId = response.scrollId();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (ElasticsearchException e) {
throw elasticSearch.searchException(e);
} finally {
long elapsed = watch.elapsed();
ActionLogContext.track("elasticsearch", elapsed, totalHits, 0);
logger.debug("forEach, totalHits={}, esServerTook={}, esClientTook={}, elapsed={}", totalHits, esServerTook, esClientTook, elapsed);
}
}
use of co.elastic.clients.elasticsearch.core.search.Hit in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method search.
@Override
public SearchResponse<T> search(SearchRequest request) {
var watch = new StopWatch();
validate(request);
long esTook = 0;
String index = request.index == null ? this.index : request.index;
int hits = 0;
try {
var searchRequest = co.elastic.clients.elasticsearch.core.SearchRequest.of(builder -> {
builder.index(index).query(request.query).aggregations(request.aggregations).sort(request.sorts).searchType(request.type).from(request.skip).size(request.limit);
if (request.trackTotalHitsUpTo != null)
builder.trackTotalHits(t -> t.count(request.trackTotalHitsUpTo));
return builder;
});
var response = elasticSearch.client.search(searchRequest, documentClass);
esTook = response.took() * 1_000_000;
hits = response.hits().hits().size();
long total = response.hits().total().value();
List<T> items = response.hits().hits().stream().map(Hit::source).toList();
return new SearchResponse<>(items, total, response.aggregations());
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (ElasticsearchException e) {
throw elasticSearch.searchException(e);
} finally {
long elapsed = watch.elapsed();
ActionLogContext.track("elasticsearch", elapsed, hits, 0);
logger.debug("search, hits={}, esTook={}, elapsed={}", hits, esTook, elapsed);
checkSlowOperation(elapsed);
}
}
Aggregations