]> BookStack Code Mirror - bookstack/blobdiff - app/Console/Commands/RegenerateSearch.php
Updated minimum php version from 7.3 to 7.4
[bookstack] / app / Console / Commands / RegenerateSearch.php
index 1757911a7ce1226063bf91098302d74b9552b722..20e3fc7983f8bd7ff472758d20f3693841864ec5 100644 (file)
@@ -2,8 +2,10 @@
 
 namespace BookStack\Console\Commands;
 
-use BookStack\Services\SearchService;
+use BookStack\Entities\Models\Entity;
+use BookStack\Entities\Tools\SearchIndex;
 use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
 
 class RegenerateSearch extends Command
 {
@@ -19,19 +21,20 @@ class RegenerateSearch extends Command
      *
      * @var string
      */
-    protected $description = 'Command description';
+    protected $description = 'Re-index all content for searching';
 
-    protected $searchService;
+    /**
+     * @var SearchIndex
+     */
+    protected $searchIndex;
 
     /**
      * Create a new command instance.
-     *
-     * @param SearchService $searchService
      */
-    public function __construct(SearchService $searchService)
+    public function __construct(SearchIndex $searchIndex)
     {
         parent::__construct();
-        $this->searchService = $searchService;
+        $this->searchIndex = $searchIndex;
     }
 
     /**
@@ -41,14 +44,18 @@ class RegenerateSearch extends Command
      */
     public function handle()
     {
-        $connection = \DB::getDefaultConnection();
+        $connection = DB::getDefaultConnection();
         if ($this->option('database') !== null) {
-            \DB::setDefaultConnection($this->option('database'));
-            $this->searchService->setConnection(\DB::connection($this->option('database')));
+            DB::setDefaultConnection($this->option('database'));
         }
 
-        $this->searchService->indexAllEntities();
-        \DB::setDefaultConnection($connection);
-        $this->comment('Search index regenerated');
+        $this->searchIndex->indexAllEntities(function (Entity $model, int $processed, int $total): void {
+            $this->info('Indexed ' . class_basename($model) . ' entries (' . $processed . '/' . $total . ')');
+        });
+
+        DB::setDefaultConnection($connection);
+        $this->line('Search index regenerated!');
+
+        return static::SUCCESS;
     }
 }