mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-11-26 10:13:38 +08:00
Updated regen-search command to show some level of progress
This commit is contained in:
parent
9f32613982
commit
820be162f5
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace BookStack\Console\Commands;
|
||||
|
||||
use BookStack\Entities\Models\Entity;
|
||||
use BookStack\Entities\Tools\SearchIndex;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
@ -22,6 +23,9 @@ class RegenerateSearch extends Command
|
|||
*/
|
||||
protected $description = 'Re-index all content for searching';
|
||||
|
||||
/**
|
||||
* @var SearchIndex
|
||||
*/
|
||||
protected $searchIndex;
|
||||
|
||||
/**
|
||||
|
@ -45,8 +49,13 @@ class RegenerateSearch extends Command
|
|||
DB::setDefaultConnection($this->option('database'));
|
||||
}
|
||||
|
||||
$this->searchIndex->indexAllEntities();
|
||||
$this->searchIndex->indexAllEntities(function (Entity $model, int $processed, int $total) {
|
||||
$this->info('Indexed ' . class_basename($model) . ' entries (' . $processed . '/' . $total . ')');
|
||||
});
|
||||
|
||||
DB::setDefaultConnection($connection);
|
||||
$this->comment('Search index regenerated');
|
||||
$this->line('Search index regenerated!');
|
||||
|
||||
return static::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,19 +51,36 @@ class SearchIndex
|
|||
|
||||
/**
|
||||
* Delete and re-index the terms for all entities in the system.
|
||||
* Can take a callback which is used for reporting progress.
|
||||
* Callback receives three arguments:
|
||||
* - An instance of the model being processed
|
||||
* - The number that have been processed so far.
|
||||
* - The total number of that model to be processed.
|
||||
*
|
||||
* @param callable(Entity, int, int)|null $progressCallback
|
||||
*/
|
||||
public function indexAllEntities()
|
||||
public function indexAllEntities(?callable $progressCallback = null)
|
||||
{
|
||||
SearchTerm::query()->truncate();
|
||||
|
||||
foreach ($this->entityProvider->all() as $entityModel) {
|
||||
$selectFields = ['id', 'name', $entityModel->textField];
|
||||
$total = $entityModel->newQuery()->withTrashed()->count();
|
||||
$chunkSize = 250;
|
||||
$processed = 0;
|
||||
|
||||
$chunkCallback = function (Collection $entities) use ($progressCallback, &$processed, $total, $chunkSize, $entityModel) {
|
||||
$this->indexEntities($entities->all());
|
||||
$processed = min($processed + $chunkSize, $total);
|
||||
|
||||
if (is_callable($progressCallback)) {
|
||||
$progressCallback($entityModel, $processed, $total);
|
||||
}
|
||||
};
|
||||
|
||||
$entityModel->newQuery()
|
||||
->withTrashed()
|
||||
->select($selectFields)
|
||||
->chunk(1000, function (Collection $entities) {
|
||||
$this->indexEntities($entities->all());
|
||||
});
|
||||
->chunk($chunkSize, $chunkCallback);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user