1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Solr\Console\Command\Collection; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\ClientException; |
6
|
|
|
use GuzzleHttp\Exception\ConnectException; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument as IArg; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption as IOpt; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Create collection. |
14
|
|
|
*/ |
15
|
|
|
class Create extends Command |
16
|
|
|
{ |
17
|
|
|
protected function configure() |
18
|
|
|
{ |
19
|
|
|
$this->setName('collection:create') |
20
|
|
|
->setDescription('Create a collection'); |
21
|
|
|
|
22
|
|
|
$this->addArgument('name', IArg::REQUIRED, 'Collection name') |
23
|
|
|
->addOption('num-shards', null, IOpt::VALUE_OPTIONAL, 'The number of shards to be create as part of the collection', 1) |
24
|
|
|
->addOption('replication-factor', null, IOpt::VALUE_OPTIONAL, 'The number of replicas to be created for each shard', 1) |
25
|
|
|
->addOption('max-shards-per-node', null, IOpt::VALUE_OPTIONAL, 'Sets a limit on the number of replicas', 1) |
26
|
|
|
->addOption('config-name', null, IOpt::VALUE_OPTIONAL, 'Name of configurations to use for this collection'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Initialize command with default config name, when not defined. |
31
|
|
|
* |
32
|
|
|
* @param Symfony\Component\Console\Input\InputInterface $input |
33
|
|
|
* @param Symfony\Component\Console\Output\OutputInterface $output |
34
|
|
|
*/ |
35
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
36
|
|
|
{ |
37
|
|
|
if (is_null($input->getOption('config-name'))) { |
38
|
|
|
$input->setOption('config-name', $input->getArgument('name')); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
parent::initialize($input, $output); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Execute create command. Creates a collection in Solr. |
46
|
|
|
* |
47
|
|
|
* @param Symfony\Component\Console\Input\InputInterface $input |
48
|
|
|
* @param Symfony\Component\Console\Output\OutpuInterface $output |
49
|
|
|
* |
50
|
|
|
* @return int |
51
|
|
|
*/ |
52
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
|
|
$url = sprintf('admin/collections?action=CREATE&name=%s&numShards=%d&replicationFactor=%d&maxShardsPerNode=%d&collection.configName=%s&wt=json', |
56
|
|
|
$input->getArgument('name'), |
57
|
|
|
$input->getOption('num-shards'), |
58
|
|
|
$input->getOption('replication-factor'), |
59
|
|
|
$input->getOption('max-shards-per-node'), |
60
|
|
|
$input->getOption('config-name') |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$result = $this->client |
64
|
|
|
->get($url) |
65
|
|
|
->json(); |
66
|
|
|
|
67
|
|
|
if (isset($result['success'])) { |
68
|
|
|
$output->writeln('<fg=green>The collection was created</fg=green>'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return 0; |
72
|
|
|
} catch (ClientException $e) { |
73
|
|
|
$response = $e->getResponse()->json(); |
74
|
|
|
|
75
|
|
|
if (isset($response['error']['msg'])) { |
76
|
|
|
$output->writeln("<fg=red>{$response['error']['msg']}</fg=red>"); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return 1; |
80
|
|
|
} catch (ConnectException $e) { |
81
|
|
|
$output->writeln('<fg=red>The connection failed for host</fg=red>'); |
82
|
|
|
|
83
|
|
|
return 1; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|