- apachesolr_search_update_index in contributions/apachesolr/apachesolr_search.module
- Implementation of hook_update_index().
apachesolr_index_nodes($rows, $namespace = '', $callback = 'apachesolr_add_node_document')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).
contributions/apachesolr/apachesolr.module, line 324
<?php
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())), NULL, 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;
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', 'Error constructing documents to index: <br /> !message', array('!message' => nl2br(strip_tags($e->getMessage()))), WATCHDOG_ERROR);
break;
}
}
if (count($documents)) {
try {
watchdog('Apache Solr', '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', '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;
}
?>