Sort by name elasticsearch

前端 未结 4 2115
梦谈多话
梦谈多话 2020-12-21 05:18

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

    $params = [
        \'index\' => $this-&         


        
相关标签:
4条回答
  • 2020-12-21 06:04

    If you have unanalyzed keyword you must use it without enabling fielddata:

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

    But if you haven't the unanalyzed keyword you should reset your index mapping to enable fielddata to name field:

        curl -X PUT "localhost:9200/my_index/_mapping" -H 'Content-Type: application/json' -d'
        {
          "properties": {
            "name": { 
              "type":     "text",
              "fielddata": true
            }
          }
        }
    
    '
    

    see : https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html

    0 讨论(0)
  • 2020-12-21 06:14

    Try this

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

    https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html

    0 讨论(0)
  • 2020-12-21 06:19

    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', ...);
    
    0 讨论(0)
  • 2020-12-21 06:25

    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"

    0 讨论(0)
提交回复
热议问题