You are here

function apachesolr_index_nodes in Apache Solr Search 5.2

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

Function to handle the indexing of nodes.

The calling function must supply a name space or track/store the timestamp and nid returned. Returns FALSE if no nodes were indexed (none found or error).

1 call to apachesolr_index_nodes()
apachesolr_search_update_index in ./apachesolr_search.module
Implementation of hook_update_index().

File

./apachesolr.module, line 368
Integration with the Apache Solr search application.

Code

function apachesolr_index_nodes($rows, $namespace = '', $callback = 'apachesolr_add_node_document') {
  if (!$rows) {

    // Nothing to do.
    return FALSE;
  }
  try {

    // Get the $solr object
    $solr = apachesolr_get_solr();

    // If there is no server available, don't continue.
    if (!$solr
      ->ping(variable_get('apachesolr_ping_timeout', 4))) {
      throw new Exception(t('No Solr instance available during indexing.'));
    }
  } catch (Exception $e) {
    watchdog('Apache Solr', nl2br(check_plain($e
      ->getMessage())), WATCHDOG_ERROR);
    return FALSE;
  }
  include_once drupal_get_path('module', 'apachesolr') . '/apachesolr.index.inc';
  $documents = array();
  $old_position = apachesolr_get_last_index($namespace);
  $position = $old_position;

  // Always build the content for the index as an anonynmous user.
  global $user;
  session_save_session(FALSE);
  $saved_user = $user;
  $user = drupal_anonymous_user();
  foreach ($rows as $row) {
    try {
      $callback($documents, $row->nid, $namespace);

      // Variables to track the last item changed.
      $position['last_change'] = $row->changed;
      $position['last_nid'] = $row->nid;
    } catch (Exception $e) {

      // Something bad happened - don't continue.
      watchdog('Apache Solr', t('Error constructing documents to index: <br /> !message', array(
        '!message' => nl2br(strip_tags($e
          ->getMessage())),
      )), WATCHDOG_ERROR);
      break;
    }
  }

  // Restore the user.
  $user = $saved_user;
  session_save_session(TRUE);
  if (count($documents)) {
    try {
      watchdog('Apache Solr', t('Adding @count documents.', array(
        '@count' => count($documents),
      )));

      // Chunk the adds by 20s
      $docs_chunk = array_chunk($documents, 20);
      foreach ($docs_chunk as $docs) {
        $solr
          ->addDocuments($docs);
      }

      // Set the timestamp to indicate an index update.
      apachesolr_index_updated(time());
    } catch (Exception $e) {
      $nids = array();
      if (!empty($docs)) {
        foreach ($docs as $doc) {
          $nids[] = $doc->nid;
        }
      }
      watchdog('Apache Solr', t('Indexing failed on one of the following nodes: @nids <br /> !message', array(
        '@nids' => implode(', ', $nids),
        '!message' => nl2br(strip_tags($e
          ->getMessage())),
      )), WATCHDOG_ERROR);
      return FALSE;
    }
  }

  // Save the new position in case it changed.
  if ($namespace && $position != $old_position) {
    $stored = variable_get('apachesolr_index_last', array());
    $stored[$namespace] = $position;
    variable_set('apachesolr_index_last', $stored);
  }
  return $position;
}