Query sort vs aggs sort?

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

{
    "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?

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.

1 Like

Great, thank you!

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