API Solr_Reindex uses configured SearchUpdater instead of always doing a direct write

Emit errors on Solr_Reindex if verbose=1
This commit is contained in:
Damian Mooyman 2015-05-06 17:01:41 +12:00
parent 66a2353d33
commit 625d282df2
3 changed files with 83 additions and 22 deletions

View File

@ -1,3 +1,3 @@
DataObject:
extensions:
- 'SearchUpdater_DeleteHandler'
- 'SearchUpdater_ObjectHandler'

View File

@ -90,9 +90,19 @@ class SearchUpdater extends Object {
$statefulids = array(array('id' => $id, 'state' => $state));
// Is this the first table for this particular object? Then add an item to $writes
if (!isset($writes[$key])) $writes[$key] = array('base' => $base, 'class' => $class, 'id' => $id, 'statefulids' => $statefulids, 'fields' => array());
if (!isset($writes[$key])) {
$writes[$key] = array(
'base' => $base,
'class' => $class,
'id' => $id,
'statefulids' => $statefulids,
'fields' => array()
);
}
// Otherwise update the class label if it's more specific than the currently recorded one
else if (is_subclass_of($class, $writes[$key]['class'])) $writes[$key]['class'] = $class;
else if (is_subclass_of($class, $writes[$key]['class'])) {
$writes[$key]['class'] = $class;
}
// Update the fields
foreach ($fields as $field => $value) {
@ -104,8 +114,17 @@ class SearchUpdater extends Object {
SearchVariant::call('extractManipulationWriteState', $writes);
// Then for each write, figure out what objects need updating
// Submit all of these writes to the search processor
static::process_writes($writes);
}
/**
* Send updates to the current search processor for execution
*
* @param array $writes
*/
public static function process_writes($writes) {
foreach ($writes as $write) {
// For every index
foreach (FullTextSearch::get_indexes() as $index => $instance) {
@ -117,7 +136,9 @@ class SearchUpdater extends Object {
// Then add then then to the global list to deal with later
foreach ($dirtyids as $dirtyclass => $ids) {
if ($ids) {
if (!self::$processor) self::$processor = Injector::inst()->create('SearchUpdateProcessor');
if (!self::$processor) {
self::$processor = Injector::inst()->create('SearchUpdateProcessor');
}
self::$processor->addDirtyIDs($dirtyclass, $ids, $index);
}
}
@ -125,7 +146,7 @@ class SearchUpdater extends Object {
}
}
// Finally, if we do have some work to do register the shutdown function to actually do the work
// If we do have some work to do register the shutdown function to actually do the work
// Don't do it if we're testing - there's no database connection outside the test methods, so we'd
// just get errors
@ -173,7 +194,7 @@ class SearchUpdater_BindManipulationCaptureFilter implements RequestFilter {
* indexed. This causes the object to be marked for deletion from the index.
*/
class SearchUpdater_DeleteHandler extends DataExtension {
class SearchUpdater_ObjectHandler extends DataExtension {
public function onAfterDelete() {
// Calling delete() on empty objects does nothing
@ -191,4 +212,36 @@ class SearchUpdater_DeleteHandler extends DataExtension {
SearchUpdater::handle_manipulation($manipulation);
}
/**
* Forces this object to trigger a re-index in the current state
*/
public function triggerReindex() {
if (!$this->owner->ID) {
return;
}
$id = $this->owner->ID;
$class = $this->owner->ClassName;
$state = SearchVariant::current_state();
$base = ClassInfo::baseDataClass($class);
$key = "$id:$base:".serialize($state);
$statefulids = array(array(
'id' => $id,
'state' => $state
));
$writes = array(
$key => array(
'base' => $base,
'class' => $class,
'id' => $id,
'statefulids' => $statefulids,
'fields' => array()
)
);
SearchUpdater::process_writes($writes);
}
}

View File

@ -200,7 +200,14 @@ class Solr_Configure extends BuildTask {
class Solr_Reindex extends BuildTask {
static $recordsPerRequest = 200;
/**
* Number of records to load and index per request
*
* @var int
* @config
*/
private static $recordsPerRequest = 200;
public function run($request) {
increase_time_limit_to();
@ -262,7 +269,7 @@ class Solr_Reindex extends BuildTask {
if($verbose) {
echo "\n Running '$cmd'\n";
$cmd .= " verbose=1";
$cmd .= " verbose=1 2>&1";
}
$res = $verbose ? passthru($cmd) : `$cmd`;
@ -304,7 +311,8 @@ class Solr_Reindex extends BuildTask {
foreach ($items as $item) {
if($verbose) echo $item->ID . ' ';
$index->add($item);
// See SearchUpdater_ObjectHandler::triggerReindex
$item->triggerReindex();
$item->destroy();
}