You are here

function apachesolr_search_process_response in Apache Solr Search 6.2

Same name and namespace in other branches
  1. 8 apachesolr_search.module \apachesolr_search_process_response()
  2. 6.3 apachesolr_search.module \apachesolr_search_process_response()
  3. 7 apachesolr_search.module \apachesolr_search_process_response()
1 call to apachesolr_search_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 593
Provides a content search implementation for node content for use with the Apache Solr search application.

Code

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

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

      // Snippets.
      // Start with an empty snippets array.
      $snippets = array();

      // Find the nicest available snippet.
      foreach ($hl_fl as $hl_param) {
        if (isset($response->highlighting->{$doc->id}->{$hl_param})) {

          // Merge arrays preserving keys.
          foreach ($response->highlighting->{$doc->id}->{$hl_param} as $values) {
            $snippets[$hl_param] = $values;
          }
        }
      }

      // If there's no snippet at this point, add the teaser.
      if (!$snippets) {
        if (isset($doc->teaser)) {
          $snippets[] = truncate_utf8($doc->teaser, 256, TRUE);
        }
      }
      $snippet = theme('apachesolr_search_snippets', $doc, $snippets);
      if (!isset($doc->body)) {
        $doc->body = $snippet;
      }
      $doc->created = strtotime($doc->created);
      $doc->changed = strtotime($doc->changed);
      $extra = array();
      $extra['comments'] = format_plural($doc->comment_count, '1 comment', '@count comments');
      if (isset($doc->is_upload_count)) {
        $extra[] = format_plural($doc->is_upload_count, '1 attachment', '@count attachments');
      }

      // Allow modules to alter each document and its extra information.
      $data = array(
        $doc,
      );
      $data['__drupal_alter_by_ref'] = array(
        &$extra,
      );
      drupal_alter('apachesolr_search_result', $data);
      $fields = (array) $doc;
      $results[] = array(
        'link' => url($doc->path, array(
          'absolute' => 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,
        'snippets' => $snippets,
        '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;
}