Closed khrizt closed 1 year ago
@khrizt I translated the cURL command that you reported:
$params = [
'index' => 'name',
'body' => [
'bool' => [
'must' => [
'multi_match' => [
'query' => 'hello',
'type' => 'best_fields'
],
'fields' => [ 'title', 'description' ],
'operator' => 'and'
]
],
'sort' => [
'_script' => [
'type' => 'number',
'script' => [
'source' => "doc['language'].value == 'en' ? 1 : 0"
],
'order' => 'desc'
]
]
]
];
$results = $client->search($params);
Let me know if this works. If you are looking for other PHP examples, I suggest to have a look at https://github.com/elastic/elasticsearch-php-examples
Hi @ezimuel, thanks a lot. It works, I don't know how variants of this I tried but none were the correct one :roll_eyes:.
Thanks!
Summary of problem or feature request
Sorting by _script from php client doesn't work or I haven't found the way to achieve that. But I managed to do this type of sorting from cURL.
I hope someone can guide me if this is a bug or am I doing something wrong.
Code snippet of problem
If I do the query from cURL I can do the following and it works.
But if I do it from PHP the sort parameter is added to the query string and not to the post body. And I haven't managed to make it work. If I do this
I get the error:
No mapping found for [Array] in order to sort on
.And if I try to send it encoded
I end up with the param in the query string and it doesn't work.
System details