diff --git a/contrib/search_api_views/README.txt b/contrib/search_api_views/README.txt index bae140f..9e41763 100644 --- a/contrib/search_api_views/README.txt +++ b/contrib/search_api_views/README.txt @@ -24,6 +24,10 @@ When these are present, the normal keywords should be ignored and the related items be returned as results instead. Sorting, filtering and range restriction should all work normally. +"Random sort" feature +--------------------- +@todo Description + "Facets block" display ---------------------- Most features should be clear to users of Views. However, the module also diff --git a/contrib/search_api_views/includes/query.inc b/contrib/search_api_views/includes/query.inc index 64ad62b..1b66b67 100644 --- a/contrib/search_api_views/includes/query.inc +++ b/contrib/search_api_views/includes/query.inc @@ -122,16 +122,16 @@ class SearchApiViewsQuery extends views_plugin_query { } /** - * Add a sort to the query. + * Adds a sort to the query. * - * @param $selector + * @param string $selector * The field to sort on. All indexed fields of the index are valid values. - * 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 + * In addition, these 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 string $order * The order to sort items in - either 'ASC' or 'DESC'. Defaults to 'ASC'. */ public function add_selector_orderby($selector, $order = 'ASC') { @@ -139,27 +139,42 @@ 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. + * Provides a sorting method as present in the Views default query plugin. * - * @param $params - * The available set of parameters for the search_api_random are: - * - 'seed': a predefined seed for the random generator + * This is provided so that the "Global: Random" sort included in Views will + * work properly with Search API Views. Random sorting is only supported if + * the active search server supports the "search_api_random_sort" feature, + * though, otherwise the call will be ignored. + * + * This method can only be used to sort randomly, as would be done with the + * default query plugin. All other calls are ignored. + * + * @param string|null $table + * Only "rand" is recognized here, all other calls are ignored. + * @param string|null $field + * Is ignored and only present for compatibility reasons. + * @param string $order + * Either "ASC" or "DESC". + * @param string|null $alias + * Is ignored and only present for compatibility reasons. + * @param array $params + * The following optional parameters are recognized: + * - seed: a predefined seed for the random generator. + * + * @see views_plugin_query_default::add_orderby() */ 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)) { + if ($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.'); + } + else { + $variables['%server'] = $server->label(); + watchdog('search_api_views', 'Tried to sort results randomly on server %server which does not support random sorting.', $variables, WATCHDOG_WARNING); } } } diff --git a/includes/query.inc b/includes/query.inc index 2206dba..771446e 100644 --- a/includes/query.inc +++ b/includes/query.inc @@ -153,7 +153,9 @@ interface SearchApiQueryInterface { * * @param 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. + * 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_sort" special field can be used to sort randomly. * @param string $order * The order to sort items in - either 'ASC' or 'DESC'. * @@ -586,9 +588,7 @@ 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')) { + if ($this->getIndex()->server()->supportsFeature('search_api_random_sort')) { $fields['search_api_random_sort'] = array('type' => 'integer'); }