Elasticsearch首先根据值排序

时间:2015-06-23 12:20:57

标签: php mysql sorting elasticsearch

我想在release date上对结果进行排序。我希望首先看2015部电影,其余时间按has_poster值排序。所以我得到的结果如下:

2015, 2015, 1989, 2017, 2006

到目前为止,这就是我所拥有的。

$params['index'] = 'movies';
$params['type']  = 'movie';
$params['body']['size'] = 50;
$params['body']['sort'] = array(
    array('has_poster'  => "desc"),
    array('_score'  => "desc"),
);        
$params['body']['query']['filtered'] = array(
    'query' => array(
        'query_string' => array(
            'query' => "survivor",
            'fields' => array('name', 'orig_title'),
            'default_operator' => "AND"
        ),
    )
);

我需要MySQL中的... ORDER BY FIELD(releasedate, 2015), has_poster DESC ...等效于Elasticsearch。

2 个答案:

答案 0 :(得分:2)

您需要脚本排序:

{
  "sort": [
    {
      "_script": {
        "script": "if(doc['releasedate'].date.year==2015) return 1; else return 0;",
        "type": "number",
        "order": "desc"
      }
    },
    {
      "has_poster": "desc"
    }
  ]
}

答案 1 :(得分:0)

我不工作=>类型名称是字符串

$params['body']['sort']= array('name' => 'desc' );

$params=[
                'index'=>'pets',
                'type'=>'bird',
                'body'=>[
                    'query'=>[
                        'bool'=>[
                            'must'=>[],
                            'filter'=>[
                                'range'=>[
                                    'registered'=>[
                                    'gte' => "2020-10-01", 
                                    'lte' => "2020-11-30"]
                                ]
                            ]
                        ]
                    ],
                    'sort'=>[
                        'name'=>[
                            'order'=>'asc'
                        ]
                    ]
                    
                ]
            ];

但不行