I have a Solr view i'd like to sort by popularity, i.e. by "Apache Solr Node statistics: Total views", but it's not offered as a sort criterion in the Views UI. So i followed a few leads and managed to add it into the sort criteria by adding this to my custom module:
/**
* Implementation of hook_views_data_alter()
*
* Exposes new fields and sorts to views
*/
function mymodule_views_data_alter(&$data) {
$data['apachesolr_node']['totalcount'] = array(
'title' => t('Total views'),
'help' => t('The total number of views for this node.'),
'field' => array(
'handler' => 'views_handler_field_node_type',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'apachesolr_views_handler_sort',
),
);
}
That works perfectly: it's now offered as a search criterion in the Views UI, and it can be exposed like any other sort. But it doesn't have any effect on the search results. Checking the actual GET request made to the Solr server, i see: sort=sort_title+asc,created+desc (title and created were my other exposed sorts). No mention of the totalcount sort.
Doublechecking that the field actually exists in the solr index (localhost/solr/admin), i find that it's not! I suppose the Views UI offering it as an apachesolr field must have confused me.
Anyway, my question is: am i doing something stupid, or should i just get down to it and add the totalcount field to the index myself using hook_apachesolr_update_index()?
Comments
Comment #1
chichilatte commentedOk, i added a new is_totalcount field to the index and made sure apachesolr_views was able to use it for sorting...
Now when i sort by is_totalcount using the exposed form, the GET request to Solr contains:
sort=sort_title+asc,created+desc,is_totalcount+desc. Brilliant!But the is_totalcount is last in the list, rather than first, so it still has no effect. I've just noticed that this also happens for the the "created" sort field too. The sort ordering in the GET request never changes! I wonder if this is a bug, or is this a unicorny feature?
Comment #2
chichilatte commentedI made a quick patch to the apachesolr_views module to allow changing the priority of sorting. Very simple change in apachesolr_views/apachesolr_views_query.inc:
Then in your custom module you'd have something like...
I would have used
mymodule_apachesolr_prepare_query()instead ofmymodule_apachesolr_modify_query(), but the prepare query hook didn't seem to be working for me :( Anyway i hope this is of help to someone, since sorting is surely extremely popular when doing a search. Maybe the apachesolr_views module will one day allow this so we don't need the patch?NOTE: tested with D6.22, Views 6.x-3.0+67-dev, Apache Solr Search 6.x-1.5, Apache Solr Views Integration 6.x-1.x-dev, Better Exposed Filters 6.x-3.x-dev.
++NOTE: If you don't want to patch the apachesolr_views module, i see these people have found a more elaborate route (which is a bit of a kludge).
Comment #4
kenorb commentedVersion 6.x is no longer supported as of Drupal 6 End of Life. For Drupal 8.x, use Search API Solr Search instead.