]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/RegenerateVectorsCommand.php
Vectors: Added command to regenerate for all
[bookstack] / app / Console / Commands / RegenerateVectorsCommand.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Entities\EntityProvider;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Search\Vectors\SearchVector;
8 use BookStack\Search\Vectors\StoreEntityVectorsJob;
9 use Illuminate\Console\Command;
10
11 class RegenerateVectorsCommand extends Command
12 {
13     /**
14      * The name and signature of the console command.
15      *
16      * @var string
17      */
18     protected $signature = 'bookstack:regenerate-vectors';
19
20     /**
21      * The console command description.
22      *
23      * @var string
24      */
25     protected $description = 'Re-index vectors for all content in the system';
26
27     /**
28      * Execute the console command.
29      */
30     public function handle(EntityProvider $entityProvider)
31     {
32         // TODO - Add confirmation before run regarding deletion/time/effort/api-cost etc...
33         SearchVector::query()->delete();
34
35         $types = $entityProvider->all();
36         foreach ($types as $type => $typeInstance) {
37             $this->info("Creating jobs to store vectors for {$type} data...");
38             /** @var Entity[] $entities  */
39             $typeInstance->newQuery()->chunkById(100, function ($entities) {
40                 foreach ($entities as $entity) {
41                     dispatch(new StoreEntityVectorsJob($entity));
42                 }
43             });
44         }
45     }
46 }