|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cviebrock\LaravelElasticsearch\Console\Command; |
|
6
|
|
|
|
|
7
|
|
|
use Elasticsearch\Client; |
|
8
|
|
|
use Illuminate\Console\Command; |
|
9
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
|
10
|
|
|
use Throwable; |
|
11
|
|
|
|
|
12
|
|
|
final class IndexCreateOrUpdateMappingCommand extends Command |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $signature = 'laravel-elasticsearch:utils:index-create-or-update-mapping |
|
18
|
|
|
{index-name : The index name} |
|
19
|
|
|
{mapping-file-path : The absolute path where mapping file is located}'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Client |
|
23
|
|
|
*/ |
|
24
|
|
|
private $client; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Filesystem |
|
28
|
|
|
*/ |
|
29
|
|
|
private $filesystem; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct( |
|
32
|
|
|
Client $client, |
|
33
|
|
|
Filesystem $filesystem |
|
34
|
|
|
) { |
|
35
|
|
|
$this->client = $client; |
|
36
|
|
|
$this->filesystem = $filesystem; |
|
37
|
|
|
|
|
38
|
|
|
parent::__construct(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function handle(): int |
|
42
|
|
|
{ |
|
43
|
|
|
$indexName = $this->argument('index-name'); |
|
44
|
|
|
$mappingFilePath = $this->argument('mapping-file-path'); |
|
45
|
|
|
|
|
46
|
|
|
if (!$this->argumentsAreValid( |
|
47
|
|
|
$indexName, |
|
48
|
|
|
$mappingFilePath |
|
49
|
|
|
)) { |
|
50
|
|
|
return self::FAILURE; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if (!$this->client->indices()->exists([ |
|
54
|
|
|
'index' => $indexName, |
|
55
|
|
|
])) { |
|
56
|
|
|
$this->output->writeln( |
|
57
|
|
|
sprintf( |
|
58
|
|
|
'<error>Index %s doesn\'t exists and mapping cannot be created or updated.</error>', |
|
59
|
|
|
$indexName |
|
60
|
|
|
) |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
return self::FAILURE; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
try { |
|
67
|
|
|
$this->client->indices()->putMapping([ |
|
68
|
|
|
'index' => $indexName, |
|
69
|
|
|
'body' => json_decode( |
|
70
|
|
|
$mappingFilePath, |
|
71
|
|
|
true |
|
72
|
|
|
), |
|
73
|
|
|
]); |
|
74
|
|
|
} catch (Throwable $exception) { |
|
75
|
|
|
$this->output->writeln( |
|
76
|
|
|
sprintf( |
|
77
|
|
|
'<error>Error creating or updating mapping for index %s, given mapping file: %s - error message: %s.</error>', |
|
78
|
|
|
$indexName, |
|
79
|
|
|
$mappingFilePath, |
|
80
|
|
|
$exception->getMessage() |
|
81
|
|
|
) |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
return self::FAILURE; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->output->writeln( |
|
88
|
|
|
sprintf( |
|
89
|
|
|
'<info>Mapping created or updated for index %s using file %s.</info>', |
|
90
|
|
|
$indexName, |
|
91
|
|
|
$mappingFilePath |
|
92
|
|
|
) |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
return self::SUCCESS; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function argumentsAreValid($indexName, $mappingFilePath): bool |
|
99
|
|
|
{ |
|
100
|
|
|
if ($indexName === null || |
|
101
|
|
|
!is_string($indexName) || |
|
102
|
|
|
mb_strlen($indexName) === 0 |
|
103
|
|
|
) { |
|
104
|
|
|
$this->output->writeln( |
|
105
|
|
|
'<error>Argument index-name must be a non empty string.</error>' |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if ($mappingFilePath === null || |
|
112
|
|
|
!is_string($mappingFilePath) || |
|
113
|
|
|
mb_strlen($mappingFilePath) === 0 || |
|
114
|
|
|
!$this->filesystem->exists($mappingFilePath) |
|
115
|
|
|
) { |
|
116
|
|
|
$this->output->writeln( |
|
117
|
|
|
'<error>Argument mapping-file-path must exists on filesystem and must be a non empty string.</error>' |
|
118
|
|
|
); |
|
119
|
|
|
|
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return true; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|