# Query sort vs aggs sort?

**URL:** <https://discuss.elastic.co/t/query-sort-vs-aggs-sort/180261>\
**Category:** Elasticsearch\
**Created:** [May 8, 2019, 9:48pm UTC](https://discuss.elastic.co/t/query-sort-vs-aggs-sort/180261 "2019-05-08T21:48:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![LoveToTrance](https://avatars.discourse-cdn.com/v4/letter/l/dbc845/32.png) [@LoveToTrance](https://discuss.elastic.co/u/LoveToTrance)\
**Post date:** [May 8, 2019, 9:48pm UTC](https://discuss.elastic.co/t/query-sort-vs-aggs-sort/180261/1 "2019-05-08T21:48:07Z")

</div>

Hi, what would be the difference between sorting on the top level query vs in the aggs vs doing both? Here is a sample query

```auto
{
    "size": 0,
    "from": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "terms": {
                        "id": [
                            211314
                        ]
                    }
                }
            ]
        }
    },
    "aggs": {
        "group_by_id": {
            "terms": {
                "size": 1,
                "field": "id"
            },
            "aggs": {
                "earliest_date": {
                    "top_hits": {
                        "size": 1,
                        "_source": {
                            "includes": [
                                "id",
                                "dateTime.utc",
                                "dateTime.local"
                            ]
                        },
                        "sort": [
                            {
                                "dateTime.utc": "asc"
                            }
                        ]
                    }
                }
            }
        }
    },
    "sort": [
        {
            "dateTime.utc": "asc"
        }
      ]
}

```

As you can see, the top level query sorts by dateTime.utc and I also sort the top\_hits by the same field. Which way would be the appropriate way to retrieve the earliest date possible for each grouping?

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 10, 2019, 4:08pm UTC](https://discuss.elastic.co/t/query-sort-vs-aggs-sort/180261/2 "2019-05-10T16:08:56Z")

</div>

The top-level `sort` orders the search hits, so the individual documents that are returned by the query.

The `sort` in the `top_hits` orders just the documents for that top hits bucket, in the aggregation. So if you are wanting to get the most recent document for each top hit grouping, using the top\_hits `sort` is the correct approach.

---

<div class="post-metadata">

**Author:** ![LoveToTrance](https://avatars.discourse-cdn.com/v4/letter/l/dbc845/32.png) [@LoveToTrance](https://discuss.elastic.co/u/LoveToTrance)\
**Post date:** [May 10, 2019, 6:25pm UTC](https://discuss.elastic.co/t/query-sort-vs-aggs-sort/180261/3 "2019-05-10T18:25:01Z")

</div>

Great, thank you!

---

<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 7, 2019, 6:25pm UTC](https://discuss.elastic.co/t/query-sort-vs-aggs-sort/180261/4 "2019-06-07T18:25:09Z")

</div>

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