【问题标题】:Sort by name elasticsearch按名称排序弹性搜索
【发布时间】:2017-02-09 23:44:27
【问题描述】:

我尝试使用弹性搜索和官方 php 客户端按名称对文档进行排序,我该如何继续?

    $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);

【问题讨论】:

    标签: php elasticsearch


    【解决方案1】:

    我知道这个问题已经有一年多了,但是在网上很难找到答案,所以我还是会回答的。
    要指定要排序的字段和排序顺序,请使用以下语法:

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

    对多个字段进行排序:

    $params['sort'] = array('updated_at:desc', 'user_id:asc', ...);
    

    【讨论】:

      【解决方案2】:

      我刚刚在寻找同一个问题的答案时看到了这篇文章,在我的情况下,解决方案与我在这里看到的解决方案相比要简单得多。

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

      使用"elasticsearch/elasticsearch": "^6.1" 对我来说效果很好

      【讨论】:

        【解决方案3】:

        试试这个

        $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

        【讨论】:

          【解决方案4】:

          如果您有未分析的keyword,则必须在不启用字段数据的情况下使用它:

          $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'
                      ]
                  ]
                ]
              ];
          

          但如果您没有未分析的关键字,您应该重置索引映射以启用 fielddata 来命名字段:

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

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

          【讨论】:

            猜你喜欢
            • 2019-01-26
            • 2021-08-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-12-30
            • 2015-11-10
            相关资源
            最近更新 更多