Order by in aggregation ES laravel

- 1 answer

Ad

I'm trying to order elements by field descending in Elasticquent, but don't know how. This is my code:

    $laws_y = Law::searchByQuery([
    ],
        [
            "group_by_law_year" => ["terms" => ['field' => "law_year", "size" => 100]]
        ]);

Can anyone help?

Ad

Answer

Ad

You can do it using the order setting and order by _term: desc:

$laws_y = Law::searchByQuery([
],
[
    "group_by_law_year" => [
         "terms" => [
              'field' => "law_year", 
              "order" => ["_term" => "desc" ],      <--- add this line
              "size" => 100
         ]
    ]
]);
Ad
source: stackoverflow.com
Ad