Similar to this issue, I would like to add the fivestar results to the Apache Solr search results. I know how to add field to the Solr index, so that's not the problem. The problem is that the Fivestar results don't seem to available anywhere in the $node object (which is passed to hook_apachesolr_update_index()). According to fivestar_nodeapi(), the form is added to the node when it is being viewed, but not at other times. To make this searchable, it would need to be added to the search index when $op == 'update index'. The question is, what function would I call to pass that value to Solr? Would it be fivestar_get_votes, or something else?

Then, once I have the value indexed and I want to display the results, I'm assuming I would call theme_fivestar_summary in my theme function or template.

Does all that sound correct?

Thanks.

Comments

shaneonabike’s picture

I've been working on a solution for this and might have it completed within the next few days here. Are you still looking for that solution?

intraclast’s picture

Hi, I'm trying to find out how to do the samething (display my fivestar ratings within apache solr search results). Did either of you manage to get this working?

Thanks

nvl.sateesh’s picture

Hi Shane,

I am looking for such solution. Please post if you have it.

I am using Apachesolr and would liek to integrate fivestar results in sorting as well as results....

thanks and have a great day!

shaneonabike’s picture

We did actually get this working (apologise for not posting sooner just have some pretty big deadlines). Also, sorry this isn't in an actual module but I think it'll still help you out regardless.

Apache Solr related code

/**
  * Modify the query string attributes prior to it being created
  * @param $query
  *  Query string to modify
**/
function modsolr_apachesolr_prepare_query(&$query, &$params) {
  // Add the rating sort
  $query->set_available_sort('sis_ratings', array(
        'title' => t('Rating'),
        'default' => 'desc',
  ));

  // Add a default date sort if nothing has been set
  if (empty($query->sortstring)) {
    $query->set_solrsort('created','desc');
  }

}

/**
  * hook_apachesolr_update_index
  *
  * Used to modify additional elements that should be indexed by the apache solr engine
  *
  * @param $document
  * @param $node
  *  Node element
  * @param $namespace
**/
function modsolr_apachesolr_update_index(&$document, $node, $namespace) {
    // Grab the votes for this node and index
    $votes =  fivestar_get_votes('node', $node->nid);
    $average = $votes['average']['value'];
    $document->sis_ratings = empty($average) ? 0 : $average;
}

Creating a block code

function modsolr_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $enabled_facets = apachesolr_get_enabled_facets('modsolr');
      $facets = olesolr_apachesolr_facets();
      // Add the blocks
      $blocks = array();
      foreach ($enabled_facets as $delta => $facet_field) {
        if (isset($facets[$delta])) {
          $blocks[$delta] = $facets[$delta] + array('cache' => BLOCK_CACHE_PER_PAGE,);
        }
      }
      return $blocks;

    case 'view':
      if (apachesolr_has_searched()) {
        $response = apachesolr_static_response_cache();
        if (empty($response)) {
          return;
        }
        $query = apachesolr_current_query();

        return apachesolr_facet_block($response, $query, 'modsolr', $delta, $delta, t('Ratings'), 'Ratings');
      }
      break;
    case 'configure':
      return apachesolr_facetcount_form('modsolr', $delta);
    case 'save':
      apachesolr_facetcount_save($edit);
      break;
  }
}

Update Solr Data after each rating


/**
 * Hook implementation for VotingAPI (sent out by Ratings)
 * Usage: Ratings are being indexed and by catching this hook we can update solr dynamically
 *
 * $cached - cached rating details
 * $content_type - content type being processed
 * $content_id - the node id
 *
**/
function olesolr_votingapi_results($cached, $content_type, $content_id) {
  // Update ApacheSolr for ratings
  if ($content_type == 'node' || $content_type == 'comment') {
    apachesolr_mark_node($content_id);
  }
}

Code for template.php


