use of org.molgenis.data.index.exception.IndexCountException in project molgenis by molgenis.
the class ClientFacade method getCount.
private long getCount(QueryBuilder query, List<Index> indexes) {
if (LOG.isTraceEnabled()) {
if (query != null) {
LOG.trace("Counting docs in index(es) '{}' with query '{}' ...", toString(indexes), query);
} else {
LOG.trace("Counting docs in index(es) '{}' ...", toString(indexes));
}
}
CountRequest countRequest = new CountRequest(toIndexNames(indexes));
if (query != null) {
countRequest.query(query);
}
CountResponse countResponse;
Stopwatch stopwatch = Stopwatch.createStarted();
try {
countResponse = client.count(countRequest, DEFAULT);
} catch (ElasticsearchStatusException e) {
if (e.status().getStatus() == 404) {
throw new UnknownIndexException(indexes.stream().map(Index::getName).toList(), e);
} else {
throw new IndexCountException(indexes.stream().map(Index::getName).toList(), e);
}
} catch (ElasticsearchException | IOException e) {
throw new IndexCountException(indexes.stream().map(Index::getName).toList(), e);
}
stopwatch.stop();
if (countResponse.getFailedShards() > 0) {
if (LOG.isErrorEnabled()) {
LOG.error(stream(countResponse.getShardFailures()).map(ShardSearchFailure::toString).collect(joining("\n")));
}
throw new IndexCountException(indexes.stream().map(Index::getName).toList());
}
if (TRUE.equals(countResponse.isTerminatedEarly())) {
throw new IndexCountTimeoutException(indexes.stream().map(Index::getName).toList(), stopwatch.elapsed(MILLISECONDS));
}
long totalHits = countResponse.getCount();
if (LOG.isDebugEnabled()) {
if (query != null) {
LOG.debug("Counted {} docs in index(es) '{}' with query '{}' in {}ms.", totalHits, toString(indexes), Strings.toString(query), stopwatch.elapsed(MILLISECONDS));
} else {
LOG.debug("Counted {} docs in index(es) '{}' in {}ms.", totalHits, toString(indexes), stopwatch.elapsed(MILLISECONDS));
}
}
return totalHits;
}
Aggregations