use of org.elasticsearch.client.core.CountResponse 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();
}
use of org.elasticsearch.client.core.CountResponse in project jackrabbit-oak by apache.
the class ElasticIndexStatisticsTest method cachedStatistics.
@Test
public void cachedStatistics() throws Exception {
MutableTicker ticker = new MutableTicker();
LoadingCache<ElasticIndexStatistics.StatsRequestDescriptor, Integer> cache = ElasticIndexStatistics.setupCountCache(100, 10, 1, ticker);
ElasticIndexStatistics indexStatistics = new ElasticIndexStatistics(elasticConnectionMock, indexDefinitionMock, cache);
CountResponse countResponse = mock(CountResponse.class);
when(countResponse.getCount()).thenReturn(100L);
// simulate some delay when invoking elastic
when(elasticClientMock.count(any(CountRequest.class), any(RequestOptions.class))).then(answersWithDelay(250, i -> countResponse));
// cache miss, read data from elastic
assertEquals(100, indexStatistics.numDocs());
verify(elasticClientMock).count(any(CountRequest.class), any(RequestOptions.class));
// index count changes in elastic
when(countResponse.getCount()).thenReturn(1000L);
// cache hit, old value returned
assertEquals(100, indexStatistics.numDocs());
verifyNoMoreInteractions(elasticClientMock);
// move cache time ahead of 2 minutes, cache reload time expired
ticker.tick(Duration.ofMinutes(2));
// old value is returned, read fresh data from elastic in background
assertEquals(100, indexStatistics.numDocs());
assertEventually(() -> {
try {
verify(elasticClientMock, times(2)).count(any(CountRequest.class), any(RequestOptions.class));
} catch (IOException e) {
fail(e.getMessage());
}
// cache hit, latest value returned
assertEquals(1000, indexStatistics.numDocs());
}, 500);
verifyNoMoreInteractions(elasticClientMock);
// index count changes in elastic
when(countResponse.getCount()).thenReturn(5000L);
// move cache time ahead of 15 minutes, cache value expired
ticker.tick(Duration.ofMinutes(15));
// cache miss, read data from elastic
assertEquals(5000, indexStatistics.numDocs());
verify(elasticClientMock, times(3)).count(any(CountRequest.class), any(RequestOptions.class));
}
use of org.elasticsearch.client.core.CountResponse in project herd by FINRAOS.
the class IndexFunctionsDaoTest method testNumberOfTypesInIndexFunction.
@Test
public void testNumberOfTypesInIndexFunction() throws Exception {
// Build mocks
CountResponse countResponse = mock(CountResponse.class);
RestHighLevelClient restHighLevelClient = mock(RestHighLevelClient.class);
// Mock the calls to external methods
when(elasticsearchRestHighLevelClientFactory.getRestHighLevelClient()).thenReturn(restHighLevelClient);
when(restHighLevelClient.count(any(CountRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(countResponse);
when(countResponse.getCount()).thenReturn(SEARCH_INDEX_DOCUMENT_COUNT);
// Call the method under test
long numberOfTypesInIndex = indexFunctionsDao.getNumberOfTypesInIndex(SEARCH_INDEX_NAME);
assertThat("The numberOfTypesInIndex is not the correct value.", numberOfTypesInIndex, is(equalTo(SEARCH_INDEX_DOCUMENT_COUNT)));
// Verify the calls to external methods
verify(elasticsearchRestHighLevelClientFactory).getRestHighLevelClient();
verify(restHighLevelClient).count(any(CountRequest.class), eq(RequestOptions.DEFAULT));
verify(countResponse).getCount();
verify(restHighLevelClient).close();
verifyNoMoreInteractions(elasticsearchRestHighLevelClientFactory, countResponse, restHighLevelClient);
}
use of org.elasticsearch.client.core.CountResponse 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();
}
use of org.elasticsearch.client.core.CountResponse 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