Skip to content

Commit 4ef0174

Browse files
committed
fix index creation with type
- type name without timestamp
1 parent dec661a commit 4ef0174

5 files changed

Lines changed: 25 additions & 7 deletions

File tree

src/Commands/InitializeIndexes.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,13 @@ protected function execute(
6565
}
6666

6767
if ($forcedDelete) {
68-
$this->delete->execute($settings->indexName());
69-
$output->writeln('Index ' . $settings->indexName() . ' deleted.');
68+
try {
69+
$this->delete->execute($settings->indexName());
70+
$output->writeln('Index ' . $settings->indexName() . ' deleted.');
71+
72+
} catch (\Spameri\Elastic\Exception\ElasticSearch $exception) {
73+
// May be non existing index exception
74+
}
7075
}
7176

7277
try {

src/Model/CreateIndex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function execute(string $index): void
3939
$indexName = $index . '-'
4040
. $this->dateTimeProvider->provide()->format(\Spameri\Elastic\Entity\Property\DateTime::INDEX_FORMAT);
4141

42-
$this->create->execute($indexName, []);
42+
$this->create->execute($indexName, [], $index);
4343
$this->addAlias->execute($index, $indexName);
4444
}
4545
}

src/Model/Indices/Create.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,18 @@ public function __construct(
2626
*/
2727
public function execute(
2828
string $index,
29-
array $parameters
29+
array $parameters,
30+
?string $type = NULL
3031
): array
3132
{
33+
if ($type === NULL) {
34+
$type = $index;
35+
}
36+
37+
if ($this->versionProvider->provide() >= \Spameri\ElasticQuery\Response\Result\Version::ELASTIC_VERSION_ID_7) {
38+
$type = NULL;
39+
}
40+
3241
if (
3342
isset($parameters['mappings']['properties'])
3443
&& $this->versionProvider->provide() <= \Spameri\ElasticQuery\Response\Result\Version::ELASTIC_VERSION_ID_7
@@ -38,7 +47,7 @@ public function execute(
3847
$parameters['mappings']['properties'][$fieldName] = $field;
3948
}
4049
$parameters['mappings'] = [
41-
$index => $parameters['mappings'],
50+
$type => $parameters['mappings'],
4251
];
4352
}
4453

src/Model/InitializeIndex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function execute(\Spameri\Elastic\Settings\IndexConfigInterface $indexCon
4242
$indexAlias . '-'
4343
. $this->dateTimeProvider->provide()->format(\Spameri\Elastic\Entity\Property\DateTime::INDEX_FORMAT);
4444

45-
$this->create->execute($indexName, $indexConfig->provide()->toArray());
45+
$this->create->execute($indexName, $indexConfig->provide()->toArray(), $indexAlias);
4646
$this->addAlias->execute($indexAlias, $indexName);
4747
}
4848
}

tests/SpameriTests/Elastic/Model/Indices/CreateTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ public function testProcess(): void
2424
$this->container->getByType(\Spameri\Elastic\Model\VersionProvider::class)
2525
);
2626

27-
$create->execute(\SpameriTests\Elastic\Config::INDEX_CREATE, $videoMapping->provide()->toArray());
27+
$create->execute(
28+
\SpameriTests\Elastic\Config::INDEX_CREATE,
29+
$videoMapping->provide()->toArray(),
30+
\SpameriTests\Elastic\Config::INDEX_CREATE
31+
);
2832
$response = $getMapping->execute(\SpameriTests\Elastic\Config::INDEX_CREATE);
2933

3034
if ($versionProvider->provide() <= \Spameri\ElasticQuery\Response\Result\Version::ELASTIC_VERSION_ID_7) {

0 commit comments

Comments
 (0)