# How to group docs by latest doc and run aggregation on the group in elastic search?

**URL:** <https://discuss.elastic.co/t/how-to-group-docs-by-latest-doc-and-run-aggregation-on-the-group-in-elastic-search/302524>\
**Category:** Elasticsearch\
**Created:** [April 15, 2022, 8:53pm UTC](https://discuss.elastic.co/t/how-to-group-docs-by-latest-doc-and-run-aggregation-on-the-group-in-elastic-search/302524 "2022-04-15T20:53:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adil\_Ismail](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adil_ismail/32/104429_2.png) [@Adil\_Ismail](https://discuss.elastic.co/u/Adil_Ismail)\
**Post date:** [April 15, 2022, 8:53pm UTC](https://discuss.elastic.co/t/how-to-group-docs-by-latest-doc-and-run-aggregation-on-the-group-in-elastic-search/302524/1 "2022-04-15T20:53:31Z")

</div>

Hi ,

I need to get all clients under a partnerId & since there would be many clients I need to group the clients by clientId,latest reportDate and run some aggregations .

Index data is like below -

```auto
[
    { partnerId: "PID1234", clientId: "c1234", reportDate: "2022-02-01" },
    { partnerId: "PID1234", clientId: "c1234", reportDate: "2030-02-01" },
    { partnerId: "PID1111", clientId: "c1222", reportDate: "2010-02-01" },
    { partnerId: "PID2222", clientId: "c1444", reportDate: "2013-02-01" },
 ]

```

I need to do something like the below query, the problem is top hits don't accept sub aggregations -

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "partnerId": "PID1234"
          }
        }
      ]
    }
  },
  "aggs": {
    "groupp": {
      "top_hits": {
        "sort": [
          {
            "clientId": {
              "order": "desc"
            }
          }
        ],
        "size": 1,
        "aggs": {
            "total_engagement_count": {
              "sum": { "field": "recommendations.totalEngagement" }
            }
          }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 15, 2022, 10:22pm UTC](https://discuss.elastic.co/t/how-to-group-docs-by-latest-doc-and-run-aggregation-on-the-group-in-elastic-search/302524/2 "2022-04-15T22:22:50Z")

</div>

Hi!!  
Look this:

> <https://github.com/elastic/elasticsearch/issues/16537#issuecomment-181965367>
>
> Hi!
> 
> Why top\_hits doesn't allow to have sub-aggregations?
> 
> I.e. I want to calcul…ate a median of last N hits in each category

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [April 16, 2022, 11:10am UTC](https://discuss.elastic.co/t/how-to-group-docs-by-latest-doc-and-run-aggregation-on-the-group-in-elastic-search/302524/3 "2022-04-16T11:10:25Z")

</div>

It’s a fairly common requirement to do analysis only on the last known state of an entity.  
See [here](https://discuss.elastic.co/t/get-last-document-string-value-in-each-bucket/260013/2) for solutions.

---

<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:** [May 14, 2022, 11:10am UTC](https://discuss.elastic.co/t/how-to-group-docs-by-latest-doc-and-run-aggregation-on-the-group-in-elastic-search/302524/4 "2022-05-14T11:10:46Z")

</div>

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