Skip to content

Commit

Permalink
feat: choose indices to copy from/to (#98)
Browse files Browse the repository at this point in the history
* feat: choose indices to copy from/to

* chore: remove old template file
  • Loading branch information
Jaspar Gupta authored Feb 4, 2020
1 parent ce06657 commit 60eef08
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 82 deletions.
7 changes: 3 additions & 4 deletions src/Console/Mappings/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Illuminate\Console\Command as BaseCommand;
use Illuminate\Support\Facades\Config;

/**
* Class Command
Expand All @@ -19,13 +20,11 @@ abstract class Command extends BaseCommand

/**
* Command constructor.
*
* @param ClientBuilder $client
*/
public function __construct(ClientBuilder $client)
public function __construct()
{
parent::__construct();

$this->client = new Connection(config('database.connections.elasticsearch'));
$this->client = new Connection(Config::get('database.connections.elasticsearch'));
}
}
2 changes: 1 addition & 1 deletion src/Console/Mappings/IndexAliasCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function handle()
protected function getIndexName(): string
{
if (!$index = $this->argument('index')) {
$indices = collect($this->getIndices())
$indices = collect($this->indices())
->sortBy('index')
->pluck('index')
->toArray();
Expand Down
69 changes: 46 additions & 23 deletions src/Console/Mappings/IndexCopyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace DesignMyNight\Elasticsearch\Console\Mappings;

use DesignMyNight\Elasticsearch\Console\Mappings\Traits\GetsIndices;
use DesignMyNight\Elasticsearch\Support\ElasticsearchException;
use Elasticsearch\Common\Exceptions\ElasticsearchException as ElasticsearchExceptionInterface;
use Illuminate\Support\Facades\DB;

/**
* Class IndexCopyCommand
Expand All @@ -13,6 +13,8 @@
*/
class IndexCopyCommand extends Command
{
use GetsIndices;

/**
* @var string $description
*/
Expand All @@ -21,7 +23,7 @@ class IndexCopyCommand extends Command
/**
* @var string $signature
*/
protected $signature = 'index:copy {from} {to}';
protected $signature = 'index:copy {from?} {to?}';

/**
* Execute the console command.
Expand All @@ -30,40 +32,61 @@ class IndexCopyCommand extends Command
*/
public function handle()
{
['from' => $fromIndex, 'to' => $toIndex] = $this->arguments();

$connection = DB::connection('elasticsearch');
$from = $this->from();
$to = $this->to();

$body = [
'source' => [
'index' => $fromIndex,
],
'dest' => [
'index' => $toIndex,
],
'source' => ['index' => $from],
'dest' => ['index' => $to],
];

try {
$this->output->write("Copying {$fromIndex} to {$toIndex}...", true);
if ($this->confirm("Would you like to copy {$from} to {$to}?")) {
try {
$this->report(
$this->client->reindex(['body' => json_encode($body)])
);
} catch (ElasticsearchExceptionInterface $exception) {
$exception = new ElasticsearchException($exception);

$result = $connection->reindex([
'body' => json_encode($body),
]);
} catch (ElasticsearchExceptionInterface $e) {
$e = new ElasticsearchException($e);
$this->output->error((string) $exception);
}
}
}

$this->output->error((string) $e);
/**
* @return string
*/
protected function from(): string
{
if ($from = $this->argument('from')) {
return $from;
}

return $this->choice(
'Which index would you like to copy from?',
collect($this->indices())->pluck('index')->toArray()
);
}

return;
/**
* @return string
*/
protected function to(): string
{
if ($to = $this->argument('to')) {
return $to;
}

$this->reportResult($result);
return $this->choice(
'Which index would you like to copy to?',
collect($this->indices())->pluck('index')->toArray()
);
}

/**
* @param $result
* @param array $result
*/
private function reportResult($result): void
private function report(array $result): void
{
// report any failures
if ($result['failures']) {
Expand Down
15 changes: 2 additions & 13 deletions src/Console/Mappings/IndexListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
class IndexListCommand extends Command
{

use GetsIndices;

/** @var string $description */
Expand All @@ -22,16 +21,6 @@ class IndexListCommand extends Command
/** @var string $signature */
protected $signature = 'index:list {--A|alias= : Name of alias indexes belong to.}';

/**
* IndexListCommand constructor.
*
* @param ClientBuilder $client
*/
public function __construct(ClientBuilder $client)
{
parent::__construct($client);
}

/**
* Execute the console command.
*
Expand All @@ -53,7 +42,7 @@ public function handle()
return;
}

if ($indices = $this->getIndices()) {
if ($indices = $this->indices()) {
$this->table(array_keys($indices[0]), $indices);

return;
Expand All @@ -67,7 +56,7 @@ public function handle()
*
* @return array
*/
protected function getIndicesForAlias(string $alias = '*'):array
protected function getIndicesForAlias(string $alias = '*'): array
{
try {
$aliases = collect($this->client->cat()->aliases());
Expand Down
21 changes: 5 additions & 16 deletions src/Console/Mappings/IndexRemoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DesignMyNight\Elasticsearch\Console\Mappings;

use Elasticsearch\ClientBuilder;
use DesignMyNight\Elasticsearch\Console\Mappings\Traits\GetsIndices;

/**
* Class IndexRemoveCommand
Expand All @@ -11,34 +11,23 @@
*/
class IndexRemoveCommand extends Command
{
use GetsIndices;

/** @var string $description */
protected $description = 'Remove index from Elasticsearch';

/** @var string $signature */
protected $signature = 'index:remove {index? : Name of the index to remove.}';

/**
* IndexRemoveCommand constructor.
*
* @param ClientBuilder $client
*/
public function __construct(ClientBuilder $client)
{
parent::__construct($client);
}

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$index = $this->argument('index');

if (!$index) {
$indices = collect($this->client->cat()->indices())->sortBy('index')->pluck('index')->toArray();
if (!$index = $this->argument('index')) {
$indices = collect($this->indices())->pluck('index')->toArray();
$index = $this->choice('Which index would you like to delete?', $indices);
}

Expand All @@ -54,7 +43,7 @@ public function handle()
*
* @return bool
*/
protected function removeIndex(string $index):bool
protected function removeIndex(string $index): bool
{
$this->info("Removing index: {$index}");

Expand Down
11 changes: 0 additions & 11 deletions src/Console/Mappings/IndexSwapCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
class IndexSwapCommand extends Command
{

use UpdatesAlias;

/** @var string $description */
Expand All @@ -21,16 +20,6 @@ class IndexSwapCommand extends Command
/** @var string $signature */
protected $signature = 'index:swap {alias : Name of alias to be updated.} {index? : Name of index to be updated to.} {old-index? : Name of current index.} {--R|remove-old-index : Deletes the old index.}';

/**
* IndexSwapCommand constructor.
*
* @param ClientBuilder $client
*/
public function __construct(ClientBuilder $client)
{
parent::__construct($client);
}

/**
* Execute the console command.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Mappings/Traits/GetsIndices.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait GetsIndices
/**
* @return array
*/
protected function getIndices():array
protected function indices(): array
{
try {
return collect($this->client->cat()->indices())->sortBy('index')->toArray();
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Mappings/Traits/HasConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ trait HasConnection
/**
* @param $connection
*/
public function setConnection($connection):void
public function setConnection($connection): void
{
$this->connection = $connection;
}

/**
* @return Builder
*/
protected function getConnection():Builder
protected function getConnection(): Builder
{
return DB::connection()->table(config('laravel-elasticsearch.mappings_migration_table', 'mappings'));
}
Expand Down
12 changes: 6 additions & 6 deletions src/Console/Mappings/Traits/UpdatesAlias.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ trait UpdatesAlias
*
* @return string
*/
protected function getActiveIndex(string $alias):string
protected function getActiveIndex(string $alias): string
{
try {
$aliases = collect($this->client->cat()->aliases());
} catch (\Exception $exception) {
$this->error('Failed to retrieve the current active index.');
}

$aliases = $aliases->filter(function (array $item) use ($alias):bool {
$aliases = $aliases->filter(function (array $item) use ($alias): bool {
return str_contains($item['index'], $alias);
})->sortByDesc('index');

Expand All @@ -45,7 +45,7 @@ protected function getActiveIndex(string $alias):string
*
* @return string
*/
protected function getAlias(string $mapping):string
protected function getAlias(string $mapping): string
{
return preg_replace('/^\d{4}\_\d{2}\_\d{2}\_\d{6}\_(update_)?/', '', $mapping, 1);
}
Expand All @@ -55,15 +55,15 @@ protected function getAlias(string $mapping):string
*
* @return string
*/
protected function getIndex(string $alias):string
protected function getIndex(string $alias): string
{
try {
$indices = collect($this->client->cat()->indices());
} catch (\Exception $exception) {
$this->error('An error occurred attempting to retrieve indices.');
}

$relevant = $indices->filter(function (array $item) use ($alias):bool {
$relevant = $indices->filter(function (array $item) use ($alias): bool {
return str_contains($item['index'], $alias);
})->sortByDesc('index');

Expand All @@ -76,7 +76,7 @@ protected function getIndex(string $alias):string
* @param string|null $currentIndex
* @param bool $removeOldIndex
*/
protected function updateAlias(?string $index, string $alias = null, ?string $currentIndex = null, bool $removeOldIndex = false):void
protected function updateAlias(?string $index, string $alias = null, ?string $currentIndex = null, bool $removeOldIndex = false): void
{
$index = $index ?? $this->getIndex($alias);

Expand Down
5 changes: 0 additions & 5 deletions src/Console/Mappings/stubs/mapping.stub

This file was deleted.

17 changes: 17 additions & 0 deletions src/Database/Migrations/Migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php


namespace DesignMyNight\Elasticsearch\Database\Migrations;

use Illuminate\Support\Facades\Schema;

class Migration extends \Illuminate\Database\Migrations\Migration
{
/** @var \Illuminate\Database\Schema\Builder */
private $schema;

public function __construct()
{
$this->schema = Schema::connection('elasticsearch');
}
}

0 comments on commit 60eef08

Please sign in to comment.