Skip to content

Commit d4a0b02

Browse files
committed
Create index if not exists while trying to reindex its mapping (#199)
(cherry picked from commit 50eedd2)
1 parent 2deddb3 commit d4a0b02

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

src/MessageHandler/UpdateClassMappingHandler.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818

1919
use Exception;
2020
use Pimcore\Bundle\GenericDataIndexBundle\Message\UpdateClassMappingMessage;
21-
use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\SearchIndexServiceInterface;
2221
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\EnqueueServiceInterface;
23-
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\ElementTypeAdapter\DataObjectTypeAdapter;
2422
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexHandler\DataObjectIndexHandler;
2523
use Pimcore\Bundle\GenericDataIndexBundle\Service\SettingsStoreServiceInterface;
2624
use Pimcore\Bundle\GenericDataIndexBundle\Traits\LoggerAwareTrait;
@@ -36,9 +34,7 @@ final class UpdateClassMappingHandler
3634

3735
public function __construct(
3836
private readonly DataObjectIndexHandler $dataObjectMappingHandler,
39-
private readonly DataObjectTypeAdapter $dataObjectTypeAdapter,
4037
private readonly EnqueueServiceInterface $enqueueService,
41-
private readonly SearchIndexServiceInterface $searchIndexService,
4238
private readonly SettingsStoreServiceInterface $settingsStoreService,
4339
) {
4440
}
@@ -59,20 +55,11 @@ public function __invoke(UpdateClassMappingMessage $message): void
5955
return;
6056
}
6157

62-
$alias = $this->dataObjectTypeAdapter->getAliasIndexName($classDefinition);
63-
if (!$this->searchIndexService->existsAlias($alias)) {
64-
$this->dataObjectMappingHandler
65-
->updateMapping(
66-
context: $classDefinition,
67-
mappingProperties: $mappingProperties
68-
);
69-
} else {
70-
$this->dataObjectMappingHandler
71-
->reindexMapping(
72-
context: $classDefinition,
73-
mappingProperties: $mappingProperties
74-
);
75-
}
58+
$this->dataObjectMappingHandler
59+
->reindexMapping(
60+
context: $classDefinition,
61+
mappingProperties: $mappingProperties
62+
);
7663

7764
$this->settingsStoreService->storeClassMapping(
7865
classDefinitionId: $classDefinition->getId(),

src/Service/SearchIndex/IndexService/IndexHandler/AbstractIndexHandler.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,21 @@ public function reindexMapping(
6565
?ClassDefinition $context = null,
6666
?array $mappingProperties = null
6767
): void {
68-
$this->searchIndexService->reindex(
69-
$this->getAliasIndexName($context),
70-
$mappingProperties ?: $this->extractMappingProperties($context)
71-
);
68+
$alias = $this->getAliasIndexName($context);
69+
$mappingProperties = $mappingProperties ?: $this->extractMappingProperties($context);
70+
71+
if (!$this->searchIndexService->existsAlias($alias)) {
72+
$this->updateMapping(
73+
context: $context,
74+
mappingProperties: $mappingProperties
75+
);
76+
} else {
77+
$this->searchIndexService->reindex(
78+
$alias,
79+
$mappingProperties
80+
);
81+
}
82+
7283
$this->createGlobalIndexAliases($context);
7384
}
7485

0 commit comments

Comments
 (0)