You are here

function apachesolr_index_send_to_solr in Apache Solr Search 6.3

Same name and namespace in other branches
  1. 8 apachesolr.index.inc \apachesolr_index_send_to_solr()
  2. 7 apachesolr.index.inc \apachesolr_index_send_to_solr()

Index an array of documents to solr.

Return value

number indexed, or FALSE on failure.

1 call to apachesolr_index_send_to_solr()
apachesolr_index_entities in ./apachesolr.index.inc
Processes all index queues associated with the passed environment.

File

./apachesolr.index.inc, line 306
Functions related to Apache Solr indexing operations.

Code

function apachesolr_index_send_to_solr($env_id, $documents) {
  try {

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

    // 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())), NULL, WATCHDOG_ERROR);
    return FALSE;
  }

  // Do not index when we do not have any documents to send
  // Send TRUE because this is not an error
  if (empty($documents)) {
    return TRUE;
  }

  // Send the document off to Solr.
  watchdog('Apache Solr', 'Adding @count documents.', array(
    '@count' => count($documents),
  ));
  try {
    $docs_chunk = array_chunk($documents, 20);
    foreach ($docs_chunk as $docs) {
      $solr
        ->addDocuments($docs);
    }
    watchdog('Apache Solr', 'Indexing succeeded on @count documents', array(
      '@count' => count($documents),
    ), WATCHDOG_INFO);
    return count($documents);
  } catch (Exception $e) {
    if (!empty($docs)) {
      foreach ($docs as $doc) {
        $eids[] = $doc->entity_type . '/' . $doc->entity_id;
      }
    }
    watchdog('Apache Solr', 'Indexing failed on one of the following entity ids: @eids <br /> !message', array(
      '@eids' => implode(', ', $eids),
      '!message' => nl2br(strip_tags($e
        ->getMessage())),
    ), WATCHDOG_ERROR);
    return FALSE;
  }
}