Top hit aggregation with double sorting

Hi there,

I am struggling to do a top hit aggregation that make sense on my elastic cluster.
Here is a sample rows:

{id: 123, order: 2, bucket_order: 2, category_id: 0},
{id: 456, order: 1, bucket_order: 2, category_id: 0},
{id: 789, order: 3, bucket_order: 3, category_id: 1},
{id: 012, order: 2, bucket_order: 1, category_id: 2},
{id: 013, order: 5, bucket_order: 1, category_id: 2}

I would like to do a aggregation by category_id, ordering the buckets by bucket_order and have the order inside bucket set to the order field so that the top hit is the first one by order.

Example response (totally making this up)

{category_id: 2, bucket_order: 1, top_hit : 012, all_hits: [012,013]},
{category_id: 0, bucket_order: 2, top_hit : 456, all_hits: [456, 123]},
{category_id: 1, bucket_order: 3, top_hit : 789, all_hits: [789]}

Can you point me to the right direction I did a aggregation but I cannot understand how I can do the double sorting ?