You are here

function apachesolr_process_response in Apache Solr Search 5.2

Same name and namespace in other branches
  1. 6 apachesolr_search.module \apachesolr_process_response()
1 call to apachesolr_process_response()
apachesolr_search_execute in ./apachesolr_search.module
Execute a search results based on keyword, filter, and sort strings.

File

./apachesolr_search.module, line 345
Provides a content search implementation for node content for use with the Apache Solr search application.

Code

function apachesolr_process_response($response, $query, $params) {
  $results = array();

  // We default to getting snippets from the body.
  $hl_fl = is_null($params['hl.fl']) ? 'body' : $params['hl.fl'];
  $total = $response->response->numFound;
  apachesolr_pager_init($total, $params['rows']);
  if ($total > 0) {
    foreach ($response->response->docs as $doc) {
      $extra = array();

      // Find the nicest available snippet.
      if (isset($response->highlighting->{$doc->id}->{$hl_fl})) {
        $snippet = theme('apachesolr_search_snippets', $doc, $response->highlighting->{$doc->id}->{$hl_fl});
      }
      elseif (isset($doc->teaser)) {
        $snippet = theme('apachesolr_search_snippets', $doc, array(
          truncate_utf8($doc->teaser, 256, TRUE),
        ));
      }
      else {
        $snippet = '';
      }
      if (!isset($doc->body)) {
        $doc->body = $snippet;
      }
      $doc->created = strtotime($doc->created);
      $doc->changed = strtotime($doc->changed);

      // Allow modules to alter each document.
      drupal_alter('apachesolr_search_result', $doc);
      $fields = array();
      foreach ($doc
        ->getFieldNames() as $field_name) {
        $fields[$field_name] = $doc
          ->getField($field_name);
      }

      // Copy code from comment_nodeapi().
      $extra[] = format_plural($doc->comment_count, '1 comment', '@count comments');
      $results[] = array(
        'link' => url($doc->path, NULL, NULL, TRUE),
        'type' => apachesolr_search_get_type($doc->type),
        // template_preprocess_search_result() runs check_plain() on the title
        // again.  Decode to correct the display.
        'title' => htmlspecialchars_decode($doc->title, ENT_QUOTES),
        'user' => theme('username', $doc),
        'date' => $doc->created,
        'node' => $doc,
        'extra' => $extra,
        'score' => $doc->score,
        'snippet' => $snippet,
        'fields' => $fields,
      );
    }

    // Hook to allow modifications of the retrieved results
    foreach (module_implements('apachesolr_process_results') as $module) {
      $function = $module . '_apachesolr_process_results';
      $function($results);
    }
  }
  return $results;
}