Skip to content

Commit

Permalink
Switch to comma-separated values for search API sorts (#3760)
Browse files Browse the repository at this point in the history
* Switch to comma-separated values
* Update docs
  • Loading branch information
dafeder committed Mar 4, 2022
1 parent ddcb10d commit 827bb91
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
25 changes: 16 additions & 9 deletions modules/metastore/modules/metastore_search/docs/openapi_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,30 @@
{
"name": "sort",
"in": "query",
"description": "Which property to sort results on. Use array format to sort on multiple properties, e.g. sort[0]=field1&sort[1]=field2",
"description": "Which property to sort results on.",
"schema": {
"type": "string"
"type": "array",
"items": {
"type": "string",
"default": "title"
}
},
"style": "form"
"style": "form",
"explode": false
},
{
"name": "sort-order",
"in": "query",
"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",
"description": "Sort results in ascending or descending order. Allowed values: <em>asc, desc</em>",
"schema": {
"type": "string",
"default": "asc",
"enum": [ "asc", "desc"]
"type": "array",
"items": {
"type": "string",
"default": "asc"
}
},
"example": "desc",
"style": "form"
"style": "form",
"explode": false
},
{
"name": "facets",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ public static function create(
public function spec() {
$spec = $this->getDoc('metastore_search');

$spec['paths']['/api/1/search']['get']['parameters'][3]['schema']['enum'] = $this->getIndexedFields();
$propList = $this->t('Available properties: %list', [
'%list' => implode(", ", $this->getIndexedFields()),
]);
$spec['paths']['/api/1/search']['get']['parameters'][3]['description'] .= " $propList";

$facetTypes = $this->getFacetTypes();
foreach ($facetTypes as $type => $example) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,11 @@ private function setSort(QueryInterface $query, array $params, IndexInterface $i
$fields = array_keys($index->getFields());

$sorts = $params['sort'] ?? [];
if (!is_array($sorts)) {
$sorts = [$sorts];
if (is_string($sorts)) {
// @todo Move this into Util class to share with FacetsCommonTrait.
$sorts = array_map('trim', str_getcsv($sorts));
}

if (empty($sorts)) {
$query->sort('search_api_relevance', Query::SORT_DESC);
}
Expand Down Expand Up @@ -194,8 +196,8 @@ private function getSortOrder(array $params, int $index = 0) {
$default = QueryInterface::SORT_ASC;

$orders = $params['sort-order'] ?? [];
if (!is_array($orders)) {
$orders = [$orders];
if (is_string($orders)) {
$orders = array_map('trim', str_getcsv($orders));
}

if (!isset($orders[$index]) || !in_array($orders[$index], $allowed)) {
Expand Down

0 comments on commit 827bb91

Please sign in to comment.