public function SearchApiQuery::sort in Search API 7
Adds a sort directive to this search query.
If no sort is manually set, the results will be sorted descending by relevance.
Parameters
string $field: The field to sort by. The special fields 'search_api_relevance' (sort by relevance) and 'search_api_id' (sort by item id) may be used. Also, if the search server supports the "search_api_random_sort" feature, the "search_api_random" special field can be used to sort randomly.
string $order: The order to sort items in - either 'ASC' or 'DESC'.
Return value
SearchApiQueryInterface The called object.
Throws
SearchApiException If the field is multi-valued or of a fulltext type.
Overrides SearchApiQueryInterface::sort
File
- includes/
query.inc, line 597 - Contains SearchApiQueryInterface and SearchApiQuery.
Class
- SearchApiQuery
- Provides a standard implementation of the SearchApiQueryInterface.
Code
public function sort($field, $order = 'ASC') {
$fields = $this->index->options['fields'];
$fields += array(
'search_api_relevance' => array(
'type' => 'decimal',
),
'search_api_id' => array(
'type' => 'integer',
),
);
if ($this
->getIndex()
->server()
->supportsFeature('search_api_random_sort')) {
$fields['search_api_random'] = array(
'type' => 'integer',
);
}
if (empty($fields[$field])) {
throw new SearchApiException(t('Trying to sort on unknown field @field.', array(
'@field' => $field,
)));
}
$type = $fields[$field]['type'];
if (search_api_is_list_type($type) || search_api_is_text_type($type)) {
throw new SearchApiException(t('Trying to sort on field @field of illegal type @type.', array(
'@field' => $field,
'@type' => $type,
)));
}
$order = strtoupper(trim($order)) == 'DESC' ? 'DESC' : 'ASC';
$this->sort[$field] = $order;
return $this;
}