You are here

function apachesolr_search_process_response in Apache Solr Search 8

Same name and namespace in other branches
  1. 6.3 apachesolr_search.module \apachesolr_search_process_response()
  2. 6.2 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_run in ./apachesolr_search.module
Execute a search results based on keyword, filter, and sort strings.
1 string reference to 'apachesolr_search_process_response'
apachesolr_search_run in ./apachesolr_search.module
Execute a search results based on keyword, filter, and sort strings.

File

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

Code

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

  // We default to getting snippets from the body content and comments.
  $hl_fl = $query
    ->getParam('hl.fl');
  if (!$hl_fl) {
    $hl_fl = array(
      'content',
      'ts_comments',
    );
  }
  $total = $response->response->numFound;
  pager_default_initialize($total, $query
    ->getParam('rows'));
  if ($total > 0) {
    $fl = $query
      ->getParam('fl');
    $languages = language_list();

    // 'id' and 'entity_type' are the only required fields in the schema, and
    // 'score' is generated by solr.
    foreach ($response->response->docs as $doc) {
      $extra = array();

      // Allow modules to alter each document and its extra information.
      drupal_alter('apachesolr_search_result', $doc, $extra, $query);

      // 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 $value) {
            $snippets[$hl_param][] = $value;
          }
        }
      }

      // If there's no snippet at this point, add the teaser.
      if (!$snippets) {
        if (isset($doc->teaser)) {
          $snippets[] = truncate_utf8($doc->teaser, 256, TRUE);
        }
      }
      $hook = 'apachesolr_search_snippets__' . $doc->entity_type;
      $bundle = !empty($doc->bundle) ? $doc->bundle : NULL;
      if ($bundle) {
        $hook .= '__' . $bundle;
      }
      $snippet = theme($hook, array(
        'doc' => $doc,
        'snippets' => $snippets,
      ));
      if (!isset($doc->content)) {
        $doc->content = $snippet;
      }

      // Normalize common dates so that we can use Drupal's normal date and
      // time handling.
      if (isset($doc->ds_created)) {
        $doc->created = strtotime($doc->ds_created);
      }
      else {
        $doc->created = NULL;
      }
      if (isset($doc->ds_changed)) {
        $doc->changed = strtotime($doc->ds_changed);
      }
      else {
        $doc->changed = NULL;
      }
      if (isset($doc->tos_name)) {
        $doc->name = $doc->tos_name;
      }
      else {
        $doc->name = NULL;
      }

      // Set all expected fields from fl to NULL if they are missing so
      // as to prevent Notice: Undefined property.
      $fl = array_merge($fl, array(
        'path',
        'label',
        'score',
      ));
      foreach ($fl as $field) {
        if (!isset($doc->{$field})) {
          $doc->{$field} = NULL;
        }
      }
      $fields = (array) $doc;

      // Define our url options. They depend on the document language.
      $url_options = array(
        'absolute' => TRUE,
      );
      if (isset($doc->ss_language) && isset($languages[$doc->ss_language])) {
        $url_options['language'] = $languages[$doc->ss_language];
      }
      $result = array(
        // link is a required field, so handle it centrally.
        'link' => url($doc->path, $url_options),
        // template_preprocess_search_result() runs check_plain() on the title
        // again.  Decode to correct the display.
        'title' => htmlspecialchars_decode($doc->label, ENT_QUOTES),
        // These values are not required by the search module but are provided
        // to give entity callbacks and themers more flexibility.
        'score' => $doc->score,
        'snippets' => $snippets,
        'snippet' => $snippet,
        'fields' => $fields,
        'entity_type' => $doc->entity_type,
        'bundle' => $bundle,
      );

      // Call entity-type-specific callbacks for extra handling.
      $function = apachesolr_entity_get_callback($doc->entity_type, 'result callback', $bundle);
      if (is_callable($function)) {
        $function($doc, $result, $extra);
      }
      $result['extra'] = $extra;
      $results[] = $result;
    }
  }

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