Skip to content

Commit a21688e

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. DeleteMultiple and InsertMultiple maps response to class `\Spameri\ElasticQuery\Response\ResultBulk` 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 e86af35 commit a21688e

26 files changed

Lines changed: 1015 additions & 29 deletions

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
},
3838
"require-dev": {
3939
"spameri/coding-standard": "dev-master",
40+
"spameri/dependency-mocker": "^1.3",
4041
"nette/tester": "^2.2.0",
4142
"phpstan/phpstan-shim": "^0.11.5",
4243
"php-coveralls/php-coveralls": "^2.1",

makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.PHONY: tests
2+
.PHONY: tests-local
3+
.PHONY: phpstan
4+
.PHONY: cs
5+
6+
7+
tests:
8+
vendor/bin/tester tests
9+
10+
tests-local:
11+
vendor/bin/tester -c tests/SpameriTests/php.ini tests
12+
13+
phpstan:
14+
vendor/bin/phpstan analyse -l 7 -c phpstan.neon src tests
15+
16+
cs:
17+
vendor/bin/phpcs --standard=vendor/spameri/coding-standard/src/ruleset.xml src tests

src/Commands/DumpIndex.php

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

src/Commands/TypeToNewIndex.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\Elastic\Commands;
4+
5+
6+
class TypeToNewIndex extends \Symfony\Component\Console\Command\Command
7+
{
8+
9+
/**
10+
* @var \Spameri\Elastic\Model\TypeToNewIndex\Migrate
11+
*/
12+
private $migrate;
13+
14+
15+
public function __construct(
16+
\Spameri\Elastic\Model\TypeToNewIndex\Migrate $migrate
17+
)
18+
{
19+
parent::__construct(NULL);
20+
$this->migrate = $migrate;
21+
}
22+
23+
24+
/**
25+
* @example spameri:elastic:move-type oldIndex productType newIndex newIndexAlias -c
26+
*/
27+
protected function configure() : void
28+
{
29+
$this
30+
->setName('spameri:elastic:move-type')
31+
->setDescription('Move type to new index to separate data and prepare for deprecation of types is ES.')
32+
->addArgument('indexFrom', \Symfony\Component\Console\Input\InputArgument::REQUIRED)
33+
->addArgument('typeFrom', \Symfony\Component\Console\Input\InputArgument::REQUIRED)
34+
->addArgument('indexTo', \Symfony\Component\Console\Input\InputArgument::REQUIRED)
35+
->addArgument('aliasTo', \Symfony\Component\Console\Input\InputArgument::REQUIRED)
36+
->addOption('allowClose', 'c', NULL,
37+
'Allows command to close index for data transfer. After data is transferred index is opened and resumes normal operations. When open it needs to check changed files after move and sync remaining.',
38+
TRUE
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+
$indexFrom = $input->getArgument('indexFrom');
51+
$typeFrom = $input->getArgument('typeFrom');
52+
$indexTo = $input->getArgument('indexTo');
53+
$aliasTo = $input->getArgument('aliasTo');
54+
$allowClose = $input->getOption('allowClose');
55+
56+
$this->migrate->setOutput($output);
57+
$this->migrate->execute($indexFrom, $typeFrom, $indexTo, $aliasTo, $allowClose);
58+
59+
$output->writeln('Done');
60+
}
61+
62+
}

src/Config/Elastic.neon

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ services:
3232
deleteMultiple:
3333
class: Spameri\Elastic\Model\DeleteMultiple
3434

35+
indiceClose:
36+
class: Spameri\Elastic\Model\Indices\Close
37+
38+
indiceOpen:
39+
class: Spameri\Elastic\Model\Indices\Open
40+
41+
indiceGet:
42+
class: Spameri\Elastic\Model\Indices\Get
43+
44+
indiceGetMapping:
45+
class: Spameri\Elastic\Model\Indices\GetMapping
46+
47+
indicePutMapping:
48+
class: Spameri\Elastic\Model\Indices\PutMapping
49+
50+
indicesCreate:
51+
class: Spameri\Elastic\Model\Indices\Create
52+
3553
serviceLocator:
3654
class: Spameri\Elastic\Model\ServiceLocator
3755

@@ -62,6 +80,12 @@ services:
6280
display:
6381
class: Spameri\Elastic\Model\ValidateMapping\Display
6482

83+
documentMigrateStatus:
84+
class: Spameri\Elastic\Model\TypeToNewIndex\DocumentMigrateStatus
85+
86+
migrate:
87+
class: Spameri\Elastic\Model\TypeToNewIndex\Migrate
88+
6589
# ###
6690
# Commands
6791
# ###
@@ -90,6 +114,21 @@ services:
90114
class: Spameri\Elastic\Commands\ValidateMapping
91115
tags: [kdyby.console.command]
92116

117+
typeToNewIndex:
118+
class: Spameri\Elastic\Commands\TypeToNewIndex
119+
tags: [kdyby.console.command]
120+
121+
dumpIndex:
122+
class: Spameri\Elastic\Commands\DumpIndex
123+
tags: [kdyby.console.command]
124+
125+
# ###
126+
# Spameri/ElasticQuery
127+
# ###
128+
129+
resultMapper:
130+
class: Spameri\ElasticQuery\Response\ResultMapper
131+
93132
# ###
94133
# Elastic/Elastic
95134
# ###

src/Model/Aggregate.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ public function __construct(
2727

2828

2929
public function execute(
30-
\Spameri\ElasticQuery\ElasticQuery $elasticQuery,
31-
string $index
30+
\Spameri\ElasticQuery\ElasticQuery $elasticQuery
31+
, string $index
32+
, ?string $type = NULL
3233
) : \Spameri\ElasticQuery\Response\ResultSearch
3334
{
35+
if ($type === NULL) {
36+
$type = $index;
37+
}
38+
3439
try {
3540
$result = $this->clientProvider->client()->search(
3641
(
@@ -39,7 +44,7 @@ public function execute(
3944
new \Spameri\ElasticQuery\Document\Body\Plain(
4045
$elasticQuery->toArray()
4146
),
42-
$index
47+
$type
4348
)
4449
)->toArray()
4550
);

src/Model/Delete.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,20 @@ public function __construct(
2626
public function execute(
2727
\Spameri\Elastic\Entity\Property\IElasticId $id
2828
, string $index
29+
, ?string $type = NULL
2930
) : bool
3031
{
32+
if ($type === NULL) {
33+
$type = $index;
34+
}
35+
3136
try {
3237
$response = $this->clientProvider->client()->delete(
3338
(
3439
new \Spameri\ElasticQuery\Document(
3540
$index,
3641
NULL,
37-
$index,
42+
$type,
3843
$id->value()
3944
)
4045
)

src/Model/DeleteMultiple.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@ class DeleteMultiple
1111
*/
1212
private $clientProvider;
1313

14+
/**
15+
* @var \Spameri\ElasticQuery\Response\ResultMapper
16+
*/
17+
private $resultMapper;
18+
1419

1520
public function __construct(
1621
\Spameri\Elastic\ClientProvider $clientProvider
22+
, \Spameri\ElasticQuery\Response\ResultMapper $resultMapper
1723
)
1824
{
1925
$this->clientProvider = $clientProvider;
26+
$this->resultMapper = $resultMapper;
2027
}
2128

2229

@@ -27,15 +34,20 @@ public function __construct(
2734
public function execute(
2835
\Spameri\Elastic\Entity\IElasticEntityCollection $entityCollection
2936
, string $index
30-
) : array
37+
, ?string $type = NULL
38+
) : \Spameri\ElasticQuery\Response\ResultBulk
3139
{
40+
if ($type === NULL) {
41+
$type = $index;
42+
}
43+
3244
$documentsArray = [];
3345
/** @var \Spameri\Elastic\Entity\IElasticEntity $entity */
3446
foreach ($entityCollection as $entity) {
3547
$documentsArray[] = [
3648
'delete' => [
3749
'_index' => $index,
38-
'_type' => $index,
50+
'_type' => $type,
3951
'_id' => $entity->id()->value(),
4052
],
4153
];
@@ -54,7 +66,7 @@ public function execute(
5466
try {
5567
$this->clientProvider->client()->indices()->refresh(
5668
(
57-
new \Spameri\ElasticQuery\Document($index)
69+
new \Spameri\ElasticQuery\Document($index)
5870
)
5971
->toArray()
6072
);
@@ -63,11 +75,10 @@ public function execute(
6375
throw new \Spameri\Elastic\Exception\ElasticSearch($exception->getMessage());
6476
}
6577

66-
if ( ! $response['errors']) {
67-
return $response['items'];
68-
}
78+
return $this->resultMapper->mapBulkResult($response);
6979
}
7080

7181
throw new \Spameri\Elastic\Exception\DocumentInsertFailed();
7282
}
83+
7384
}

0 commit comments

Comments
 (0)