Skip to content

Commit

Permalink
Add ability to specify index name
Browse files Browse the repository at this point in the history
  • Loading branch information
odiaseo committed Dec 24, 2017
1 parent 244a5a3 commit fc0fda8
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Console/ElasticIndexCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function handle()
$payload = (new IndexPayload($configurator))
->setIfNotEmpty('body.settings', $configurator->getSettings())
->setIfNotEmpty('body.mappings._default_', $configurator->getDefaultMapping())
->setIfNotEmpty('body.aliases', $configurator->getAliases())
->get();

$this->client->indices()->create($payload);
Expand Down
52 changes: 52 additions & 0 deletions src/Console/ElasticModelImportCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace SynergyScoutElastic\Console;

use Illuminate\Contracts\Events\Dispatcher;
use Laravel\Scout\Events\ModelsImported;
use SynergyScoutElastic\Console\Features\RequiresModelArgument;

class ElasticModelImportCommand extends BaseCommand
{
use RequiresModelArgument;

protected $name = 'search:model-import';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Import the given model into the search index';

/**
* Execute the console command.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
*
* @return void
*/
public function handle(Dispatcher $events)
{
if (!$model = $this->getModel()) {
return;
}

$start = time();
$class = get_class($model);
$this->info('Importing [' . $class . '] into index [.' . $model->getIndexConfigurator()->getName() . ']');

$events->listen(ModelsImported::class, function ($event) use ($class) {
$key = $event->models->last()->getKey();

$this->line('<comment>Imported [' . $class . '] models up to ID:</comment> ' . $key);
});

$model::makeAllSearchable();

$events->forget(ModelsImported::class);
$end = time();
$duration = round(($end - $start) / 60, 1);
$this->info(sprintf('All [%s] records have been imported in %s mins.', $class, $duration));
}
}
1 change: 0 additions & 1 deletion src/Console/ElasticUpdateMappingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function handle()
}

$configurator = $model->getIndexConfigurator();

$mapping = array_merge_recursive($configurator->getDefaultMapping(), $model->getMapping());

if (empty($mapping)) {
Expand Down
18 changes: 14 additions & 4 deletions src/Console/Features/RequiresIndexConfiguratorArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

namespace SynergyScoutElastic\Console\Features;

use SynergyScoutElastic\IndexConfigurator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use SynergyScoutElastic\IndexConfigurator;

trait RequiresIndexConfiguratorArgument
{
/**
*
* @return IndexConfigurator
*/
protected function getIndexConfigurator()
{
$name = (string)$this->option('name');
$configuratorClass = trim($this->argument('index-configurator'));

$configuratorInstance = new $configuratorClass;
$name = trim($name);
$configuratorInstance = new $configuratorClass($name);

if (!($configuratorInstance instanceof IndexConfigurator)) {
$this->error(sprintf(
Expand All @@ -26,7 +29,7 @@ protected function getIndexConfigurator()
return null;
}

return (new $configuratorClass);
return $configuratorInstance;
}

protected function getArguments()
Expand All @@ -35,4 +38,11 @@ protected function getArguments()
['index-configurator', InputArgument::REQUIRED, 'The index configurator class'],
];
}

protected function getOptions()
{
return [
['name', null, InputOption::VALUE_OPTIONAL, 'Name of index to create'],
];
}
}
11 changes: 11 additions & 0 deletions src/Console/Features/RequiresModelArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use SynergyScoutElastic\Models\Searchable;
use SynergyScoutElastic\Models\SearchableInterface;

Expand All @@ -30,6 +31,9 @@ protected function getModel()
return null;
}

$name = (string)$this->option('name');
$modelInstance->getIndexConfigurator()->setName($name);

return $modelInstance;
}

Expand All @@ -39,4 +43,11 @@ protected function getArguments()
['model', InputArgument::REQUIRED, 'The model class'],
];
}

protected function getOptions()
{
return [
['name', null, InputOption::VALUE_REQUIRED, 'Name of elastic search index'],
];
}
}
35 changes: 35 additions & 0 deletions src/IndexConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,57 @@ abstract class IndexConfigurator

protected $defaultMapping = [];

protected $aliases = [];

/**
* @return string
*/
public function getName()
{
if (isset($this->name)) {
return $this->name;
}

return $this->getDefaultIndexName();
}

/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}

/**
* @return string
*/
protected function getDefaultIndexName()
{
return Str::snake(str_replace('IndexConfigurator', '', class_basename($this)));
}

/**
* @return array
*/
public function getSettings()
{
return $this->settings;
}

/**
* @return array
*/
public function getDefaultMapping()
{
return $this->defaultMapping;
}

/**
* @return array
*/
public function getAliases(): array
{
return $this->aliases;
}
}
12 changes: 12 additions & 0 deletions src/Models/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public static function searchRaw($query)
*/
public function getIndexConfigurator(): IndexConfigurator
{

if ($this->indexConfigurator instanceof IndexConfigurator) {
return $this->indexConfigurator;
}

static $indexConfigurator;

if (!$indexConfigurator) {
Expand All @@ -74,6 +79,13 @@ public function getIndexConfigurator(): IndexConfigurator
return $indexConfigurator;
}

public function setIndexConfigurator(IndexConfigurator $configurator)
{
$this->indexConfigurator = $configurator;

return $this;
}

/**
* @return array
*/
Expand Down
7 changes: 7 additions & 0 deletions src/Models/SearchableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ public static function searchRaw($query);
*/
public function getIndexConfigurator(): IndexConfigurator;

/**
* @param IndexConfigurator $configurator
*
* @return mixed
*/
public function setIndexConfigurator(IndexConfigurator $configurator);

/**
* @return array
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Providers/ScoutElasticServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use SynergyScoutElastic\Console\ElasticIndexCreateCommand;
use SynergyScoutElastic\Console\ElasticIndexDropCommand;
use SynergyScoutElastic\Console\ElasticIndexUpdateCommand;
use SynergyScoutElastic\Console\ElasticModelImportCommand;
use SynergyScoutElastic\Console\ElasticUpdateMappingCommand;
use SynergyScoutElastic\Console\IndexConfiguratorMakeCommand;
use SynergyScoutElastic\Console\SearchableModelMakeCommand;
Expand Down Expand Up @@ -44,6 +45,7 @@ public function boot()
ElasticIndexUpdateCommand::class,
ElasticIndexDropCommand::class,
ElasticUpdateMappingCommand::class,
ElasticModelImportCommand::class,
]);

$this->app->make(EngineManager::class)
Expand Down
1 change: 1 addition & 0 deletions tests/ConsoleCommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private function getMockInputs(array $arguments)
{
$input = $this->prophesize(InputInterface::class);
$input->getArguments()->willReturn($arguments);
$input->getOption('name')->willReturn('test_index');

foreach ($arguments as $key => $value) {
$input->getArgument($key)->willReturn($value);
Expand Down

0 comments on commit fc0fda8

Please sign in to comment.