From db1184d200d2d66cad09c460e775b9f7aee297a3 Mon Sep 17 00:00:00 2001 From: Dan Krieger Date: Sat, 20 Jun 2020 11:12:05 -0700 Subject: [PATCH] Optional param for chunk size on scout:import Allow setting an optional chunk size when running a batch import. --- src/Console/ImportCommand.php | 7 +++++-- src/Searchable.php | 5 +++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Console/ImportCommand.php b/src/Console/ImportCommand.php index 0493af6a..ed557a4d 100644 --- a/src/Console/ImportCommand.php +++ b/src/Console/ImportCommand.php @@ -13,7 +13,9 @@ class ImportCommand extends Command * * @var string */ - protected $signature = 'scout:import {model}'; + protected $signature = 'scout:import + {model : Class name of model to bulk import} + {--c|chunk-size= : Number of items to bulk process at a time (Defaults to config value: `scout.chunk.searchable`)}'; /** * The console command description. @@ -31,6 +33,7 @@ class ImportCommand extends Command public function handle(Dispatcher $events) { $class = $this->argument('model'); + $chunkSize = $this->option('chunk-size'); $model = new $class; @@ -40,7 +43,7 @@ public function handle(Dispatcher $events) $this->line('Imported ['.$class.'] models up to ID: '.$key); }); - $model::makeAllSearchable(); + $model::makeAllSearchable($chunkSize); $events->forget(ModelsImported::class); diff --git a/src/Searchable.php b/src/Searchable.php index 83c3c66b..1ba4aba4 100644 --- a/src/Searchable.php +++ b/src/Searchable.php @@ -113,9 +113,10 @@ public static function search($query = '', $callback = null) /** * Make all instances of the model searchable. * + * @param int $chunkSize * @return void */ - public static function makeAllSearchable() + public static function makeAllSearchable($chunkSize = null) { $self = new static; @@ -126,7 +127,7 @@ public static function makeAllSearchable() $query->withTrashed(); }) ->orderBy($self->getKeyName()) - ->searchable(); + ->searchable($chunkSize); } /**