1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchBundle\Command; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchBundle\Service\ImportService; |
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
20
|
|
|
|
21
|
|
|
class IndexImportCommand extends AbstractIndexServiceAwareCommand |
22
|
|
|
{ |
23
|
|
|
const NAME = 'ongr:es:index:import'; |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
parent::configure(); |
30
|
|
|
|
31
|
|
|
$this |
32
|
|
|
->setName(self::NAME) |
33
|
|
|
->setDescription('Imports data to elasticsearch index.') |
34
|
|
|
->addArgument( |
35
|
|
|
'filename', |
36
|
|
|
InputArgument::REQUIRED, |
37
|
|
|
'Select file to store output' |
38
|
|
|
) |
39
|
|
|
->addOption( |
40
|
|
|
'bulk-size', |
41
|
|
|
'b', |
42
|
|
|
InputOption::VALUE_REQUIRED, |
43
|
|
|
'Set bulk size for import', |
44
|
|
|
1000 |
45
|
|
|
) |
46
|
|
|
->addOption( |
47
|
|
|
'gzip', |
48
|
|
|
'z', |
49
|
|
|
InputOption::VALUE_NONE, |
50
|
|
|
'Import a gzip file' |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
58
|
|
|
{ |
59
|
|
|
$io = new SymfonyStyle($input, $output); |
60
|
|
|
$index = $this->getIndex($input->getOption(parent::INDEX_OPTION)); |
61
|
|
|
|
62
|
|
|
// Initialize options array |
63
|
|
|
$options = []; |
64
|
|
|
if ($input->getOption('gzip')) { |
65
|
|
|
$options['gzip'] = null; |
66
|
|
|
} |
67
|
|
|
$options['bulk-size'] = $input->getOption('bulk-size'); |
68
|
|
|
|
69
|
|
|
/** @var ImportService $importService */ |
70
|
|
|
$importService = $this->getContainer()->get(ImportService::class); |
71
|
|
|
$importService->importIndex( |
72
|
|
|
$index, |
73
|
|
|
$input->getArgument('filename'), |
74
|
|
|
$output, |
75
|
|
|
$options |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
$io->success('Data import completed!'); |
79
|
|
|
|
80
|
|
|
return 0; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.