Skip to content

Commit

Permalink
Multiple sorts in search (#3758)
Browse files Browse the repository at this point in the history
  • Loading branch information
dafeder committed Mar 1, 2022
1 parent bf9e6ab commit cac1e7a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
{
"name": "sort",
"in": "query",
"description": "Which property to sort results on.",
"description": "Which property to sort results on. Use array format to sort on multiple properties, e.g. sort[0]=field1&sort[1]=field2",
"schema": {
"type": "string"
},
Expand All @@ -81,7 +81,7 @@
{
"name": "sort-order",
"in": "query",
"description": "Sort results in ascending or descending order.",
"description": "Sort results in ascending or descending order. If sorting on multiple properties, use array format to map to sort values, e.g. sort-order[0]=asc&sort-order[1]=desc",
"schema": {
"type": "string",
"default": "asc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,19 @@ private function setFieldConditions(QueryInterface $query, array $params, IndexI
private function setSort(QueryInterface $query, array $params, IndexInterface $index): QueryInterface {
$fields = array_keys($index->getFields());

if (isset($params['sort']) && in_array($params['sort'], $fields)) {
$query->sort($params['sort'], $this->getSortOrder($params));
return $query;
$sorts = $params['sort'] ?? [];
if (!is_array($sorts)) {
$sorts = [$sorts];
}
if (empty($sorts)) {
$query->sort('search_api_relevance', Query::SORT_DESC);
}
foreach ($sorts as $index => $sort) {
if (in_array($sort, $fields)) {
$query->sort($sort, $this->getSortOrder($params, $index));
}
}

$query->sort('search_api_relevance', Query::SORT_DESC);
return $query;
}

Expand All @@ -172,20 +179,30 @@ private function setSort(QueryInterface $query, array $params, IndexInterface $i
*
* @param array $params
* Search parameters.
* @param int $index
* The array index to match the sort order to the sort field, in case
* of multiple sorts.
*
* @return mixed
* String describing sort order as ascending or descending.
*/
private function getSortOrder(array $params) {
private function getSortOrder(array $params, int $index = 0) {
$allowed = [
strtolower(QueryInterface::SORT_ASC),
strtolower(QueryInterface::SORT_DESC),
];
$default = QueryInterface::SORT_ASC;
if (!isset($params['sort-order'])) {
return $default;

$orders = $params['sort-order'] ?? [];
if (!is_array($orders)) {
$orders = [$orders];
}
if ($params['sort-order'] != 'asc' && $params['sort-order'] != 'desc') {

if (!isset($orders[$index]) || !in_array($orders[$index], $allowed)) {
return $default;
}
return ($params['sort-order'] == 'asc') ? QueryInterface::SORT_ASC :
QueryInterface::SORT_DESC;

return strtoupper($orders[$index]);
}

/**
Expand Down

0 comments on commit cac1e7a

Please sign in to comment.