You are here

public function SearchApiMultiQuery::sort in Search API Multi-Index Searches 7

Add a sort directive to this search query.

If no sort is manually set, the results will be sorted descending by relevance.

How sorts on index-specific fields are handled may differ between service backends.

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.

string $order: The order to sort items in - either 'ASC' or 'DESC'.

Return value

SearchApiMultiQueryInterface The called object.

Throws

SearchApiException If the field is multi-valued or of a fulltext type.

Overrides SearchApiMultiQueryInterface::sort

File

./search_api_multi.query.inc, line 648

Class

SearchApiMultiQuery
Standard implementation of SearchApiMultiQueryInterface.

Code

public function sort($field, $order = 'ASC') {
  if ($field != 'search_api_relevance' && $field != 'search_api_id') {
    list($index_id, $f) = explode(':', $field, 2);
    $index = $this->indexes[$index_id];
    $fields = $index->options['fields'];
    if (empty($fields[$f])) {
      throw new SearchApiException(t('Trying to sort on unknown field @field.', array(
        '@field' => $f,
      )));
    }
    $type = $fields[$f]['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' => $f,
        '@type' => $type,
      )));
    }
    $this->used_indexes[$index_id] = TRUE;
  }
  $order = strtoupper(trim($order)) == 'DESC' ? 'DESC' : 'ASC';
  $this->sort[$field] = $order;
  return $this;
}