This is just for future reference.

If you're using the flag module you can index flags like this:

function hook_apachesolr_update_index(&$document, $node) {
  $flag_id = 1; // You have to look up your flag id yourself.
  $count = db_result(db_query("SELECT count FROM {flag_counts} f WHERE content_type = 'node' AND content_id = %d AND fid = %d", $node->nid, $flag_id));
  // Note the is_ prefix is important; it tells Solr to make a new dynamic field that is a single integer.
  $document->is_flag_count = $count;
}

Then you need to guarantee that the flags get updated in the index later, so every time a node gets flagged you mark it for re-indexing.

/**
  * Implementation of hook_flag from the flag module so that we can reindex nodes that get flagged.
*/
function hook_flag($op, $flag, $content_id, $account) {
  if ($flag->content_type == 'node') {
    // In node_load's static caching we trust.
    $node = node_load($content_id);
    _apachesolr_nodeapi_update($node);
  }
}

Comments

Scott Reynolds’s picture

Don't you want to mark the node instead of _apachesolr_nodeapi_update() ?

/**
 * Mark one node as needing re-indexing.
 */
function apachesolr_mark_node($nid)

The nodeapi_update checks if the node should be removed from the index based on $node->status. Flag doesn't change that.

Heres how i did it for a specific flag name.

/**
 * Implementation of hook_flag().
 */
function hook_flag($event, $flag, $content_id, $account) {
  if ($flag->name == 'watchlist') {
    if ($event == 'flag' || $event == 'unflag') {
      apachesolr_mark_node($content_id);
    }
  }
}
robertdouglass’s picture

Indeed, that's much nicer. Thanks!

Status: Fixed » Closed (fixed)

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

maxmic’s picture

Please, Can you help me? Where can I put the above code? On a new module? in Flag module.

Many thanks,

Massimo

djalloway’s picture

If one wanted to Bias Negatively nodes which are flagged, what would I need to adjust in the above code to do so?
Any pointers in the right direction would be appreciated!

bibo’s picture

I wonder how to put user-specific flagging (which could be used to say "I like this content") into Solr.
Probably not with this approach?

a_c_m’s picture

Being able to change results based on flag would be great.

cpliakas’s picture

Comments #4 - #7, this issue is closed and these are separate topics. If you want to discuss these support / feature requests it would be better to open separate issues so they can be tracked individually.

Thanks,
Chris