function theme_apachesolr_unclick_link($facet_text, $path, $options = array()) {
  apachesolr_js();

  // Determine if we are dealing with ratings output
  if ( $options['delta'] == 'sis_ratings' ) {
    $options['html'] = TRUE;
    $text = theme('fivestar_static', $facet_text, variable_get('fivestar_stars_resource', 5));
  }

  if (empty($options['html'])) {
    $text = check_plain($facet_text);
  }
  else {
    // Don't pass this option as TRUE into apachesolr_l().
    unset($options['html']);
  }
  $options['attributes']['class'] = 'apachesolr-unclick';
  return apachesolr_l("(-)", $path, $options) . ' '. $text;
}

/*
  * Override the theme output for facet links - specifically ratings
  *
  * $facet_text - text for link
  * $path - path for link
  * $options - various class / styling attributes
  * $count - count of solr results
  * $active - active link?
  * $num_found
  *
  * @return - link output
**/
function theme_apachesolr_facet_link($facet_text, $path, $options = array(), $count, $active = FALSE, $num_found = NULL) {
  // Determine if we are dealing with ratings output
  if ( $options['delta'] == 'sis_ratings' ) {
    $options['html'] = TRUE;
    $text = theme('fivestar_static', $facet_text, variable_get('fivestar_stars_resource', 5)) . "<div class=\"fivestar-widget-static\">($count)</div>";
  } else {
    $text = $facet_text ." ($count)";
  }

  // Setup other variables before output
  $options['attributes']['class'][] = 'apachesolr-facet';
  if ($active) {
    $options['attributes']['class'][] = 'active';
  }
  $options['attributes']['class'] = implode(' ', $options['attributes']['class']);
  return apachesolr_l($text,  $path, $options);
}
intraclast’s picture

Thanks very much for posting this. I'm pretty new to coding in drupal and how it all works but this is definitely pointing me in the right direction here. Now I just have to learn how to make a module... (I said I was new :) )

shaneonabike’s picture

seanb’s picture

After placing the code from #4 in a module, I found that there is a function missing: olesolr_apachesolr_facets()

I guess this is a custom function as I can't find any olesolr module and you have another function named olesolr_votingapi_results().

Could you please post the code for olesolr_apachesolr_facets() or maybe describe what this function is supposed to return? If I get this all working I will post the complete module code here.

incaic’s picture

Thanks @ShaneOnABike! Code from #4 worked for me.
FYI: Your koumbit.org link from #6 is not open to the public.

@seanB, I renamed the missing function

  $facets = olesolr_apachesolr_facets();

to

  $facets = modsolr_apachesolr_facets();

and defined it as

function modsolr_apachesolr_facets() {
  $facets['sis_ratings'] = array(
    'info' => t('Ratings'),
    'facet_field' => 'sis_rating',
  );
  return $facets;
}

Also, olesolr_votingapi_results() should be renamed to modsolr_votingapi_results().

ericduran’s picture

Status: Active » Fixed

I'm going to close this issue as it's not really a fivestar issue.

Feel free to open it up again if needed.

shaneonabike’s picture

Sorry about the issues with the article posting I didn't see that comment until now...

http://www.koumbit.org/en/articles/how-integrate-fivestar-ratings-apache... is now live if people need that. Also the changes above are correct.

incaic’s picture

@ShaneOnABike

FYI The code sections are unreadable. They have html tags spelled out inside of them.

Status: Fixed » Closed (fixed)

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

BeaPower’s picture

will this work with views too?

Zereff’s picture

Status: Closed (fixed) » Needs review

Hello! Sorry, but I don't understood where your paste this snippets. How you create module or change old modules.
Tell me please where you paste all snippets for search by fivestar sorting? I view your article about integrated fivestar rating in apachesolr, but I don't understood where to put your snippets in which module? May be I need to create new module and think about how to use your code?

BeaPower’s picture

Will this work for search api solr search?

dave reid’s picture

Issue summary: View changes
Status: Needs review » Closed (fixed)

Restoring status as per #9.