Search in sources :

Example 1 with IndexCountTimeoutException

use of org.molgenis.data.index.exception.IndexCountTimeoutException 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;
}
Also used : Stopwatch(com.google.common.base.Stopwatch) CountResponse(org.elasticsearch.client.core.CountResponse) IndexCountTimeoutException(org.molgenis.data.index.exception.IndexCountTimeoutException) Index(org.molgenis.data.elasticsearch.generator.model.Index) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) CountRequest(org.elasticsearch.client.core.CountRequest) IndexCountException(org.molgenis.data.index.exception.IndexCountException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure)

Aggregations

Stopwatch (com.google.common.base.Stopwatch)1 IOException (java.io.IOException)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 ElasticsearchStatusException (org.elasticsearch.ElasticsearchStatusException)1 ShardSearchFailure (org.elasticsearch.action.search.ShardSearchFailure)1 CountRequest (org.elasticsearch.client.core.CountRequest)1 CountResponse (org.elasticsearch.client.core.CountResponse)1 Index (org.molgenis.data.elasticsearch.generator.model.Index)1 IndexCountException (org.molgenis.data.index.exception.IndexCountException)1 IndexCountTimeoutException (org.molgenis.data.index.exception.IndexCountTimeoutException)1 UnknownIndexException (org.molgenis.data.index.exception.UnknownIndexException)1