3 namespace BookStack\Console\Commands;
5 use BookStack\Entities\Models\Entity;
6 use BookStack\Entities\Tools\SearchIndex;
7 use Illuminate\Console\Command;
8 use Illuminate\Support\Facades\DB;
10 class RegenerateSearch extends Command
13 * The name and signature of the console command.
17 protected $signature = 'bookstack:regenerate-search {--database= : The database connection to use.}';
20 * The console command description.
24 protected $description = 'Re-index all content for searching';
29 protected $searchIndex;
32 * Create a new command instance.
34 public function __construct(SearchIndex $searchIndex)
36 parent::__construct();
37 $this->searchIndex = $searchIndex;
41 * Execute the console command.
45 public function handle()
47 $connection = DB::getDefaultConnection();
48 if ($this->option('database') !== null) {
49 DB::setDefaultConnection($this->option('database'));
52 $this->searchIndex->indexAllEntities(function (Entity $model, int $processed, int $total): void {
53 $this->info('Indexed ' . class_basename($model) . ' entries (' . $processed . '/' . $total . ')');
56 DB::setDefaultConnection($connection);
57 $this->line('Search index regenerated!');
59 return static::SUCCESS;