Search in sources :

Example 1 with CountRequest

use of org.elasticsearch.client.core.CountRequest in project graylog2-server by Graylog2.

the class IndexToolsAdapterES7 method count.

@Override
public long count(Set<String> indices, Optional<Set<String>> includedStreams) {
    final CountRequest request = new CountRequest(indices.toArray(new String[0]), buildStreamIdFilter(includedStreams)).indicesOptions(IndicesOptions.fromOptions(true, false, true, false));
    final CountResponse result = client.execute((c, requestOptions) -> c.count(request, requestOptions), "Unable to count documents of index.");
    return result.getCount();
}
Also used : CountRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.core.CountRequest) CountResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.core.CountResponse)

Example 2 with CountRequest

use of org.elasticsearch.client.core.CountRequest in project jackrabbit-oak by apache.

the class ElasticAbstractQueryTest method countDocuments.

protected long countDocuments(Tree index) {
    ElasticIndexDefinition esIdxDef = getElasticIndexDefinition(index);
    CountRequest request = new CountRequest(esIdxDef.getIndexAlias());
    try {
        return esConnection.getClient().count(request, RequestOptions.DEFAULT).getCount();
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : CountRequest(org.elasticsearch.client.core.CountRequest) IOException(java.io.IOException)

Example 3 with CountRequest

use of org.elasticsearch.client.core.CountRequest in project herd by FINRAOS.

the class IndexFunctionsDaoImpl method getNumberOfTypesInIndex.

@Override
public long getNumberOfTypesInIndex(final String indexName) {
    LOGGER.info("Counting the number of documents in index={}.", indexName);
    // Build the count request.
    CountRequest countRequest = new CountRequest(indexName);
    countRequest.query(QueryBuilders.matchAllQuery());
    // Create the count response object.
    CountResponse countResponse;
    // Get the Elasticsearch REST high level client. The REST high level client is auto closeable, so use try with resources.
    try (final RestHighLevelClient restHighLevelClient = elasticsearchRestHighLevelClientFactory.getRestHighLevelClient()) {
        // Make the count request.
        countResponse = restHighLevelClient.count(countRequest, RequestOptions.DEFAULT);
    } catch (final IOException ioException) {
        LOGGER.error("Caught IOException while attempting to use the ElasticsearchRestHighLevelClient.", ioException);
        throw new ElasticsearchRestClientException("Caught IOException while attempting to use the ElasticsearchRestHighLevelClient.", ioException);
    }
    return countResponse.getCount();
}
Also used : CountRequest(org.elasticsearch.client.core.CountRequest) CountResponse(org.elasticsearch.client.core.CountResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) IOException(java.io.IOException) ElasticsearchRestClientException(org.finra.herd.dao.exception.ElasticsearchRestClientException)

Example 4 with CountRequest

use of org.elasticsearch.client.core.CountRequest 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)

Example 5 with CountRequest

use of org.elasticsearch.client.core.CountRequest in project elasticflow by springwings.

the class EsWriter method getDocumentNums.

private long getDocumentNums(String instance, String storeId) throws Exception {
    String iName = Common.getStoreName(instance, storeId);
    CountRequest countRequest = new CountRequest(iName);
    CountResponse response = getESC().getClient().count(countRequest, RequestOptions.DEFAULT);
    return response.getCount();
}
Also used : CountRequest(org.elasticsearch.client.core.CountRequest) CountResponse(org.elasticsearch.client.core.CountResponse)

Aggregations

CountRequest (org.elasticsearch.client.core.CountRequest)11 CountResponse (org.elasticsearch.client.core.CountResponse)8 IOException (java.io.IOException)4 SearchRequest (org.elasticsearch.action.search.SearchRequest)3 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)3 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)2 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)2 GetRequest (org.elasticsearch.action.get.GetRequest)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2 ClearScrollRequest (org.elasticsearch.action.search.ClearScrollRequest)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 SearchScrollRequest (org.elasticsearch.action.search.SearchScrollRequest)2 AbstractEsClientInstrumentationTest (co.elastic.apm.agent.esrestclient.AbstractEsClientInstrumentationTest)1 Span (co.elastic.apm.agent.impl.transaction.Span)1 Stopwatch (com.google.common.base.Stopwatch)1 DatasourceClient (io.openk9.datasource.client.api.DatasourceClient)1 HttpResponseWriter (io.openk9.http.util.HttpResponseWriter)1 RouterHandler (io.openk9.http.web.RouterHandler)1 DocumentEntityRequest (io.openk9.index.writer.entity.model.DocumentEntityRequest)1 JsonFactory (io.openk9.json.api.JsonFactory)1