You are here

function apachesolr_index_send_to_solr in Apache Solr Search 8

Same name and namespace in other branches
  1. 6.3 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.

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 326
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.
  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;
  }
}