Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort by name elasticsearch

I try to sort my documents per name using elastic search & the official php client , how i can proceed ?

    $params = [
        'index' => $this->index ,
        'type' => 'videos',
        'from' => $this->uri->segment(2),
        'size' => 12,
        'body' => [
        'query' => [
        'filtered' => [
            'filter' => [
                'term' => [ 'name' => $query ] ,
                'term' => [ 'tags' => $query ]
              ]
           ]
        ]
      ]
    ];




    $data['results'] = $this->client->search($params);
like image 915
NinjaX Avatar asked Mar 12 '23 00:03

NinjaX


2 Answers

I know this question is over a year old, but the answer is not easy to find on the internet, so I'll answer it anyway.
To specify the field to sort on and the order to sort in, use the following syntax:

$params['sort'] = array('updated_at:desc');

To sort on multiple fields:

$params['sort'] = array('updated_at:desc', 'user_id:asc', ...);
like image 128
Frank Avatar answered Mar 19 '23 02:03

Frank


I just saw this post while searching for an answer to the same question, in my case the solution was much simpler and different to the ones I saw here.

$params['body']['sort'] = [ 'id' => 'desc']

this worked fine for me using "elasticsearch/elasticsearch": "^6.1"

like image 27
Eli Avatar answered Mar 19 '23 01:03

Eli