You are here

function apachesolr_mlt_suggestions in Apache Solr Search 5

Same name and namespace in other branches
  1. 5.2 apachesolr.module \apachesolr_mlt_suggestions()
  2. 6 apachesolr.module \apachesolr_mlt_suggestions()
  3. 6.2 apachesolr.module \apachesolr_mlt_suggestions()

function apachesolr_mlt_suggestions() This function loads a the parameters for each moreLikeThis query, performs the query, and returns a list of linked node titles.

Parameters

int $block_id A block ID for loading the suggestions:

Return value

array An array to be returned to hook_block

1 call to apachesolr_mlt_suggestions()
apachesolr_mlt_block in contrib/apachesolr_mlt/apachesolr_mlt.module
Implementation of hook_block

File

contrib/apachesolr_mlt/apachesolr_mlt.module, line 59

Code

function apachesolr_mlt_suggestions($block_id) {
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = check_plain(arg(1));
    $solr =& apachesolr_get_solr(variable_get('apachesolr_host', 'localhost'), variable_get('apachesolr_port', 8983), variable_get('apachesolr_path', '/solr'));
    try {
      $fields = array(
        'mlt.mintf',
        'mlt.mindf',
        'mlt.minwl',
        'mlt.maxwl',
        'mlt.maxqt',
        'mlt.boost',
        'mlt.qf',
      );
      $block = apachesolr_mlt_load_block($block_id);
      $params = array(
        'mlt' => 'true',
        'fl' => 'nid,title',
        'mlt.fl' => check_plain(implode(',', $block['mlt_fl'])),
      );
      unset($block['mlt_fl']);
      foreach ($fields as $field) {
        $drupal_fieldname = str_replace('.', '_', $field);
        if (!empty($block[$drupal_fieldname])) {
          $params[$field] = check_plain($block[$drupal_fieldname]);
        }
      }
      $response = $solr
        ->search('nid:' . $nid, 0, 10, $params);
      $r = (array) end($response->moreLikeThis);

      //TODO: Figure out why this works
      $links = array();
      if (is_array($r['docs'])) {
        foreach ($r['docs'] as $doc) {
          $links[] = l($doc->title, 'node/' . $doc->nid);
        }
      }
      $suggestions = array();
      if (count($links) > 0) {
        $suggestions['subject'] = $block['name'];
        $suggestions['content'] = theme('apachesolr_mlt_recommendation_block', $links);
      }
      return $suggestions;
    } catch (Exception $e) {
      watchdog('Apache Solr', $e
        ->getMessage(), WATCHDOG_ERROR);
    }
  }
}