1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace BEdita\ElasticSearch\Command; |
5
|
|
|
|
6
|
|
|
use BEdita\Core\Search\SearchRegistry; |
7
|
|
|
use BEdita\ElasticSearch\Adapter\ElasticSearchAdapter; |
8
|
|
|
use Cake\Command\Command; |
9
|
|
|
use Cake\Console\Arguments; |
10
|
|
|
use Cake\Console\ConsoleIo; |
11
|
|
|
use Cake\Console\ConsoleOptionParser; |
12
|
|
|
use Cake\Core\Configure; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Utility command to update the configured ElasticSearch index. |
16
|
|
|
*/ |
17
|
|
|
class UpdateIndexCommand extends Command |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @inheritDoc |
21
|
|
|
*/ |
22
|
|
|
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser |
23
|
|
|
{ |
24
|
|
|
return parent::buildOptionParser($parser) |
25
|
|
|
->setDescription('Update ElasticSearch indices for configured adapters.') |
26
|
|
|
->addOption('create', [ |
27
|
|
|
'help' => 'Create missing indices, otherwise they will be ignored', |
28
|
|
|
'boolean' => true, |
29
|
|
|
]) |
30
|
|
|
->addOption('adapters', [ |
31
|
|
|
'help' => 'Update indices only for these adapters (comma-separated list).', |
32
|
|
|
'required' => false, |
33
|
|
|
]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritDoc} |
38
|
|
|
* |
39
|
|
|
* @throws \Exception Error loading adapter from registry |
40
|
|
|
*/ |
41
|
|
|
public function execute(Arguments $args, ConsoleIo $io): ?int |
42
|
|
|
{ |
43
|
|
|
$registry = new SearchRegistry(); |
44
|
|
|
$adapters = array_keys((array)Configure::read('Search.adapters')); |
45
|
|
|
if ($args->hasOption('adapters')) { |
46
|
|
|
$adapters = explode(',', (string)$args->getOption('adapters')); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
foreach ($adapters as $name) { |
50
|
|
|
/** @var array<string, mixed> $config */ |
51
|
|
|
$config = (array)Configure::read(sprintf('Search.adapters.%s', $name)); |
52
|
|
|
if (empty($config)) { |
53
|
|
|
$io->warning(sprintf('Missing configuration for adapter "%s", skipping index update', $name)); |
54
|
|
|
|
55
|
|
|
continue; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$adapter = $registry->load($name, $config); |
59
|
|
|
if (!$adapter instanceof ElasticSearchAdapter) { |
60
|
|
|
$io->warning(sprintf( |
61
|
|
|
'Adapter "%s" is not an instance of %s, skipping index update', |
62
|
|
|
$name, |
63
|
|
|
ElasticSearchAdapter::class, |
64
|
|
|
)); |
65
|
|
|
|
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$index = $adapter->getIndex(); |
70
|
|
|
if (!$index->indexExists()) { |
71
|
|
|
if (!$args->getOption('create')) { |
72
|
|
|
$io->warning(sprintf('Index "%s" for adapter "%s" does not exist', $index->getName(), $name)); |
73
|
|
|
|
74
|
|
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (!$index->create()) { |
78
|
|
|
$io->error(sprintf('Error creating missing index "%s" for adapter "%s"', $index->getName(), $name)); |
79
|
|
|
} else { |
80
|
|
|
$io->success(sprintf('Created missing index "%s" for adapter "%s"', $index->getName(), $name)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
continue; |
84
|
|
|
} |
85
|
|
|
if (!$index->updateProperties() || !$index->updateAnalysis()) { |
86
|
|
|
$io->error(sprintf('Error updating index "%s" for adapter "%s"', $index->getName(), $name)); |
87
|
|
|
|
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$io->success(sprintf('Updated index "%s" for adapter "%s"', $index->getName(), $name)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return static::CODE_SUCCESS; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|