# Simple aggregation using java API - elastic 1.x

**URL:** <https://discuss.elastic.co/t/simple-aggregation-using-java-api-elastic-1-x/71070>\
**Category:** Elasticsearch\
**Created:** [January 10, 2017, 11:30am UTC](https://discuss.elastic.co/t/simple-aggregation-using-java-api-elastic-1-x/71070 "2017-01-10T11:30:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmaia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mmaia/32/12963_2.png) [@mmaia](https://discuss.elastic.co/u/mmaia)\
**Post date:** [January 10, 2017, 11:30am UTC](https://discuss.elastic.co/t/simple-aggregation-using-java-api-elastic-1-x/71070/1 "2017-01-10T11:30:50Z")

</div>

I am new to aggregations with Elastic Search and have been stuck for a couple of hours with this very simple example taken from this link: [https://www.elastic.co/guide/en/elasticsearch/guide/1.x/\_aggregation\_test\_drive.html](https://www.elastic.co/guide/en/elasticsearch/guide/1.x/_aggregation_test_drive.html)

Basically I am trying to do the java api version of this very simple working aggregation:

```
http://localhost:9200/cars/transactions/_search?search_type=count
{
 "aggs" : {
   "colors" : {
       "terms" : {
           "field" : "color"
       }
    }
  }
}

```

and this is the java version I am trying to build but it returns empty buckets:

```
SearchResponse sr = client
        .prepareSearch("cars")
        .setTypes("transactions")
        .setSearchType(SearchType.COUNT)
        .addAggregation(AggregationBuilders.terms("colors").field("color"))
        .execute()
        .actionGet();

```

while using the elastic DSL I get a proper resopnse with buckets grouped by colors but with the java version I get an empty bucket.

---

<div class="post-metadata">

**Author:** ![mmaia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mmaia/32/12963_2.png) [@mmaia](https://discuss.elastic.co/u/mmaia)\
**Post date:** [January 10, 2017, 2:51pm UTC](https://discuss.elastic.co/t/simple-aggregation-using-java-api-elastic-1-x/71070/2 "2017-01-10T14:51:24Z")

</div>

Hi,

java code is working, my issue was related to using this code in unit testing and for some reason had bad results, when used against a running elastic cluster the code works fine.  
I've tried to delete the question but couldn't find the option.

---

<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:** [February 7, 2017, 2:51pm UTC](https://discuss.elastic.co/t/simple-aggregation-using-java-api-elastic-1-x/71070/3 "2017-02-07T14:51:35Z")

</div>

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