# Top n over Max() aggregation with group by and then return all fields

**URL:** <https://discuss.elastic.co/t/top-n-over-max-aggregation-with-group-by-and-then-return-all-fields/342954>\
**Category:** Elasticsearch\
**Created:** [September 13, 2023, 12:58pm UTC](https://discuss.elastic.co/t/top-n-over-max-aggregation-with-group-by-and-then-return-all-fields/342954 "2023-09-13T12:58:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aakashagrawal](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@aakashagrawal](https://discuss.elastic.co/u/aakashagrawal)\
**Post date:** [September 13, 2023, 12:58pm UTC](https://discuss.elastic.co/t/top-n-over-max-aggregation-with-group-by-and-then-return-all-fields/342954/1 "2023-09-13T12:58:47Z")

</div>

Hi,  
I'm a total newbie to Elasticsearch and hence please ignore if you think my question is very basic.  
I've already looked at this post which solves one part of my problem:

> [@Max() aggregation with group by and then return all fields](https://discuss.elastic.co/t/max-aggregation-with-group-by-and-then-return-all-fields/172719):
>
> Hi, I have a collection of articles with different author, title and revisions. Can I run a search to get all the articles with the biggest revision in their author+title group (records in bold)? Author | PublishedDate | Revision | Title ---------------+------------------------+---------------+--------------- James |2019-02-04T00:00:00.000Z|1 |I wonder why James |2019-03-04T00:00:00.000Z|2 |I wonder why Parker |2019-0…

What I want is only top n (say top 2) results based on max of Revision number from that post's problem statement.

Reposting the data and expected results here:  
Sample Data:  
Title | Author | PublishedDate | Revision | Reviewer | ReviewComment  
---------------+---------------+------------------------+---------------+---------------+-----------------  
The Endgame |Parker |2018-12-04|1 |Martin |not good  
**The Endgame |Parker |2018-12-14|2 |Henry |passed**  
I Star Wars |James |2019-02-04|1 |Mary |needs improvement  
I wonder why |James |2019-03-04|4 |Jack |awesome!  
**I wonder why |James |2019-03-04|5 |Jones |not bad**

I want top 2 (based on Revision) unique Title+Author as highlighted in **bold** above.

---

<div class="post-metadata">

**Author:** ![aakashagrawal](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@aakashagrawal](https://discuss.elastic.co/u/aakashagrawal)\
**Post date:** [September 13, 2023, 4:24pm UTC](https://discuss.elastic.co/t/top-n-over-max-aggregation-with-group-by-and-then-return-all-fields/342954/2 "2023-09-13T16:24:56Z")

</div>

Found the response from a colleague that worked:

```auto
{
    "aggs": {
        "top_n_title": {
            "multi_terms": {
                "terms": [
                    {
                        "field": "Title"
                    },
                    {
                        "field": "Author"
                    }
                ],
                "order": {
                    "max_Revision": "desc"
                },                
                "size": 1000                
            },
            "aggs": {
                "max_Revision": {
                    "max": {
                        "field": "Revision"
                    }
                }
            }
        }
    }
}

```

---

<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:** [October 11, 2023, 4:25pm UTC](https://discuss.elastic.co/t/top-n-over-max-aggregation-with-group-by-and-then-return-all-fields/342954/3 "2023-10-11T16:25:19Z")

</div>

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