# How to use elasticsearch java api to apply aggregations on a query result

**URL:** https://discuss.elastic.co/t/how-to-use-elasticsearch-java-api-to-apply-aggregations-on-a-query-result/378968
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [June 6, 2025, 8:00pm UTC](https://discuss.elastic.co/t/how-to-use-elasticsearch-java-api-to-apply-aggregations-on-a-query-result/378968 "2025-06-06T20:00:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![nobodyg](https://avatars.discourse-cdn.com/v4/letter/n/e5b9ba/32.png) [@nobodyg](https://discuss.elastic.co/u/nobodyg)
#### Post date: [June 6, 2025, 8:00pm UTC](https://discuss.elastic.co/t/how-to-use-elasticsearch-java-api-to-apply-aggregations-on-a-query-result/378968/1 "2025-06-06T20:00:31Z")

</div>

I am using co.elastic.clients:elasticsearch-java:9.0.1

I am trying to apply a query and then apply aggregations  
the query is about to get the user name and visiting dates within a date range

the result could be:  
userA 2025-06-01  
userA 2025-06-01  
userA 2025-06-02  
userA 2025-06-02  
userB 2025-06-01  
userB 2025-06-02  
userB 2025-06-02  
userB 2025-06-03

the aggregations is about to get the number of visiting days for each user  
the result in this case is:  
userA: 2  
userB: 3

I tried apply a query then apply termAggregations, also tried apply a filterAggregation then apply termAggregations, both not work

not sure how to achieve this?

select username, count(visitdate)  
select distinct username, visitdate  
from ...  
where visitdate between ... and...  
group by username

code samples are appreciated

thank you in advance

---

<div class="post-metadata">

### Author: ![nobodyg](https://avatars.discourse-cdn.com/v4/letter/n/e5b9ba/32.png) [@nobodyg](https://discuss.elastic.co/u/nobodyg)
#### Post date: [June 6, 2025, 11:50pm UTC](https://discuss.elastic.co/t/how-to-use-elasticsearch-java-api-to-apply-aggregations-on-a-query-result/378968/2 "2025-06-06T23:50:12Z")

</div>

looks like this is caused by the special character (.) in the field name.  
the field name is xxx.xx

---

<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: [July 11, 2025, 10:46am UTC](https://discuss.elastic.co/t/how-to-use-elasticsearch-java-api-to-apply-aggregations-on-a-query-result/378968/3 "2025-07-11T10:46:39Z")

</div>

Hello!

If you're not sure which aggregation to use, it's best to experiment first with Kibana, since it's faster to copy paste examples from the documentation, and then when you have something that works, translate it to Java 🙂

I think [Terms Aggregation](https://www.elastic.co/docs/reference/aggregations/search-aggregations-bucket-terms-aggregation) should work in this case, let's try it:

1. Create a new index to hold the user data

```json
PUT /users-log
{
  "mappings": {
    "properties": {
      "user": {
        "type": "keyword"
      }
    }
  }
}

```

1. Bulk post data to the index

```json
POST users-log/_bulk?refresh=true
{ "index": { "_id": "1" } }
{ "user": "userA", "date": "2025-06-01"}
{ "index": { "_id": "2" } }
{ "user": "userA", "date": "2025-06-02"}
{ "index": { "_id": "3" } }
{ "user": "userB", "date": "2025-06-02"}

```

1. Terms Aggregation

```json
GET users-log/_search
{
  "aggs": {
    "user-log": {
      "terms": { "field": "user" }
    }
  }
}

```

In the relevant part of the response, here is the result!

```json
...
"aggregations": {
    "user-log": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "userA",
          "doc_count": 2
        },
        {
          "key": "userB",
          "doc_count": 1
        }
      ]
    }
  }

```

And lastly, here's how to translate the aggregation to Java:

```java
esClient.search(s -> s
    .aggregations("user-log", a -> a
        .terms(t -> t
            .field("user")
        )
    )
,Object.class);

```

---

<div class="post-metadata">

### Author: ![RainTown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raintown/32/140206_2.png) [@RainTown](https://discuss.elastic.co/u/RainTown)
#### Post date: [July 11, 2025, 11:10am UTC](https://discuss.elastic.co/t/how-to-use-elasticsearch-java-api-to-apply-aggregations-on-a-query-result/378968/4 "2025-07-11T11:10:26Z")

</div>

@ltrotta - I think @nobodyg is rather looking for this:

```auto
POST users-log/_bulk?refresh=true
{ "index": { "_id": "1" } }
{ "user": "userA", "date": "2025-06-01"}
{ "index": { "_id": "2" } }
{ "user": "userA", "date": "2025-06-01"}
{ "index": { "_id": "3" } }
{ "user": "userA", "date": "2025-06-02"}
{ "index": { "_id": "4" } }
{ "user": "userA", "date": "2025-06-02"}
{ "index": { "_id": "5" } }
{ "user": "userB", "date": "2025-06-01"}
{ "index": { "_id": "6" } }
{ "user": "userB", "date": "2025-06-02"}
{ "index": { "_id": "7" } }
{ "user": "userB", "date": "2025-06-02"}
{ "index": { "_id": "8" } }
{ "user": "userB", "date": "2025-06-03"}

```

and aggr as follows:

```auto
GET users-log/_search
{
  "size": 0,
  "aggs": {
    "aggr1": {
      "terms": {
        "field": "user"
      },
      "aggs": {
        "aggr2": {
          "cardinality": {
            "field": "date"
          }
        }
      }
    }
  }
}

```

which generates these results:

```auto
  "aggregations": {
    "aggr1": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "userA",
          "doc_count": 4,
          "aggr2": {
            "value": 2
          }
        },
        {
          "key": "userB",
          "doc_count": 4,
          "aggr2": {
            "value": 3
          }
        }
      ]
    }
  }

```

I'm not a java coder, so leave turning that into Java code for someone else.

---

<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: [July 11, 2025, 1:21pm UTC](https://discuss.elastic.co/t/how-to-use-elasticsearch-java-api-to-apply-aggregations-on-a-query-result/378968/5 "2025-07-11T13:21:26Z")

</div>

@RainTown thanks for clarifying, I went for a more generic approach just to provide guidance, but your example is more precise 🙂
