1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nimble\ElasticBundle\Command; |
4
|
|
|
|
5
|
|
|
use Nimble\ElasticBundle\Index\IndexManager; |
6
|
|
|
use Nimble\ElasticBundle\Populator\PopulatorManager; |
7
|
|
|
use Nimble\ElasticBundle\Type\Type; |
8
|
|
|
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
class PopulateCommand extends AbstractBaseCommand |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
|
|
protected function configure() |
19
|
|
|
{ |
20
|
|
|
$this |
21
|
|
|
->setName('elastic:populate') |
22
|
|
|
->setDescription('Populates indexes and types.') |
23
|
|
|
->addOption('index', 'i', InputOption::VALUE_OPTIONAL, 'Name of the index to populate.') |
24
|
|
|
->addOption('type', 't', InputOption::VALUE_OPTIONAL, 'Name of the type to populate.') |
25
|
|
|
->addOption('batch', 'b', InputOption::VALUE_OPTIONAL, 'Size of single batch.', 100) |
26
|
|
|
; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return IndexManager |
31
|
|
|
*/ |
32
|
|
|
protected function getIndexManager() |
33
|
|
|
{ |
34
|
|
|
return $this->getContainer()->get('nimble_elastic.index_manager'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return PopulatorManager |
39
|
|
|
*/ |
40
|
|
|
protected function getPopulatorManager() |
41
|
|
|
{ |
42
|
|
|
return $this->getContainer()->get('nimble_elastic.populator_manager'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Type $type |
47
|
|
|
* @param int $batchSize |
48
|
|
|
* @param OutputInterface $output |
49
|
|
|
*/ |
50
|
|
|
protected function populateType(Type $type, $batchSize, OutputInterface $output) |
51
|
|
|
{ |
52
|
|
|
$progress = $this->createProgressBar($output); |
53
|
|
|
|
54
|
|
|
if ($type->getIndex()->isAliased()) { |
55
|
|
|
$output->writeln(sprintf('Index is aliased - using elasticsearch type <info>%s.%s</info>.', |
56
|
|
|
$type->getIndex()->getName(), |
57
|
|
|
$type->getName() |
58
|
|
|
)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$output->writeln(sprintf('Populating type <info>%s.%s</info>.', |
62
|
|
|
$type->getIndex()->getId(), |
63
|
|
|
$type->getName() |
64
|
|
|
)); |
65
|
|
|
|
66
|
|
|
$count = $this->getPopulatorManager()->createPopulator($type)->populate($batchSize, $progress); |
67
|
|
|
|
68
|
|
|
if ($count === 0) { |
69
|
|
|
$output->writeln('<warning>No data found to populate.</warning>'); |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->writeSuccessMessage($output, sprintf('Successfully populated <info>%d</info> documents.', $count)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
80
|
|
|
{ |
81
|
|
|
$this->configureFormatter($output); |
82
|
|
|
|
83
|
|
|
$indexManager = $this->getIndexManager(); |
84
|
|
|
|
85
|
|
|
$indexId = $input->getOption('index'); |
86
|
|
|
$typeName = $input->getOption('type'); |
87
|
|
|
$batchSize = $input->getOption('batch'); |
88
|
|
|
|
89
|
|
|
/* TODO: Throw message if index/type not found or no fetcher for selected type defined. */ |
90
|
|
|
|
91
|
|
|
foreach ($indexManager->getIndexes() as $index) { |
92
|
|
|
if (null !== $indexId && $index->getId() !== $indexId) { |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
foreach ($index->getTypes() as $type) { |
97
|
|
|
if (null !== $typeName && $type->getName() !== $typeName) { |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->populateType($type, $batchSize, $output); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|