Search in sources :

Example 1 with CountResponse

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();
}
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 CountResponse

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));
}
Also used : CountRequest(org.elasticsearch.client.core.CountRequest) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LoadingCache(com.google.common.cache.LoadingCache) Mock(org.mockito.Mock) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Ticker(com.google.common.base.Ticker) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) CountResponse(org.elasticsearch.client.core.CountResponse) Mockito.verify(org.mockito.Mockito.verify) MockitoAnnotations(org.mockito.MockitoAnnotations) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Duration(java.time.Duration) RequestOptions(org.elasticsearch.client.RequestOptions) Assert.fail(org.junit.Assert.fail) CountRequest(org.elasticsearch.client.core.CountRequest) AdditionalAnswers.answersWithDelay(org.mockito.AdditionalAnswers.answersWithDelay) ElasticTestUtils.assertEventually(org.apache.jackrabbit.oak.plugins.index.elastic.ElasticTestUtils.assertEventually) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) RequestOptions(org.elasticsearch.client.RequestOptions) CountResponse(org.elasticsearch.client.core.CountResponse) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with CountResponse

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);
}
Also used : CountRequest(org.elasticsearch.client.core.CountRequest) CountResponse(org.elasticsearch.client.core.CountResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) Test(org.junit.Test)

Example 4 with CountResponse

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();
}
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 5 with CountResponse

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;
}
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

CountRequest (org.elasticsearch.client.core.CountRequest)10 CountResponse (org.elasticsearch.client.core.CountResponse)10 IOException (java.io.IOException)3 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)3 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)3 Test (org.junit.Test)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 Ticker (com.google.common.base.Ticker)1 LoadingCache (com.google.common.cache.LoadingCache)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 Datasource (io.openk9.model.Datasource)1 PluginDriverManagerClient (io.openk9.plugin.driver.manager.client.api.PluginDriverManagerClient)1 ReactorNettyUtils (io.openk9.reactor.netty.util.ReactorNettyUtils)1 ReactorActionListener (io.openk9.search.client.api.ReactorActionListener)1