# Datehistogram aggregation query using java api client

**URL:** <https://discuss.elastic.co/t/datehistogram-aggregation-query-using-java-api-client/359000>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [May 7, 2024, 11:39pm UTC](https://discuss.elastic.co/t/datehistogram-aggregation-query-using-java-api-client/359000 "2024-05-07T23:39:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rohini1](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@Rohini1](https://discuss.elastic.co/u/Rohini1)\
**Post date:** [May 7, 2024, 11:39pm UTC](https://discuss.elastic.co/t/datehistogram-aggregation-query-using-java-api-client/359000/1 "2024-05-07T23:39:30Z")

</div>

Need to convert this to java api client code  
"aggregations":{"dateHistogram":{"date\_histogram":{"field":"createTime","interval":"6h","offset":0,"order":{"\_key":"asc"},"keyed":false,"min\_doc\_count":0,"extended\_bounds":{"min":1709577360000,"max":1712169360000}},"aggregations":{"terms":{"terms":{"field":"effectedObject.keyword","size":5,"min\_doc\_count":1,"shard\_min\_doc\_count":0,"show\_term\_doc\_count\_error":false,"order":[{"aggs":"desc"},{"\_key":"asc"}]},"aggregations":{"aggs":{"min":{"field":"holdFolder"}}}}}}}'

Able to create datehistogram aggregation but facing issue in adding metrics sub aggregation(min aggregation in this case) to terms aggregation

---

<div class="post-metadata">

**Author:** ![Rohini1](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@Rohini1](https://discuss.elastic.co/u/Rohini1)\
**Post date:** [May 8, 2024, 7:42pm UTC](https://discuss.elastic.co/t/datehistogram-aggregation-query-using-java-api-client/359000/2 "2024-05-08T19:42:16Z")

</div>

can someone assist here?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 8, 2024, 10:54pm UTC](https://discuss.elastic.co/t/datehistogram-aggregation-query-using-java-api-client/359000/3 "2024-05-08T22:54:36Z")

</div>

Please be patient in waiting for responses to your question and refrain from pinging multiple times asking for a response or opening multiple topics for the same question. This is a community forum, it may take time for someone to reply to your question. For more information please refer to the [Community Code of Conduct](https://www.elastic.co/community/codeofconduct) specifically the section "Be patient". Also, please refrain from pinging folks directly, this is a forum and anyone that participates might be able to assist you.

If you are in need of a service with an SLA that covers response times for questions then you may want to consider talking to us about a [subscription](https://www.elastic.co/subscriptions).

It's fine to answer on your own thread after 2 or 3 days (not including weekends) if you don't have an answer.

---

<div class="post-metadata">

**Author:** ![Rohini1](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@Rohini1](https://discuss.elastic.co/u/Rohini1)\
**Post date:** [May 8, 2024, 11:10pm UTC](https://discuss.elastic.co/t/datehistogram-aggregation-query-using-java-api-client/359000/4 "2024-05-08T23:10:22Z")

</div>

Sorry about that. I will wait.

---

<div class="post-metadata">

**Author:** ![ltrotta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ltrotta/32/124003_2.png) [@ltrotta](https://discuss.elastic.co/u/ltrotta)\
**Post date:** [May 10, 2024, 8:42am UTC](https://discuss.elastic.co/t/datehistogram-aggregation-query-using-java-api-client/359000/5 "2024-05-10T08:42:47Z")

</div>

Hello,  
Subaggregations in the java client can be added by calling the `aggregations` method provided in the Aggregation class itself; your example would look something like this:

```auto
        esClient.search(s -> s
                .index("index")
                .aggregations("dateHistogram", a1 -> a1
                    .dateHistogram(dh -> dh
                        .field("createTime")
                        ...)
                    .aggregations("terms", a2 -> a2
                        .terms(ts -> ts
                            .field("effectedObject.keyword")
                            ...)
                        .aggregations("aggs", a3 -> a3
                            .min(m -> m
                                .field("holdFolder")))))
            ,Object.class);

```

---

<div class="post-metadata">

**Author:** ![Rohini1](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@Rohini1](https://discuss.elastic.co/u/Rohini1)\
**Post date:** [May 14, 2024, 5:20am UTC](https://discuss.elastic.co/t/datehistogram-aggregation-query-using-java-api-client/359000/6 "2024-05-14T05:20:11Z")

</div>

Thanks..it worked

---

<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:** [June 11, 2024, 5:20am UTC](https://discuss.elastic.co/t/datehistogram-aggregation-query-using-java-api-client/359000/7 "2024-06-11T05:20:37Z")

</div>

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