# Top hits aggregation group by

**URL:** <https://discuss.elastic.co/t/top-hits-aggregation-group-by/46161>\
**Category:** Elasticsearch\
**Created:** [April 3, 2016, 12:17pm UTC](https://discuss.elastic.co/t/top-hits-aggregation-group-by/46161 "2016-04-03T12:17:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kannan\_rbk](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kannan\_rbk](https://discuss.elastic.co/u/kannan_rbk)\
**Post date:** [April 3, 2016, 12:17pm UTC](https://discuss.elastic.co/t/top-hits-aggregation-group-by/46161/1 "2016-04-03T12:17:43Z")

</div>

Hi,

I am using top\_hits\_aggregation to get my users last visited document. I want to group by the results of top\_hits\_aggregation.

**Sample Rows Returned**

```
`{
    "lastvisited_users": {
       "buckets": [
          { 
              "top_user_hits": {
                    "hits": {
                         "hits": [
                            { _source: {"c": "s"}},
                            { _source: {"c": "s,b"}}, 
                            { _source: {"c": "s"}}, 
                          ] 
                   }
              }
          }
       ]
    }
}`

```

I want to group by the results by field "c".

**Expected**

```
{
  "s": 2,
  "s,b": 1
}

```

I am using elasticsearch version 1.7 in production. How to do this?

---

<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, 11:02pm UTC](https://discuss.elastic.co/t/top-hits-aggregation-group-by/46161/2 "2017-07-05T23:02:53Z")

</div>


