Search in sources :

Example 6 with CountRequest

use of org.elasticsearch.client.core.CountRequest in project jobrunr by jobrunr.

the class ElasticSearchStorageProvider method countJobs.

long countJobs(QueryBuilder queryBuilder) throws IOException {
    CountRequest countRequest = new CountRequest(jobIndexName);
    countRequest.query(queryBuilder);
    CountResponse countResponse = client.count(countRequest, RequestOptions.DEFAULT);
    return countResponse.getCount();
}
Also used : CountRequest(org.elasticsearch.client.core.CountRequest) CountResponse(org.elasticsearch.client.core.CountResponse)

Example 7 with CountRequest

use of org.elasticsearch.client.core.CountRequest in project jobrunr by jobrunr.

the class ElasticSearchStorageProvider method getJobStats.

@Override
public JobStats getJobStats() {
    try {
        GetResponse getResponse = client.get(new GetRequest(metadataIndexName, STATS_ID), RequestOptions.DEFAULT);
        SearchRequest searchRequest = new SearchRequest(jobIndexName);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(matchAllQuery());
        searchSourceBuilder.aggregation(terms(Jobs.FIELD_STATE).field(Jobs.FIELD_STATE));
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        Terms terms = searchResponse.getAggregations().get(Jobs.FIELD_STATE);
        List<? extends Terms.Bucket> buckets = terms.getBuckets();
        return new JobStats(Instant.now(), 0L, buckets.stream().filter(bucket -> StateName.SCHEDULED.name().equals(bucket.getKeyAsString())).map(MultiBucketsAggregation.Bucket::getDocCount).findFirst().orElse(0L), buckets.stream().filter(bucket -> StateName.ENQUEUED.name().equals(bucket.getKeyAsString())).map(MultiBucketsAggregation.Bucket::getDocCount).findFirst().orElse(0L), buckets.stream().filter(bucket -> StateName.PROCESSING.name().equals(bucket.getKeyAsString())).map(MultiBucketsAggregation.Bucket::getDocCount).findFirst().orElse(0L), buckets.stream().filter(bucket -> StateName.FAILED.name().equals(bucket.getKeyAsString())).map(MultiBucketsAggregation.Bucket::getDocCount).findFirst().orElse(0L), buckets.stream().filter(bucket -> StateName.SUCCEEDED.name().equals(bucket.getKeyAsString())).map(MultiBucketsAggregation.Bucket::getDocCount).findFirst().orElse(0L), ((Number) getResponse.getSource().getOrDefault(FIELD_VALUE, 0L)).longValue(), buckets.stream().filter(bucket -> StateName.DELETED.name().equals(bucket.getKeyAsString())).map(MultiBucketsAggregation.Bucket::getDocCount).findFirst().orElse(0L), (int) client.count(new CountRequest(recurringJobIndexName), RequestOptions.DEFAULT).getCount(), (int) client.count(new CountRequest(backgroundJobServerIndexName), RequestOptions.DEFAULT).getCount());
    } catch (IOException e) {
        throw new StorageException(e);
    }
}
Also used : CountRequest(org.elasticsearch.client.core.CountRequest) SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) GetRequest(org.elasticsearch.action.get.GetRequest) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) IOException(java.io.IOException) GetResponse(org.elasticsearch.action.get.GetResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 8 with CountRequest

use of org.elasticsearch.client.core.CountRequest in project spring-data-elasticsearch by spring-projects.

the class RequestConverters method count.

/**
 * Creates a count request.
 *
 * @param countRequest the search defining the data to be counted
 * @return Elasticsearch count request
 * @since 4.0
 */
public static Request count(CountRequest countRequest) {
    Request request = new Request(HttpMethod.POST.name(), endpoint(countRequest.indices(), countRequest.types(), "_count"));
    Params params = new Params(request);
    addCountRequestParams(params, countRequest);
    if (countRequest.source() != null) {
        request.setEntity(createEntity(countRequest.source(), REQUEST_BODY_CONTENT_TYPE));
    }
    return request;
}
Also used : ExplainRequest(org.elasticsearch.action.explain.ExplainRequest) GetMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest) SearchTemplateRequest(org.elasticsearch.script.mustache.SearchTemplateRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) FieldCapabilitiesRequest(org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest) CountRequest(org.elasticsearch.client.core.CountRequest) IndexTemplatesExistRequest(org.elasticsearch.client.indices.IndexTemplatesExistRequest) GetStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) GetRequest(org.elasticsearch.action.get.GetRequest) RankEvalRequest(org.elasticsearch.index.rankeval.RankEvalRequest) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest) IndicesAliasesRequest(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest) AbstractBulkByScrollRequest(org.elasticsearch.index.reindex.AbstractBulkByScrollRequest) DeleteIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) GetSettingsRequest(org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest) PutStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) PutIndexTemplateRequest(org.elasticsearch.client.indices.PutIndexTemplateRequest) DeleteStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) SearchRequest(org.elasticsearch.action.search.SearchRequest) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) RefreshRequest(org.elasticsearch.action.admin.indices.refresh.RefreshRequest) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) GetIndexRequest(org.elasticsearch.action.admin.indices.get.GetIndexRequest) GetIndexTemplatesRequest(org.elasticsearch.client.indices.GetIndexTemplatesRequest) GetFieldMappingsRequest(org.elasticsearch.client.indices.GetFieldMappingsRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) Request(org.elasticsearch.client.Request) AnalyzeRequest(org.elasticsearch.client.indices.AnalyzeRequest) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) RethrottleRequest(org.elasticsearch.client.RethrottleRequest) FlushRequest(org.elasticsearch.action.admin.indices.flush.FlushRequest) PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ReindexRequest(org.elasticsearch.index.reindex.ReindexRequest)

