# Group by latest Plus Aggregation for Analysis or Delete old records

**URL:** <https://discuss.elastic.co/t/group-by-latest-plus-aggregation-for-analysis-or-delete-old-records/288663>\
**Category:** Elasticsearch\
**Created:** [November 8, 2021, 6:03pm UTC](https://discuss.elastic.co/t/group-by-latest-plus-aggregation-for-analysis-or-delete-old-records/288663 "2021-11-08T18:03:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Mousa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmed_mousa/32/96851_2.png) [@Ahmed\_Mousa](https://discuss.elastic.co/u/Ahmed_Mousa)\
**Post date:** [November 8, 2021, 6:03pm UTC](https://discuss.elastic.co/t/group-by-latest-plus-aggregation-for-analysis-or-delete-old-records/288663/1 "2021-11-08T18:03:39Z")

</div>

Hello everyone,

I am trying to get statistics from my elastic on some filters.  
I managed to get some using this:

```auto
GET <index>/_search
{
  "query": {
    "bool": {"must":[ 
      {"terms":{"type.keyword":["video", "text"]}}, 
      {"terms":{"category.keyword": ["Mathematics", "Chemistry", "Biology", "Physics"]}} 
     ]}
  },
  "size": 20,
  "aggs": { 
      "source_language":{"terms":{"field": "source_language.keyword"}},
      "translation_language":{"terms":{"field": "translation_language.keyword"}}
    }
}

```

Where on certain types and categories it will return number of records for with each source\_language and translation \_language.

I want to add another _filter_ for this statistics.  
I have 2 identifiers for each doc (source\_id, translation\_id) and **date** field.  
I want to **group by** on both identifiers and get the **latest doc** using the **date** field.

I tired top\_hits, but it doesn't have sub aggregation.

Also, I am working on Elasticsearch 6.5 (mandatory)

I need a way to make multi\_terms from 7.15 and a way to get that latest documents, then another way to make my aggregation for my required analysis like (translation\_language)

Basically, I wand the same analysis on latest versions only.

any suggestion where to start ?

And if this isn't possible, another approach came to mine is to delete older versions if new version uploaded (new version will be indicated using source\_id, translation\_id, and date)

The data is streamed using logstash from postgres sql using jdbc-plugin

---

<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:** [December 6, 2021, 6:03pm UTC](https://discuss.elastic.co/t/group-by-latest-plus-aggregation-for-analysis-or-delete-old-records/288663/2 "2021-12-06T18:03:58Z")

</div>

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