Skip to content

Commit be669e6

Browse files
committed
New command
- Type to new index `spameri:elastic:move-type` - Moves documents from type to new index - Use when you need to split old index with multiple types New command - Dump index `spameri:elastic:dump-index` dumps all data from index to specified file - Handy for moving documents to another index - Or for backing purposes Added type support to ElasticSearch services for working with old indexes which has multiple types. Search returns ResultSearch object. New Indices services - Close, Open for clean in objects closing and opening indexes - Create to create empty index with no settings - Get to get all settings and mappings for index - GetMapping for getting mapping for specific index or index and type - PutMapping for putting mapping for specific index or index and type. And you can specify dynamic option of index Please bare in mind that types will be deprecated in ES and are added here only to provide backward compatibility.
1 parent 34185ec commit be669e6

26 files changed

Lines changed: 670 additions & 60 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"kdyby/console": "^2.7.1",
3535
"kdyby/datetime-provider": "v1.0.0",
3636
"kdyby/monolog": "^1.3.2",
37-
"tracy/tracy": "^2.5.3"
37+
"tracy/tracy": "^2.6.0"
3838
},
3939
"require-dev": {
4040
"spameri/coding-standard": "dev-master",

src/Commands/DumpIndex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(
2222

2323

2424
/**
25-
* @example spameri:elastic:move-type oldIndex productType newIndex newIndexAlias -c
25+
* @example spameri:elastic:dump-index index elasticDump.dump
2626
*/
2727
protected function configure() : void
2828
{

src/Commands/RestoreIndex.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\Elastic\Commands;
4+
5+
6+
class RestoreIndex extends \Symfony\Component\Console\Command\Command
7+
{
8+
9+
/**
10+
* @var \Spameri\Elastic\Model\RestoreIndex
11+
*/
12+
private $restoreIndex;
13+
14+
15+
public function __construct(
16+
\Spameri\Elastic\Model\RestoreIndex $migrate
17+
)
18+
{
19+
parent::__construct(NULL);
20+
$this->restoreIndex = $migrate;
21+
}
22+
23+
24+
/**
25+
* @example spameri:elastic:restore-index
26+
*/
27+
protected function configure() : void
28+
{
29+
$this
30+
->setName('spameri:elastic:restore-index')
31+
->setDescription('Restores data from provided dump file.')
32+
->addArgument('filename', \Symfony\Component\Console\Input\InputArgument::REQUIRED)
33+
->addArgument(
34+
'step',
35+
\Symfony\Component\Console\Input\InputArgument::OPTIONAL,
36+
'Number of documents per one bulk index',
37+
500
38+
)
39+
;
40+
}
41+
42+
43+
protected function execute(
44+
\Symfony\Component\Console\Input\InputInterface $input
45+
, \Symfony\Component\Console\Output\OutputInterface $output
46+
)
47+
{
48+
$output->writeln('Starting');
49+
50+
$index = $input->getArgument('index');
51+
$step = (int) $input->getArgument('step');
52+
53+
$this->restoreIndex->setOutput($output);
54+
$this->restoreIndex->execute($index, $step);
55+
56+
$output->writeln('Done');
57+
}
58+
59+
}

src/Config/Elastic.neon

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ services:
2626
search:
2727
class: Spameri\Elastic\Model\Search
2828

29+
scroll:
30+
class: Spameri\Elastic\Model\Scroll
31+
2932
delete:
3033
class: Spameri\Elastic\Model\Delete
3134

@@ -57,7 +60,7 @@ services:
5760
class: Spameri\Elastic\Model\Mapping
5861

5962
userProvider:
60-
class: Spameri\Elastic\Model\UserProvider
63+
class: Spameri\Elastic\Model\NetteUserProvider
6164

6265
neonSettingsProvider:
6366
class: Spameri\Elastic\Settings\NeonSettingsProvider(%host%, %port%)
@@ -86,41 +89,69 @@ services:
8689
migrate:
8790
class: Spameri\Elastic\Model\TypeToNewIndex\Migrate
8891

92+
modelDumpIndex:
93+
class: \Spameri\Elastic\Model\DumpIndex
94+
95+
modelRestoreIndex:
96+
class: \Spameri\Elastic\Model\RestoreIndex
97+
8998
# ###
9099
# Commands
91100
# ###
92101

93102
createIndex:
94103
class: Spameri\Elastic\Commands\CreateIndex(%entities%)
95-
tags: [kdyby.console.command]
104+
tags:
105+
- kdyby.console.command
106+
- console.command
96107

97108
updateMapping:
98109
class: Spameri\Elastic\Commands\UpdateMapping(%entities%)
99-
tags: [kdyby.console.command]
110+
tags:
111+
- kdyby.console.command
112+
- console.command
100113

101114
deleteIndex:
102115
class: Spameri\Elastic\Commands\DeleteIndex
103-
tags: [kdyby.console.command]
116+
tags:
117+
- kdyby.console.command
118+
- console.command
104119

105120
addAlias:
106121
class: Spameri\Elastic\Commands\AddAlias
107-
tags: [kdyby.console.command]
122+
tags:
123+
- kdyby.console.command
124+
- console.command
108125

109126
removeAlias:
110127
class: Spameri\Elastic\Commands\RemoveAlias
111-
tags: [kdyby.console.command]
128+
tags:
129+
- kdyby.console.command
130+
- console.command
112131

113132
validateMappingCommand:
114133
class: Spameri\Elastic\Commands\ValidateMapping
115-
tags: [kdyby.console.command]
134+
tags:
135+
- kdyby.console.command
136+
- console.command
116137

117138
typeToNewIndex:
118139
class: Spameri\Elastic\Commands\TypeToNewIndex
119-
tags: [kdyby.console.command]
140+
tags:
141+
- kdyby.console.command
142+
- console.command
120143

121144
dumpIndex:
122145
class: Spameri\Elastic\Commands\DumpIndex
123-
tags: [kdyby.console.command]
146+
tags:
147+
- kdyby.console.command
148+
- console.command
149+
150+
restoreIndex:
151+
class: Spameri\Elastic\Commands\RestoreIndex
152+
tags:
153+
- kdyby.console.command
154+
- console.command
124155

125156
# ###
126157
# Spameri/ElasticQuery
@@ -138,8 +169,8 @@ services:
138169
setup:
139170
- setLogger(@elasticSearch.elasticPanelLogger)
140171

141-
constantProvider:
142-
class: Kdyby\DateTimeProvider\Provider\ConstantProvider(@elasticSearch.dateTime)
172+
dateTimeProvider:
173+
class: Spameri\Elastic\Provider\DateTimeProvider(@elasticSearch.dateTime)
143174

144175
dateTime:
145176
class: \DateTimeImmutable
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\Elastic\DI;
4+
5+
6+
class ElasticSearch30Extension extends \Nette\DI\CompilerExtension
7+
{
8+
9+
10+
}

src/DI/ElasticSearchExtension.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ public function loadConfiguration() : void
106106

107107
$services = $this->toggleDebugBar($config, $services);
108108

109+
if ( ! class_exists(\Symfony\Component\Console\Command\Command::class)) {
110+
$services = $this->removeCommandDefinitions($services);
111+
}
112+
113+
if ( ! \class_exists(\Nette\Security\User::class)) {
114+
unset($services['services']['userProvider']);
115+
}
116+
109117
$this->setConfigOptions($services, $config);
110118

111119
$this->compiler->parseServices(
@@ -175,4 +183,23 @@ public function toggleDebugBar(
175183
return $services;
176184
}
177185

186+
187+
public function removeCommandDefinitions(
188+
array $services
189+
): array
190+
{
191+
$iterableServices = $services['services'];
192+
foreach ($iterableServices as $serviceKey => $serviceArray) {
193+
if (isset($serviceArray['tags'])) {
194+
foreach ($serviceArray['tags'] as $tag) {
195+
if ($tag === 'kdyby.console.command') {
196+
unset($services[$serviceKey]);
197+
}
198+
}
199+
}
200+
}
201+
202+
return $services;
203+
}
204+
178205
}

src/Diagnostics/PanelLogger.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ class PanelLogger implements \Psr\Log\LoggerInterface
2525

2626

2727
public function __construct(
28-
\Psr\Log\LoggerInterface $logger = NULL
28+
\Psr\Log\LoggerInterface $logger
2929
)
3030
{
31-
$this->logger = $logger ?: new \Psr\Log\NullLogger;
31+
$this->logger = $logger;
3232
}
3333

3434

src/Entity/Property/DateTime.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class DateTime extends \Nette\Utils\DateTime implements \Spameri\Elastic\Entity\
77
{
88

99
public const FORMAT = 'Y-m-d\TH:i:s';
10+
public const INDEX_FORMAT = 'Y-m-d_H-i-s';
1011

1112

1213
public function format($format = NULL)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\Elastic\Exception;
4+
5+
6+
class ScrollNotInitialized extends \Spameri\Elastic\Exception\ElasticSearchException
7+
{
8+
9+
}

src/Mapper/ElasticMapper.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ class ElasticMapper
1212
private $clientProvider;
1313

1414
/**
15-
* @var \Kdyby\DateTimeProvider\Provider\ConstantProvider
15+
* @var \Spameri\Elastic\Provider\DateTimeProvider
1616
*/
17-
private $constantProvider;
17+
private $dateTimeProvider;
1818

1919

2020
public function __construct(
2121
\Spameri\Elastic\ClientProvider $clientProvider
22-
, \Kdyby\DateTimeProvider\Provider\ConstantProvider $constantProvider
22+
, \Spameri\Elastic\Provider\DateTimeProvider $dateTimeProvider
2323
)
2424
{
2525
$this->clientProvider = $clientProvider;
26-
$this->constantProvider = $constantProvider;
26+
$this->dateTimeProvider = $dateTimeProvider;
2727
}
2828

2929

@@ -74,7 +74,7 @@ public function createIndex(array $entity) : void
7474
throw new \Spameri\Elastic\Exception\IndexAlreadyExists($entity['index']);
7575

7676
} catch (\Elasticsearch\Common\Exceptions\Missing404Exception $exception) {
77-
$indexName = $entity['index'] . '-' . $this->constantProvider->getDateTime()->format('Y-m-d_H-i-s');
77+
$indexName = $entity['index'] . '-' . $this->dateTimeProvider->provide()->format(\Spameri\Elastic\Entity\Property\DateTime::INDEX_FORMAT);
7878
$this->clientProvider->client()->indices()->create(
7979
(
8080
new \Spameri\ElasticQuery\Document(

0 commit comments

Comments
 (0)