I would like to sort my returned search results by date (descending), attribute2 (ascending), attribute3 (descending), attribute4 (ascending). Based on the PHP client documentation, I would expect that I should be able to do this by specifying
$search->sort(['date' => 'DESC', 'attribute2' => 'ASC', 'attribute3' => 'DESC', 'attribute4' => 'ASC']);
after setting the index using
$search->setIndex('indx');
and before running get()
, like so:
$results = $search->search($search_string)->get();
The first problem that I notice is that the PHP client doesn’t accept capitalized sort order parameters, as would be customary in MySQL. (I would ask, as a feature request, that the sort order parameters be made case insensitive.)
Okay, fine, so
$search->sort(['date' => 'desc', 'attribute2' => 'asc', 'attribute3' => 'desc', 'attribute4' => 'asc']);
should work, right? No:
PHP Fatal error: Uncaught Manticoresearch\\Exceptions\\ResponseException: "Error parsing json query: \\"sort\\" property \\"\\" should be an object"
All right, maybe we can still at least sort by date
? Let’s try:
$search->sort(['date' => 'desc']);
Now our returned listing goes from
1999-06-04
2023-01-19
2005-02-21
2005-12-14
1998-10-29
2008-10-16
1985-04-19
2020-05-25
2015-10-07
to
2005-02-21
2005-12-14
1998-10-29
1999-06-04
2008-10-16
2015-10-07
1985-04-19
2020-05-25
2023-01-19
Something has changed, but the results are not properly ordered by date.
So, I’m not really sure if sort()
just plain doesn’t work, or if I’m using it wrong.
In any case, I would like to propose, as a feature request, that
$search->sort(['date' => 'DESC', 'attribute2' => 'ASC', 'attribute3' => 'DESC', 'attribute4' => 'ASC']);
be an acceptable input to the sort()
function, both with multiple attributes sorted using different directions and with sort directions of any capitalization being acceptable.