forked from rankarpan/scout-elasticsearch-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(command): Add reindex command to package
- Loading branch information
Roman Szymański
committed
Aug 27, 2019
1 parent
934f90a
commit 394ec0b
Showing
2 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace ScoutElastic\Console; | ||
|
||
use Illuminate\Console\Command; | ||
use ScoutElastic\Facades\ElasticClient; | ||
|
||
class ReindexSearchCommand extends Command | ||
{ | ||
protected $name = 'scout:reindex'; | ||
|
||
protected $description = 'Reindex Elasticsearch'; | ||
|
||
public function handle() | ||
{ | ||
foreach (config('scout_elastic.models', []) as $searchableModel) { | ||
|
||
if (!in_array(\ScoutElastic\Searchable::class, class_uses($searchableModel))) { | ||
continue; | ||
} | ||
|
||
$this->newElasticHandle($searchableModel); | ||
} | ||
} | ||
|
||
private function newElasticHandle($searchableModel) | ||
{ | ||
$searchableModelObject = new $searchableModel; | ||
|
||
$this->info("\nIndexing " . $searchableModel); | ||
|
||
$indexConfiguratorClass = $searchableModelObject->indexConfigurator; | ||
|
||
$indexConfigurator = new $indexConfiguratorClass(); | ||
|
||
if (ElasticClient::indices()->exists(['index' => $indexConfigurator->getName()])) { | ||
|
||
$this->call('elastic:drop-index', [ | ||
'index-configurator' => $searchableModelObject->indexConfigurator | ||
]); | ||
} | ||
|
||
$this->call('elastic:create-index', [ | ||
'index-configurator' => $searchableModelObject->indexConfigurator | ||
]); | ||
$this->call('elastic:update-mapping', [ | ||
'model' => $searchableModel | ||
]); | ||
|
||
$this->call('scout:import', [ | ||
'model' => $searchableModel | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters