# Term Aggregation ElasticSearch

**URL:** <https://discuss.elastic.co/t/term-aggregation-elasticsearch/63993>\
**Category:** Elasticsearch\
**Created:** [October 26, 2016, 11:26am UTC](https://discuss.elastic.co/t/term-aggregation-elasticsearch/63993 "2016-10-26T11:26:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aniketpatil](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@aniketpatil](https://discuss.elastic.co/u/aniketpatil)\
**Post date:** [October 26, 2016, 11:26am UTC](https://discuss.elastic.co/t/term-aggregation-elasticsearch/63993/1 "2016-10-26T11:26:27Z")

</div>

I want to group STATUS field which I am fetching from elasticsearch through Java API. Below is my code

```
SearchRequest request =
    Requests.searchRequest(ConstantsValue.indexName)
    .types(ConstantsValue._Type)
    .source("{\"_source\" : [\"STATUS\"],\"aggs\": {\"group_by_STATUS\": {\"terms\": {\"field\": \"STATUS\"}}}}");

    SearchResponse response = client.search(request).actionGet();
    for (SearchHit hit : response.getHits()) 
    {         
       System.out.println("Value :"+hit.sourceAsString());
    }

```

But it shows all STATUS value in result. The output which I am getting is

```
Value :{"STATUS":"SUCCESS"}
Value :{"STATUS":"SUCCESS"}
Value :{"STATUS":"ERROR"}
Value :{"STATUS":"ERROR"}
Value :{"STATUS":"ERROR"}
Value :{"STATUS":"WAITING"}
Value :{"STATUS":"WAITING"}
Value :{"STATUS":"ERROR"}
Value :{"STATUS":"WAITING"}
Value :{"STATUS":"SUCCESS"}

```

I want each status just once so that I can apply further queries. I have tried my best but can't get it. If I could fetch unique value, that would be help me to find some more attributes. Please help me in this

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [October 26, 2016, 12:12pm UTC](https://discuss.elastic.co/t/term-aggregation-elasticsearch/63993/2 "2016-10-26T12:12:35Z")

</div>

Hi, you must read Aggregations instead Hits :

StringTerms terms = response.getAggregations().get("group\_by\_STATUS");  
for(Terms.Bucket entry : terms.getBuckets() ) {  
String status = entry.getKeyAsString().toString();  
}

---

<div class="post-metadata">

**Author:** ![aniketpatil](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@aniketpatil](https://discuss.elastic.co/u/aniketpatil)\
**Post date:** [October 26, 2016, 12:26pm UTC](https://discuss.elastic.co/t/term-aggregation-elasticsearch/63993/3 "2016-10-26T12:26:20Z")

</div>

It's working now.. thank you

---

<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:** [July 5, 2017, 10:09pm UTC](https://discuss.elastic.co/t/term-aggregation-elasticsearch/63993/4 "2017-07-05T22:09:28Z")

</div>