Example 9 with CountRequest

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

the class MessagesES7IT method messageCount.

@Override
protected long messageCount(String indexName) {
    this.elasticsearch.elasticsearchClient().execute((c, requestOptions) -> c.indices().refresh(new RefreshRequest(), requestOptions));
    final CountRequest countRequest = new CountRequest(indexName);
    final CountResponse result = this.elasticsearch.elasticsearchClient().execute((c, requestOptions) -> c.count(countRequest, requestOptions));
    return result.getCount();
}
Also used : RefreshRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.refresh.RefreshRequest) CountRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.core.CountRequest) CountResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.core.CountResponse)

Example 10 with CountRequest

use of org.elasticsearch.client.core.CountRequest in project apm-agent-java by elastic.

the class AbstractEs6_4ClientInstrumentationTest method testCountRequest_validateSpanContentAndDbContext.

@Test
public void testCountRequest_validateSpanContentAndDbContext() throws Exception {
    createDocument();
    reporter.reset();
    CountRequest countRequest = new CountRequest(INDEX);
    SearchSourceBuilder countSourceBuilder = new SearchSourceBuilder();
    countSourceBuilder.query(QueryBuilders.termQuery(FOO, BAR));
    countRequest.source(countSourceBuilder);
    CountResponse responses = doCount(countRequest);
    assertThat(responses.getCount()).isEqualTo(1);
    List<Span> spans = reporter.getSpans();
    assertThat(spans).hasSize(1);
    Span span = spans.get(0);
    validateSpanContent(span, String.format("Elasticsearch: POST /%s/_count", INDEX), 200, "POST");
    validateDbContextContent(span, "{\"query\":{\"term\":{\"foo\":{\"value\":\"bar\",\"boost\":1.0}}}}");
    deleteDocument();
}
Also used : CountRequest(org.elasticsearch.client.core.CountRequest) CountResponse(org.elasticsearch.client.core.CountResponse) Span(co.elastic.apm.agent.impl.transaction.Span) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) AbstractEsClientInstrumentationTest(co.elastic.apm.agent.esrestclient.AbstractEsClientInstrumentationTest) Test(org.junit.Test)

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