I needed to allow users to sort results by the number of bookmark flags. I implemented hook_apachesolr_update_index() and hook_apachesolr_prepare_query() to get Solr to index this information and present it as sort option, but still needed to integrate this sort with Views. I did it with the attached patch, which obviously should not be incorporated into this module, but might serve as useful sample code for anyone else needing to implement a custom sort. If there's a better way to extend this module by implementing hooks in a custom module, I'd love to hear it, but this works for me.

CommentFileSizeAuthor
apachesolr_views.patch2.67 KBmvc

Comments

Scott Reynolds’s picture

Good news is you don't have to do it this way. This is the proper way to do it.

function my_module_views_data_alter(&$data) {
  $data['apachesolr']['is_flag_count'] = array(
    'title' => t('Flag count'),
    'help' => t('The number of flags for the node.'),
    'field' => array(
      'numeric' => TRUE,
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'apachesolr_views_handler_sort',
    ),
}

and apachesolr_views_field_generic is going away eventually, it was a first pass at creating solid field support (and it doesn't at all effect this patch).

mvc’s picture

Status: Needs review » Fixed

that works perfectly. thanks, i'll close this.

i wrote up how we used your module here: http://www.koumbit.org/en/node/15808

Scott Reynolds’s picture

Excellent, should be noted, that with a recent commit, this is now changed to:

function my_module_views_data_alter(&$data) {
  $data['apachesolr_node']['is_flag_count'] = array(
    'title' => t('Flag count'),
    'help' => t('The number of flags for the node.'),
    'field' => array(
      'numeric' => TRUE,
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'apachesolr_views_handler_sort',
    ),
}

The Views table has now been update to reflect entities that are indexed through Solr. See #576286: Add general SQL support

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jpdaut’s picture

I have implemented hook_apachesolr_update_index(), hook_apachesolr_prepare_query() and hook_views_data_alter() in a small module. However when I select my new sort option in Views and click Update I invariably get: "The Apache Solr search engine is not available." and I can't do any search after that. Does anyone have any clue on what the problem could be?

jpdaut’s picture

Never mind. I reverted back to my last backup which works.