diff --git a/contrib/search_api_views/includes/query.inc b/contrib/search_api_views/includes/query.inc index ee551bb..64ad62b 100644 --- a/contrib/search_api_views/includes/query.inc +++ b/contrib/search_api_views/includes/query.inc @@ -126,8 +126,11 @@ class SearchApiViewsQuery extends views_plugin_query { * * @param $selector * The field to sort on. All indexed fields of the index are valid values. - * In addition, the special fields 'search_api_relevance' (sort by - * relevance) and 'search_api_id' (sort by item id) may be used. + * In addition, the special fields may be used: + * - 'search_api_relevance': sort by relevance + * - 'search_api_id': sort by item id + * - 'search_api_random_sort': random sort (available only if the server + * supports the "search_api_random_sort" feature) * @param $order * The order to sort items in - either 'ASC' or 'DESC'. Defaults to 'ASC'. */ @@ -136,6 +139,32 @@ class SearchApiViewsQuery extends views_plugin_query { } /** + * Implement the same add_orderby() method as views_plugin_query_default so + * that Views' "Global: Random" sort can be used if the server supports the + * "search_api_random_sort" feature. + * + * @param $params + * The available set of parameters for the search_api_random are: + * - 'seed': a predefined seed for the random generator + */ + public function add_orderby($table, $field = NULL, $order = 'ASC', $alias = '', $params = array()) { + $server = $this->getIndex()->server(); + if ($table == 'rand') { + if ($server->supportsFeature('search_api_random_sort')) { + $this->add_selector_orderby('search_api_random_sort', $order); + if (!empty($params)) { + $this->setOption('search_api_random_sort', $params); + } + } else { + $class = search_api_get_service_info($server->class); + watchdog('search_api_views', 'The search service "@class" does not offer "Random sort" functionality.', + array('@class' => $class['name']), WATCHDOG_ERROR); + $this->abort('This search service does not offer "Random sort" functionality.'); + } + } + } + + /** * Defines the options used by this query plugin. * * Adds some access options. diff --git a/includes/query.inc b/includes/query.inc index b005cf7..2206dba 100644 --- a/includes/query.inc +++ b/includes/query.inc @@ -586,6 +586,12 @@ class SearchApiQuery implements SearchApiQueryInterface { 'search_api_relevance' => array('type' => 'decimal'), 'search_api_id' => array('type' => 'integer'), ); + + $server = $this->getIndex()->server(); + if ($server->supportsFeature('search_api_random_sort')) { + $fields['search_api_random_sort'] = array('type' => 'integer'); + } + if (empty($fields[$field])) { throw new SearchApiException(t('Trying to sort on unknown field @field.', array('@field' => $field))); }