You are here

function apachesolr_index_send_to_solr in Apache Solr Search 7

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

Index an array of documents to solr.

Parameters

$env_id:

array $documents:

Return value

bool|int number indexed, or FALSE on failure.

Throws

Exception

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 337
Functions related to Apache Solr indexing operations.

Code

function apachesolr_index_send_to_solr($env_id, array $documents) {

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

  // 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.
  $log_success = variable_get('apachesolr_watchdog_successes', TRUE);
  if ($log_success) {
    watchdog('Apache Solr', 'Environment @env_id: Adding @count documents.', array(
      '@env_id' => $env_id,
      '@count' => count($documents),
    ));
  }
  try {
    $docs_chunk = array_chunk($documents, 20);
    foreach ($docs_chunk as $docs) {
      $solr
        ->addDocuments($docs);
    }
    if ($log_success) {
      watchdog('Apache Solr', 'Environment @env_id: Indexing succeeded on @count documents', array(
        '@env_id' => $env_id,
        '@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', 'Environment @env_id: Indexing failed on one of the following entity ids: @eids <br /> !message', array(
      '@env_id' => $env_id,
      '@eids' => implode(', ', $eids),
      '!message' => nl2br(strip_tags($e
        ->getMessage())),
    ), WATCHDOG_ERROR);
    return FALSE;
  }
}