Convert java query output in json format

Hi all,
can someone help get my java query output in json format im just printing it in line and now i want to upgrade my code output tried multiple ways but no luck.
Below is my code elasticsearch java api code.

 try {
            var sub3TermAggregation = new Aggregation.Builder()
                    .sum(new SumAggregation.Builder()
                            .field("labels.row_count").build())
                    .build();

            var sub4Aggregation = new Aggregation.Builder()
                    .terms(new TermsAggregation.Builder()
                            .field("labels.job_id")
                            .size(100000)
                            //   .order(List.of("_key", SortOrder.Desc))
                            .minDocCount(1)
                            .build())
                    .aggregations("3", sub3TermAggregation)
                    .build();

            var aggregation = new Aggregation.Builder()
                    .terms(new TermsAggregation.Builder()
                            .field("labels.client_id")
                            .size(100000)
                            //     .order(List.of("_key", SortOrder.Desc))
                            .minDocCount(1)
                            .build())
                    .aggregations("4", sub4Aggregation)
                    .build();

            var boolquery = BoolQuery
                    .of(b -> b.filter(Query
                            .of(q -> q.range(RangeQuery
                                    .of(r -> r.field("@timestamp")
                                            .gte(JsonData.of("1664755200000"))
                                            .lte(JsonData.of("1664787600000"))
                                            .format("epoch_millis")
                                    ))
                            )).filter(f -> f.queryString(QueryStringQuery.of(qs -> qs
                            .query("service.name:*itm*").analyzeWildcard(true))))
                    );
            var query = Query.of(q -> q.bool(boolquery));

            var searchRequest = new SearchRequest.Builder()
                    .index("*itm*")
                    .size(0)
                    .query(query)
                    .aggregations("2", aggregation)
                    .build();

            var response = client.search(searchRequest, Void.class);


            var aggs2 = response.aggregations().get("2");
            if (Objects.nonNull(aggs2)) {
                var buckets = aggs2.sterms().buckets().array();
                for (StringTermsBucket bucket : buckets) {
                    var subAggs4 = bucket.aggregations().get("4");
                    subAggs4.sterms().buckets().array().forEach(agg3Bucket -> {
                        System.out.println(bucket.key() + bucket.docCount() + "," +
                                agg3Bucket.key() + agg3Bucket.docCount() + "," + agg3Bucket.aggregations().get("3").sum().value());
                    });

                }

            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

And here is the example output of query. it is printing like client_id,job_id,sum_count

24311,5660901,1428.0
255641,5660931,4260.0
340441,17090711,1051574.0
344691,17091121,13613.0
373201,17091971,2769.0
381221,5661261,277579.0
516391,5661071,1761.0
528191,5660691,428.0

And now i want to upgarde my code for future updates for eg. like make a csv of output. and for that output should be json.

Here the json example im trying to archive from my code.

{
  "result": [
    {
      "client_id": "24311",
      "job_id": "5660901",      
      "sum_count": "1428.0"
    },
    {
      "client_id": "24311",
      "job_id": "5660931",      
      "sum_count": "1428.0"
    },
    {
       "client_id": "24311",
      "job_id": "5660901",      
      "sum_count": "1428.0"
    }
  ]
}

Can someone take a look into this it would be very great. if there is anything req. pls let me know.
Thanks.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.