Skip to content

Commit

Permalink
feat(command): Add reindex command to package
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Szymański committed Aug 27, 2019
1 parent 934f90a commit 394ec0b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
54 changes: 54 additions & 0 deletions src/Console/ReindexSearchCommand.php
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
]);
}
}
4 changes: 3 additions & 1 deletion src/ScoutElasticServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use ScoutElastic\Console\ElasticUpdateMappingCommand;

use ScoutElastic\Console\IndexConfiguratorMakeCommand;
use ScoutElastic\Console\ReindexSearchCommand;
use ScoutElastic\Console\SearchableModelMakeCommand;
use ScoutElastic\Console\SearchRuleMakeCommand;
use ScoutElastic\Console\AggregateRuleMakeCommand;
Expand Down Expand Up @@ -43,7 +44,8 @@ public function boot()
ElasticIndexUpdateCommand::class,
ElasticIndexDropCommand::class,
ElasticUpdateMappingCommand::class,
ElasticMigrateCommand::class
ElasticMigrateCommand::class,
ReindexSearchCommand::class
]);

require_once __DIR__.'/Macros.php';
Expand Down

0 comments on commit 394ec0b

Please sign in to comment.