1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nimble\ElasticBundle\Command; |
4
|
|
|
|
5
|
|
|
use Nimble\ElasticBundle\Exception\TypeNotFoundException; |
6
|
|
|
use Nimble\ElasticBundle\Index\IndexManager; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
class ResetCommand extends AbstractBaseCommand |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
|
|
protected function configure() |
17
|
|
|
{ |
18
|
|
|
$this |
19
|
|
|
->setName('elastic:reset') |
20
|
|
|
->setDescription('Resets and index or all indexes.') |
21
|
|
|
->addOption('index', 'i', InputOption::VALUE_OPTIONAL, 'Name of the index to reset. All are reset if null.') |
22
|
|
|
->addOption('type', 't', InputOption::VALUE_OPTIONAL, 'Name of the type to reset.') |
23
|
|
|
; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return IndexManager |
28
|
|
|
*/ |
29
|
|
|
protected function getIndexManager() |
30
|
|
|
{ |
31
|
|
|
return $this->getContainer()->get('nimble_elastic.index_manager'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param array $indexIds |
36
|
|
|
* @param OutputInterface $output |
37
|
|
|
*/ |
38
|
|
|
protected function resetIndexes(array $indexIds, OutputInterface $output) |
39
|
|
|
{ |
40
|
|
|
foreach ($indexIds as $indexId) { |
41
|
|
|
|
42
|
|
|
$index = $this->getIndexManager()->getIndex($indexId); |
43
|
|
|
|
44
|
|
|
if ($index->isAliased()) { |
45
|
|
|
$output->writeln(sprintf('Index is aliased - using elasticsearch index <info>%s</info>.', |
46
|
|
|
$index->getName() |
47
|
|
|
)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->writeTaskStart($output, sprintf('Resetting index <info>%s</info>', $indexId)); |
51
|
|
|
|
52
|
|
|
$index->reset(); |
53
|
|
|
|
54
|
|
|
$this->writeTaskSuccess($output); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array $indexIds |
60
|
|
|
* @param string $typeName |
61
|
|
|
* @param OutputInterface $output |
62
|
|
|
*/ |
63
|
|
|
protected function resetType(array $indexIds, $typeName, OutputInterface $output) |
64
|
|
|
{ |
65
|
|
|
$typeFound = false; |
66
|
|
|
|
67
|
|
|
foreach ($indexIds as $indexId) { |
68
|
|
|
$index = $this->getIndexManager()->getIndex($indexId); |
69
|
|
|
|
70
|
|
|
if (!$index->hasType($typeName)) { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($index->isAliased()) { |
75
|
|
|
$output->writeln(sprintf('Index is aliased - using elasticsearch type <info>%s.%s</info>.', |
76
|
|
|
$index->getName(), |
77
|
|
|
$typeName |
78
|
|
|
)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->writeTaskStart($output, sprintf('Resetting type <info>%s.%s</info> ... ', $indexId, $typeName)); |
82
|
|
|
|
83
|
|
|
$index->getType($typeName)->reset(); |
84
|
|
|
$typeFound = true; |
85
|
|
|
|
86
|
|
|
$this->writeTaskSuccess($output); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (!$typeFound) { |
90
|
|
|
throw new TypeNotFoundException($typeName); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
98
|
|
|
{ |
99
|
|
|
$this->configureFormatter($output); |
100
|
|
|
|
101
|
|
|
$indexManager = $this->getIndexManager(); |
102
|
|
|
|
103
|
|
|
$indexId = $input->getOption('index'); |
104
|
|
|
$typeName = $input->getOption('type'); |
105
|
|
|
|
106
|
|
|
if (null !== $indexId) { |
107
|
|
|
$indexIds = [$indexId]; |
108
|
|
|
} else { |
109
|
|
|
$indexIds = $indexManager->getIndexIds(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (empty($indexIds)) { |
113
|
|
|
throw new \RuntimeException('No indexes found.'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (null !== $typeName) { |
117
|
|
|
$this->resetType($indexIds, $typeName, $output); |
118
|
|
|
} else { |
119
|
|
|
$this->resetIndexes($indexIds, $output); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->writeSuccessMessage($output, 'Reset finished successfully.'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|