# Bucket aggregation with java api

**URL:** <https://discuss.elastic.co/t/bucket-aggregation-with-java-api/315074>\
**Category:** Elasticsearch\
**Created:** [September 24, 2022, 7:28am UTC](https://discuss.elastic.co/t/bucket-aggregation-with-java-api/315074 "2022-09-24T07:28:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shubh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shubh/32/111180_2.png) [@Shubh](https://discuss.elastic.co/u/Shubh)\
**Post date:** [September 24, 2022, 7:28am UTC](https://discuss.elastic.co/t/bucket-aggregation-with-java-api/315074/1 "2022-09-24T07:28:54Z")

</div>

Hi all can anyone pls help me convert below query in java i have been stuck to this from very long and frustrated.. pls help here

```auto
GET /_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "@timestamp": {
              "gte": 1663632000000,
              "lte": 1663804799000,
              "format": "epoch_millis"
            }
          }
        },
        {
          "query_string": {
            "analyze_wildcard": true,
            "query": "service.name:*itm*"
          }
        }
      ]
    }
  },
  "aggs": {
    "terms_by": {
      "terms": {
        "field": "labels.client_id",
        "size": 100000,
        "order": {
          "_key": "desc"
        },
        "min_doc_count": 1
      },
      "aggs": {
        "sum_rowcount": {
          "sum": {
            "field": "labels.row_count"
          }
          , "aggs": {
            "job_id": {
              "terms": {
                "field": "labels.job_id",
                "size": 100000
              }
            }
          }
          
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [September 24, 2022, 11:27am UTC](https://discuss.elastic.co/t/bucket-aggregation-with-java-api/315074/2 "2022-09-24T11:27:01Z")

</div>

Hi @Shubh

Try this:

```auto
Map<String, Aggregation> map = new HashMap<>();

    var subTermAggregation = new Aggregation.Builder()
        .terms(new TermsAggregation.Builder()
            .field("labels.job_id").size(100000).build())
        .build();

    var subAggregation = new Aggregation.Builder()
        .sum(new SumAggregation.Builder().field("labels.row_count").build())
        .aggregations(new HashMap<>() {{
          put("job_id", subTermAggregation);
        }})
        .build();

    var aggregation = new Aggregation.Builder()
        .terms(new TermsAggregation.Builder()
            .field("labels.client_id")
            .size(100000)
            .order(Map.of("_key", SortOrder.Desc))
            .minDocCount(1)
            .build())
        .aggregations(new HashMap<>() {{
          put("sum_rowcount", subAggregation);
        }}).build();

    map.put("terms_by", aggregation);

    var boolquery = BoolQuery.of(b -> b.filter(Query.of(q -> q.range(RangeQuery.of(
        r -> r.field("@timestamp").gte(JsonData.of("1663632000000")).lte(JsonData.of("1663804799000")).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("idx_name")
        .size(0)
        .query(query)
        .aggregations(map)
        .build();

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

```

---

<div class="post-metadata">

**Author:** ![Rain\_Water](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rain_water/32/98764_2.png) [@Rain\_Water](https://discuss.elastic.co/u/Rain_Water)\
**Post date:** [September 24, 2022, 12:42pm UTC](https://discuss.elastic.co/t/bucket-aggregation-with-java-api/315074/3 "2022-09-24T12:42:47Z")

</div>

Hey.. Thanx for reply.. Can i do more aggregation if i have to?

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [September 24, 2022, 4:06pm UTC](https://discuss.elastic.co/t/bucket-aggregation-with-java-api/315074/4 "2022-09-24T16:06:31Z")

</div>

Yes, you can see how it was done for the aggs "sum\_rowcount" and "job\_id".

---

<div class="post-metadata">

**Author:** ![Shubh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shubh/32/111180_2.png) [@Shubh](https://discuss.elastic.co/u/Shubh)\
**Post date:** [September 24, 2022, 5:17pm UTC](https://discuss.elastic.co/t/bucket-aggregation-with-java-api/315074/5 "2022-09-24T17:17:32Z")

</div>

can help me out how would i export this query data in csv file that will help me alot. im stuck on exporting query data with program. if you hlep me that would be greatfull.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [September 24, 2022, 9:28pm UTC](https://discuss.elastic.co/t/bucket-aggregation-with-java-api/315074/6 "2022-09-24T21:28:41Z")

</div>

If I understand you want to export the aggregation results to a csv file.  
Well, first you need to extract the aggregation from the response and use a lib or any other code that will create the csv.  
What you're asking for is way off topic in your post. In that case I would recommend creating a new post for this new question.

---

<div class="post-metadata">

**Author:** ![Shubh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shubh/32/111180_2.png) [@Shubh](https://discuss.elastic.co/u/Shubh)\
**Post date:** [September 25, 2022, 8:12am UTC](https://discuss.elastic.co/t/bucket-aggregation-with-java-api/315074/7 "2022-09-25T08:12:17Z")

</div>

ok thanks that helps me alot

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 23, 2022, 8:13am UTC](https://discuss.elastic.co/t/bucket-aggregation-with-java-api/315074/8 "2022-10-23T08:13:11Z")

</div>

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