1

I want to sort results on the release date. I want 2015 movies first and the rest of the years sorted on the has_poster value. So I get results like:

2015, 2015, 1989, 2017, 2006

etc.

So far this is what I've got.

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

I need the equivalent of ... ORDER BY FIELD(releasedate, 2015), has_poster DESC ... in MySQL for Elasticsearch.

2 Answers 2

2

You need a scripted sorting:

{
  "sort": [
    {
      "_script": {
        "script": "if(doc['releasedate'].date.year==2015) return 1; else return 0;",
        "type": "number",
        "order": "desc"
      }
    },
    {
      "has_poster": "desc"
    }
  ]
}
Sign up to request clarification or add additional context in comments.

6 Comments

I just tried this one but I get a BadRequest400Exception error in PHP (laravel). If I comment out the _script part then I get results again.
ScriptException[scripts of type [inline], operation [search] and lang [groovy] are disabled]; }] you need to enable dynamic scripting in Elasticsearch.
Oh. I'll try that and will let you know if it worked. Thanks!
But I need to make you aware of elastic.co/blog/… and suggest putting the script on file, instead. But, until then, see the script itself works for you.
|
0

Im not work =>type name is string

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

or

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

but not work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.