# Count results of top hits aggregation

**URL:** <https://discuss.elastic.co/t/count-results-of-top-hits-aggregation/134082>\
**Category:** Elasticsearch\
**Created:** [May 31, 2018, 3:33pm UTC](https://discuss.elastic.co/t/count-results-of-top-hits-aggregation/134082 "2018-05-31T15:33:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pri](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@pri](https://discuss.elastic.co/u/pri)\
**Post date:** [May 31, 2018, 3:33pm UTC](https://discuss.elastic.co/t/count-results-of-top-hits-aggregation/134082/1 "2018-05-31T15:33:04Z")

</div>

Hi,  
I am looking to do a count of the result of top hits aggregation.  
The result of the top hits aggregation could be:  
[  
{  
"defect\_name": "defect1",  
"status": "open"  
},  
{  
"defect\_name": "defect2",  
"status": "open"  
},  
{  
"defect\_name": "defect3",  
"status": "open"  
}  
]

And I want the query to return 3, the count of result set of the top hits aggregation. Is this possible in Elasticsearch

Thanks

---

<div class="post-metadata">

**Author:** ![JKhondhu](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@JKhondhu](https://discuss.elastic.co/u/JKhondhu)\
**Post date:** [May 31, 2018, 5:27pm UTC](https://discuss.elastic.co/t/count-results-of-top-hits-aggregation/134082/2 "2018-05-31T17:27:15Z")

</div>

What do you need the top hits for?  
Top hits gives you the hits, I suggest we would need to use a bucket here.

If you try this terms agg:

```auto
GET test/_search
{
    "aggs" : {
        "status_count" : {
            "terms" : { "field" : "status.keyword" }
        }
    }
}

```

resp:

```auto
"aggregations": {
    "status_count": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "open",
          "doc_count": 3

```

Does this suffice?

ref: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html)

---

<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 28, 2018, 5:27pm UTC](https://discuss.elastic.co/t/count-results-of-top-hits-aggregation/134082/3 "2018-06-28T17:27:33Z")

</div>

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