function apachesolr_index_nodes in Apache Solr Search 6.2
Same name and namespace in other branches
- 5.2 apachesolr.module \apachesolr_index_nodes()
- 6 apachesolr.module \apachesolr_index_nodes()
Handles the indexing of nodes.
Parameters
array $rows: Each $row in $rows must have: $row->nid $row->changed
string $namespace: Usually the calling module. Is used as a clue for other modules when they decide whether to create extra $documents, and is used to track the last_index timestamp.
Return value
timestamp Either a timestamp representing the last value of apachesolr_get_last_index to be indexed, or FALSE if indexing failed.
2 calls to apachesolr_index_nodes()
- apachesolr_batch_index_nodes in ./
apachesolr.admin.inc - Batch Operation Callback
- apachesolr_search_cron in ./
apachesolr_search.module - Implementation of hook_cron(). Indexes nodes.
File
- ./
apachesolr.module, line 420 - Integration with the Apache Solr search application.
Code
function apachesolr_index_nodes($rows, $namespace) {
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;
}
module_load_include('inc', 'apachesolr', 'apachesolr.index');
$documents = array();
$old_position = apachesolr_get_last_index($namespace);
$position = $old_position;
// Invoke hook_apachesolr_document_handlers to find out what modules build $documents
// from nodes in this namespace.
$callbacks = module_invoke_all('apachesolr_document_handlers', 'node', $namespace);
$callbacks = array_filter($callbacks, 'function_exists');
// 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 {
// Build node. Set reset = TRUE to avoid static caching of all nodes that get indexed.
if ($node = node_load($row->nid, NULL, TRUE)) {
foreach ($callbacks as $callback) {
// The callback can either return a $document or an array of $documents.
$documents[] = $callback($node, $namespace);
}
}
// Variables to track the last item changed.
$position['last_change'] = $row->changed;
$position['last_nid'] = $row->nid;
} catch (Exception $e) {
// Something bad happened - log the error.
watchdog('Apache Solr', 'Error constructing documents to index: <br /> !message', array(
'!message' => "Node ID: {$row->nid}<br />" . nl2br(strip_tags($e
->getMessage())),
), WATCHDOG_ERROR);
}
}
// Restore the user.
$user = $saved_user;
session_save_session(TRUE);
// Flatten $documents
$tmp = array();
apachesolr_flatten_documents_array($documents, $tmp);
$documents = $tmp;
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_set_last_updated($position['last_change']);
} 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;
}