1 year ago

#385978

test-img

khasim

Simple aggregation is getting failed in javaelasticsearch 8.0+ client

I have got a simple method that performs simple terms aggregation using elastic search8.0 I am able to do it using RestHighLevelClient but with ElasticsearchClient I am getting empty buckets.

can someone please help me to resolve

public void aggregate(ElasticsearchClient  client) throws ElasticsearchException, IOException {
    String field = "loglevel";
    Map<String, Long> buckets = new HashMap<String, Long>();
    SearchResponse<SspDevLog> response = client.search(fn -> fn
            .aggregations("loglevel", a -> a.terms(v-> v.field(field))), SspDevLog.class);
    Map<String, Aggregate> aggrs = response.aggregations();
    for(Map.Entry<String, Aggregate> entry : aggrs.entrySet()) {
        Aggregate aggregate = entry.getValue();
        StringTermsAggregate sterms = aggregate.sterms();
        Buckets<StringTermsBucket> sbuckets = sterms.buckets();
        List<StringTermsBucket> bucArr = sbuckets.array();
        for(StringTermsBucket bucObj : bucArr) {
            buckets.put(bucObj.key(), bucObj.docCount());
        }
    }
    System.out.println(buckets);
}

elasticsearch

elasticsearch-java-api

0 Answers

Your Answer

Accepted video resources