From 6bcbfdc9b8f0513c646980be58108ed56e434a93 Mon Sep 17 00:00:00 2001 From: lukmzig Date: Thu, 14 Nov 2024 08:37:43 +0100 Subject: [PATCH] add functionality to enqueue dependent items --- config/services/search/search-services.yaml | 3 + src/Model/SearchIndex/HitData.php | 42 ++++++++++++++ src/Repository/IndexQueueRepository.php | 48 +++++++++++++++- .../OpenSearch/OpenSearchService.php | 4 +- .../Search/FetchIdsBySearchService.php | 48 ++++++++++++++++ .../FetchIdsBySearchServiceInterface.php | 10 ++++ src/Service/ElementService.php | 16 ++++++ src/Service/ElementServiceInterface.php | 7 +++ .../Asset/AssetSearchService.php | 10 ++-- .../Document/DocumentSearchService.php | 10 ++-- .../SearchService/IndexNameResolver.php | 4 +- .../RequiredByElementListService.php | 57 +++++++++++++++++++ .../RequiredByElementListServiceInterface.php | 29 ++++++++++ .../SearchIndex/IndexQueue/EnqueueService.php | 24 +++++++- .../IndexQueue/EnqueueServiceInterface.php | 14 ++++- src/Service/SearchIndex/IndexQueueService.php | 48 ++++++++++++---- .../AbstractElementTypeAdapter.php | 11 +++- .../DataObjectTypeAdapter.php | 7 +-- .../SearchIndex/IndexService/IndexService.php | 19 ------- .../IndexService/IndexServiceInterface.php | 2 - .../SearchIndex/SearchIndexConfigService.php | 30 ++++++++++ .../SearchIndexConfigServiceInterface.php | 5 ++ .../Functional/SearchIndex/IndexQueueTest.php | 2 +- 23 files changed, 389 insertions(+), 61 deletions(-) create mode 100644 src/Model/SearchIndex/HitData.php create mode 100644 src/Service/Search/SearchService/RequiredByElementListService.php create mode 100644 src/Service/Search/SearchService/RequiredByElementListServiceInterface.php diff --git a/config/services/search/search-services.yaml b/config/services/search/search-services.yaml index 1df6548d..682cf04f 100644 --- a/config/services/search/search-services.yaml +++ b/config/services/search/search-services.yaml @@ -40,6 +40,9 @@ services: Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchResultItem\LazyLoading\AssetLazyLoadingHandlerInterface: class: Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchResultItem\LazyLoading\AssetLazyLoadingHandler + Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\RequiredByElementListServiceInterface: + class: Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\RequiredByElementListService + Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchResultItem\LazyLoading\DataObjectLazyLoadingHandlerInterface: class: Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchResultItem\LazyLoading\DataObjectLazyLoadingHandler diff --git a/src/Model/SearchIndex/HitData.php b/src/Model/SearchIndex/HitData.php new file mode 100644 index 00000000..f5c1e879 --- /dev/null +++ b/src/Model/SearchIndex/HitData.php @@ -0,0 +1,42 @@ +id; + } + + public function getElementType(): string + { + return $this->elementType; + } + + public function getIndex(): string + { + return $this->index; + } +} \ No newline at end of file diff --git a/src/Repository/IndexQueueRepository.php b/src/Repository/IndexQueueRepository.php index 894623a0..cf42d49b 100644 --- a/src/Repository/IndexQueueRepository.php +++ b/src/Repository/IndexQueueRepository.php @@ -17,6 +17,7 @@ namespace Pimcore\Bundle\GenericDataIndexBundle\Repository; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Exception as DBALException; use Doctrine\DBAL\Query\QueryBuilder as DBALQueryBuilder; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\NonUniqueResultException; @@ -24,6 +25,8 @@ use Doctrine\ORM\QueryBuilder; use Exception; use Pimcore\Bundle\GenericDataIndexBundle\Entity\IndexQueue; +use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexQueueOperation; +use Pimcore\Bundle\GenericDataIndexBundle\Model\SearchIndex\HitData; use Pimcore\Bundle\GenericDataIndexBundle\Service\TimeServiceInterface; use Pimcore\Bundle\GenericDataIndexBundle\Traits\LoggerAwareTrait; use Symfony\Component\Serializer\Exception\ExceptionInterface; @@ -105,7 +108,7 @@ public function getUnhandledIndexQueueEntries( /** * @param IndexQueue[] $entries * - * @throws \Doctrine\DBAL\Exception + * @throws DBALException */ public function deleteQueueEntries(array $entries): void { @@ -166,7 +169,7 @@ public function generateSelectQuery( } /** - * @throws \Doctrine\DBAL\Exception + * @throws DBALException */ public function enqueueBySelectQuery(DBALQueryBuilder $queryBuilder): void { @@ -186,7 +189,46 @@ public function enqueueBySelectQuery(DBALQueryBuilder $queryBuilder): void } /** - * @throws \Doctrine\DBAL\Exception + * @throws DBALException + * @param HitData[] $enqueueItemList + */ + public function enqueueByItemList(array $enqueueItemList, IndexQueueOperation $operation, int $operationTime): void + { + if (empty($enqueueItemList)) { + return; + } + + $sql = <<connection->quote($item->getId()), + $this->connection->quote($item->getElementType()), + $this->connection->quote($item->getIndex()), + $operation->value, + $operationTime + ); + } + + $this->connection->executeQuery( + sprintf($sql, IndexQueue::TABLE, implode(',', $values)) + ); + } + + + /** + * @throws DBALException */ public function dispatchItems( int $limit diff --git a/src/SearchIndexAdapter/OpenSearch/OpenSearchService.php b/src/SearchIndexAdapter/OpenSearch/OpenSearchService.php index 42009f59..cbbbd8ef 100644 --- a/src/SearchIndexAdapter/OpenSearch/OpenSearchService.php +++ b/src/SearchIndexAdapter/OpenSearch/OpenSearchService.php @@ -37,9 +37,9 @@ */ final class OpenSearchService implements SearchIndexServiceInterface { - private const INDEX_VERSION_ODD = 'odd'; + public const INDEX_VERSION_ODD = 'odd'; - private const INDEX_VERSION_EVEN = 'even'; + public const INDEX_VERSION_EVEN = 'even'; use LoggerAwareTrait; diff --git a/src/SearchIndexAdapter/OpenSearch/Search/FetchIdsBySearchService.php b/src/SearchIndexAdapter/OpenSearch/Search/FetchIdsBySearchService.php index b2811e02..94fb22e5 100644 --- a/src/SearchIndexAdapter/OpenSearch/Search/FetchIdsBySearchService.php +++ b/src/SearchIndexAdapter/OpenSearch/Search/FetchIdsBySearchService.php @@ -16,11 +16,14 @@ namespace Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\OpenSearch\Search; +use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\FieldCategory; use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\FieldCategory\SystemField; use Pimcore\Bundle\GenericDataIndexBundle\Exception\InvalidArgumentException; use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\OpenSearchSearchInterface; use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Sort\FieldSort; use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Sort\FieldSortList; +use Pimcore\Bundle\GenericDataIndexBundle\Model\SearchIndex\HitData; +use Pimcore\Bundle\GenericDataIndexBundle\Model\SearchIndexAdapter\SearchResultHit; use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\SearchIndexServiceInterface; use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\SearchIndexConfigServiceInterface; @@ -46,6 +49,27 @@ public function fetchAllIds(OpenSearchSearchInterface $search, string $indexName return $this->doFetchIds($search, $indexName); } + /** + * @return HitData[] + */ + public function fetchAllTypesAndIds( + OpenSearchSearchInterface $search, + string $indexName, + bool $sortById = true + ): array + { + $search = clone $search; + if ($sortById) { + $search->setSortList(new FieldSortList([new FieldSort(SystemField::ID->getPath())])); + } + + if ($search->getSortList()->isEmpty()) { + throw new InvalidArgumentException('Search must have a sort defined to be able to fetch all ids'); + } + + return $this->doFetchIdsAndTypes($search, $indexName); + } + private function doFetchIds(OpenSearchSearchInterface $search, string $indexName, ?array $searchAfter = null): array { $search->setFrom(0); @@ -63,6 +87,30 @@ private function doFetchIds(OpenSearchSearchInterface $search, string $indexName return $ids; } + private function doFetchIdsAndTypes(OpenSearchSearchInterface $search, string $indexName, ?array $searchAfter = null): array + { + $search->setFrom(0); + $search->setSize($this->getPageSize()); + $search->setSource([SystemField::ELEMENT_TYPE->getPath()]); + $search->setSearchAfter($searchAfter); + $searchResult = $this->searchIndexService->search($search, $indexName); + $hits = $searchResult->getHits(); + $idsAndTypes = array_map( + static fn (SearchResultHit $item) => + new HitData( + id: $item->getId(), + elementType: $item->getSource()[FieldCategory::SYSTEM_FIELDS->value][SystemField::ELEMENT_TYPE->value], + index: $item->getIndex(), + ), + $hits); + $lastHit = $searchResult->getLastHit(); + if ($lastHit && (count($hits) === $this->getPageSize())) { + return array_merge($idsAndTypes, $this->doFetchIdsAndTypes($search, $indexName, $lastHit->getSort())); + } + + return $idsAndTypes; + } + private function getPageSize(): int { $maxResultWindow = $this->searchIndexConfigService->getIndexSettings()['max_result_window']; diff --git a/src/SearchIndexAdapter/OpenSearch/Search/FetchIdsBySearchServiceInterface.php b/src/SearchIndexAdapter/OpenSearch/Search/FetchIdsBySearchServiceInterface.php index db10dc81..847635bd 100644 --- a/src/SearchIndexAdapter/OpenSearch/Search/FetchIdsBySearchServiceInterface.php +++ b/src/SearchIndexAdapter/OpenSearch/Search/FetchIdsBySearchServiceInterface.php @@ -17,8 +17,18 @@ namespace Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\OpenSearch\Search; use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\OpenSearchSearchInterface; +use Pimcore\Bundle\GenericDataIndexBundle\Model\SearchIndex\HitData; interface FetchIdsBySearchServiceInterface { public function fetchAllIds(OpenSearchSearchInterface $search, string $indexName, bool $sortById = true): array; + + /** + * @return HitData[] + */ + public function fetchAllTypesAndIds( + OpenSearchSearchInterface $search, + string $indexName, + bool $sortById = true + ): array; } diff --git a/src/Service/ElementService.php b/src/Service/ElementService.php index 86260ac5..60e3558f 100644 --- a/src/Service/ElementService.php +++ b/src/Service/ElementService.php @@ -21,8 +21,10 @@ use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType; use Pimcore\Bundle\GenericDataIndexBundle\Exception\InvalidElementTypeException; use Pimcore\Model\Asset; +use Pimcore\Model\DataObject; use Pimcore\Model\DataObject\AbstractObject; use Pimcore\Model\Document; +use Pimcore\Model\Element\ElementInterface; /** * @internal @@ -47,6 +49,20 @@ public function getElementByType(int $id, string $type): Asset|AbstractObject|Do }; } + /** + * @throws InvalidElementTypeException + */ + public function getElementType(ElementInterface $element): ElementType + { + return match (true) { + $element instanceof Asset => ElementType::ASSET, + $element instanceof Document => ElementType::DOCUMENT, + $element instanceof DataObject => ElementType::DATA_OBJECT, + default => throw new InvalidElementTypeException('Invalid element type: ' . $element->getType()) + }; + } + + public function classDefinitionExists(string $name): bool { try { diff --git a/src/Service/ElementServiceInterface.php b/src/Service/ElementServiceInterface.php index a4d3cecb..99c6031f 100644 --- a/src/Service/ElementServiceInterface.php +++ b/src/Service/ElementServiceInterface.php @@ -16,10 +16,12 @@ namespace Pimcore\Bundle\GenericDataIndexBundle\Service; +use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType; use Pimcore\Bundle\GenericDataIndexBundle\Exception\InvalidElementTypeException; use Pimcore\Model\Asset; use Pimcore\Model\DataObject\AbstractObject; use Pimcore\Model\Document; +use Pimcore\Model\Element\ElementInterface; /** * @internal @@ -33,5 +35,10 @@ interface ElementServiceInterface */ public function getElementByType(int $id, string $type): Asset|AbstractObject|Document|null; + /** + * @throws InvalidElementTypeException + */ + public function getElementType(ElementInterface $element): ElementType; + public function classDefinitionExists(string $name): bool; } diff --git a/src/Service/Search/SearchService/Asset/AssetSearchService.php b/src/Service/Search/SearchService/Asset/AssetSearchService.php index 20921bbf..6d406898 100644 --- a/src/Service/Search/SearchService/Asset/AssetSearchService.php +++ b/src/Service/Search/SearchService/Asset/AssetSearchService.php @@ -52,7 +52,7 @@ public function search( AssetSearchInterface $assetSearch, PermissionTypes $permissionType = PermissionTypes::LIST ): AssetSearchResult { - $assetSearch = $this->searchHelper->addSearchRestrictions( + $search = $this->searchHelper->addSearchRestrictions( search: $assetSearch, userPermission: UserPermissionTypes::ASSETS->value, workspaceType: AssetWorkspace::WORKSPACE_TYPE, @@ -60,7 +60,7 @@ public function search( ); $searchResult = $this->searchHelper->performSearch( - search: $assetSearch, + search: $search, indexName: $this->assetTypeAdapter->getAliasIndexName() ); @@ -75,12 +75,12 @@ public function search( items: $this->searchHelper->hydrateSearchResultHits( $searchResult, $childrenCounts, - $assetSearch->getUser() + $search->getUser() ), pagination: $this->paginationInfoService->getPaginationInfoFromSearchResult( searchResult: $searchResult, - page: $assetSearch->getPage(), - pageSize: $assetSearch->getPageSize() + page: $search->getPage(), + pageSize: $search->getPageSize() ), aggregations: $searchResult->getAggregations(), ); diff --git a/src/Service/Search/SearchService/Document/DocumentSearchService.php b/src/Service/Search/SearchService/Document/DocumentSearchService.php index 473db3f2..62d8184b 100644 --- a/src/Service/Search/SearchService/Document/DocumentSearchService.php +++ b/src/Service/Search/SearchService/Document/DocumentSearchService.php @@ -52,7 +52,7 @@ public function search( DocumentSearchInterface $documentSearch, PermissionTypes $permissionType = PermissionTypes::LIST ): DocumentSearchResult { - $documentSearch = $this->searchHelper->addSearchRestrictions( + $search = $this->searchHelper->addSearchRestrictions( search: $documentSearch, userPermission: UserPermissionTypes::DOCUMENTS->value, workspaceType: DocumentWorkspace::WORKSPACE_TYPE, @@ -60,7 +60,7 @@ public function search( ); $searchResult = $this->searchHelper->performSearch( - search: $documentSearch, + search: $search, indexName: $this->documentTypeAdapter->getAliasIndexName() ); @@ -75,12 +75,12 @@ public function search( items: $this->searchHelper->hydrateSearchResultHits( $searchResult, $childrenCounts, - $documentSearch->getUser() + $search->getUser() ), pagination: $this->paginationInfoService->getPaginationInfoFromSearchResult( searchResult: $searchResult, - page: $documentSearch->getPage(), - pageSize: $documentSearch->getPageSize() + page: $search->getPage(), + pageSize: $search->getPageSize() ), aggregations: $searchResult->getAggregations(), ); diff --git a/src/Service/Search/SearchService/IndexNameResolver.php b/src/Service/Search/SearchService/IndexNameResolver.php index 41ea6cf8..0b5e48ae 100644 --- a/src/Service/Search/SearchService/IndexNameResolver.php +++ b/src/Service/Search/SearchService/IndexNameResolver.php @@ -26,6 +26,7 @@ use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\ElementTypeAdapter\AssetTypeAdapter; use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\ElementTypeAdapter\DataObjectTypeAdapter; use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\ElementTypeAdapter\DocumentTypeAdapter; +use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\SearchIndexConfigServiceInterface; /** * @internal @@ -36,6 +37,7 @@ public function __construct( private AssetTypeAdapter $assetTypeAdapter, private DataObjectTypeAdapter $dataObjectTypeAdapter, private DocumentTypeAdapter $documentTypeAdapter, + private SearchIndexConfigServiceInterface $searchIndexConfigService ) { } @@ -56,7 +58,7 @@ public function resolveIndexName(SearchInterface $search): string } if ($search instanceof ElementSearch) { - return IndexName::ELEMENT_SEARCH->value; + return $this->searchIndexConfigService->getIndexName(IndexName::ELEMENT_SEARCH->value); } throw new InvalidArgumentException('Unsupported search type: ' . get_class($search)); diff --git a/src/Service/Search/SearchService/RequiredByElementListService.php b/src/Service/Search/SearchService/RequiredByElementListService.php new file mode 100644 index 00000000..bd0278d4 --- /dev/null +++ b/src/Service/Search/SearchService/RequiredByElementListService.php @@ -0,0 +1,57 @@ +searchProvider->createElementSearch(); + $search->addModifier( + new RequiredByFilter( + $element->getId(), + $this->elementService->getElementType($element) + ) + ); + + return $this->fetchIdsService->fetchAllTypesAndIds( + $this->transformToAdapterSearchService->transform($search), + $this->indexNameResolver->resolveIndexName($search) + ); + } +} \ No newline at end of file diff --git a/src/Service/Search/SearchService/RequiredByElementListServiceInterface.php b/src/Service/Search/SearchService/RequiredByElementListServiceInterface.php new file mode 100644 index 00000000..fdf7a585 --- /dev/null +++ b/src/Service/Search/SearchService/RequiredByElementListServiceInterface.php @@ -0,0 +1,29 @@ +requiredByElementListService->getDependencyList($element); + foreach (array_chunk($dependentItems, 1000) as $chunk) { + $this->indexQueueRepository->enqueueByItemList( + $chunk, + $operation, + $this->timeService->getCurrentMillisecondTimestamp() + ); + } + } + public function dispatchQueueMessages(bool $synchronously = false): void { $this->queueMessagesDispatcher->dispatchQueueMessages($synchronously); diff --git a/src/Service/SearchIndex/IndexQueue/EnqueueServiceInterface.php b/src/Service/SearchIndex/IndexQueue/EnqueueServiceInterface.php index 93966df4..cf7f2d2a 100644 --- a/src/Service/SearchIndex/IndexQueue/EnqueueServiceInterface.php +++ b/src/Service/SearchIndex/IndexQueue/EnqueueServiceInterface.php @@ -17,6 +17,8 @@ namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue; use Doctrine\DBAL\Exception; +use Exception as ThrowableException; +use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexQueueOperation; use Pimcore\Bundle\GenericDataIndexBundle\Exception\EnqueueElementsException; use Pimcore\Model\DataObject\ClassDefinition; use Pimcore\Model\Element\ElementInterface; @@ -53,13 +55,21 @@ public function enqueueAssets(): self; public function enqueueDocuments(): self; /** - * @throws \Exception + * @throws ThrowableException */ - public function enqueueRelatedItemsOnUpdate( + public function enqueueRelatedItems( ElementInterface $element, bool $includeElement, string $operation ): void; + /** + * @throws ThrowableException + */ + public function enqueueDependentItems( + ElementInterface $element, + IndexQueueOperation $operation + ): void; + public function dispatchQueueMessages(bool $synchronously = false): void; } diff --git a/src/Service/SearchIndex/IndexQueueService.php b/src/Service/SearchIndex/IndexQueueService.php index 439c2e0e..e693981b 100644 --- a/src/Service/SearchIndex/IndexQueueService.php +++ b/src/Service/SearchIndex/IndexQueueService.php @@ -18,6 +18,7 @@ use Exception; use Pimcore\Bundle\GenericDataIndexBundle\Entity\IndexQueue; +use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType; use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexQueueOperation; use Pimcore\Bundle\GenericDataIndexBundle\Exception\HandleIndexQueueEntriesException; use Pimcore\Bundle\GenericDataIndexBundle\Exception\IndexDataException; @@ -62,17 +63,7 @@ public function updateIndexQueue( $this->doHandleIndexData($element, $operation); } - $this->enqueueService->enqueueRelatedItemsOnUpdate( - element: $element, - includeElement: !$processSynchronously, - operation: $operation - ); - - if ($element instanceof Asset) { - foreach ($this->indexService->updateAssetDependencies($element) as $asset) { - $this->updateIndexQueue($asset, IndexQueueOperation::UPDATE->value); - } - } + $this->handleQueueByOperation($element, $operation, $processSynchronously); $this->pathService->rewriteChildrenIndexPaths($element); } catch (Exception $e) { @@ -140,11 +131,44 @@ private function doHandleIndexData(ElementInterface $element, string $operation) } } + /** + * @throws Exception + */ + private function handleQueueByOperation( + ElementInterface $element, + string $operation, + bool $processSynchronously + ): void + { + $this->enqueueService->enqueueRelatedItems( + element: $element, + includeElement: !$processSynchronously, + operation: $operation + ); + + if (($operation === IndexQueueOperation::UPDATE->value) && $element instanceof Asset) { + $this->enqueueService->enqueueDependentItems( + element: $element, + operation: IndexQueueOperation::UPDATE + ); + } + + if ($operation === IndexQueueOperation::DELETE->value) { + $this->enqueueService->enqueueDependentItems( + element: $element, + operation: IndexQueueOperation::DELETE + ); + } + } + private function handleEntryByOperation(string $operation, IndexQueue $entry): void { if ($operation === IndexQueueOperation::DELETE->value) { $this->indexService->deleteFromSpecificIndex( - $this->searchIndexConfigService->getIndexName($entry->getElementIndexName()), + $this->searchIndexConfigService->getIndexName( + $entry->getElementIndexName(), + ElementType::DATA_OBJECT->value === $entry->getElementType() + ), $entry->getElementId() ); diff --git a/src/Service/SearchIndex/IndexService/ElementTypeAdapter/AbstractElementTypeAdapter.php b/src/Service/SearchIndex/IndexService/ElementTypeAdapter/AbstractElementTypeAdapter.php index 4e4b576c..bbd40e83 100644 --- a/src/Service/SearchIndex/IndexService/ElementTypeAdapter/AbstractElementTypeAdapter.php +++ b/src/Service/SearchIndex/IndexService/ElementTypeAdapter/AbstractElementTypeAdapter.php @@ -20,6 +20,8 @@ use Exception; use Pimcore\Bundle\GenericDataIndexBundle\Event\UpdateIndexDataEventInterface; use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\SearchIndexConfigServiceInterface; +use Pimcore\Model\DataObject\ClassDefinition; +use Pimcore\Model\DataObject\Concrete; use Pimcore\Model\Element\ElementInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Contracts\Service\Attribute\Required; @@ -33,17 +35,22 @@ abstract class AbstractElementTypeAdapter abstract public function supports(ElementInterface $element): bool; + /** + * @throws Exception + */ public function getAliasIndexNameByElement(ElementInterface $element): string { return $this->searchIndexConfigService->getIndexName( - $this->getIndexNameShortByElement($element) + $this->getIndexNameShortByElement($element), + $element instanceof Concrete ); } public function getAliasIndexName(mixed $context = null): string { return $this->searchIndexConfigService->getIndexName( - $this->getIndexNameShort($context) + $this->getIndexNameShort($context), + $context instanceof ClassDefinition ); } diff --git a/src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php b/src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php index eb84f8c9..334f703f 100644 --- a/src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php +++ b/src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php @@ -26,7 +26,6 @@ use Pimcore\Bundle\GenericDataIndexBundle\Event\DataObject\UpdateFolderIndexDataEvent; use Pimcore\Bundle\GenericDataIndexBundle\Event\DataObject\UpdateIndexDataEvent; use Pimcore\Bundle\GenericDataIndexBundle\Event\UpdateIndexDataEventInterface; -use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\SearchIndexConfigService; use Pimcore\Bundle\GenericDataIndexBundle\Service\Serializer\Normalizer\DataObjectNormalizer; use Pimcore\Model\DataObject\AbstractObject; use Pimcore\Model\DataObject\ClassDefinition; @@ -67,7 +66,7 @@ public function getIndexNameShortByElement(ElementInterface $element): string public function getIndexNameShort(mixed $context): string { return match (true) { - $context instanceof ClassDefinition => SearchIndexConfigService::CLASS_INDEX_PREFIX . $context->getName(), + $context instanceof ClassDefinition => $context->getName(), $context === IndexName::DATA_OBJECT->value => $context, default => IndexName::DATA_OBJECT_FOLDER->value, }; @@ -75,9 +74,7 @@ public function getIndexNameShort(mixed $context): string public function getIndexNameByClassDefinition(ClassDefinition $classDefinition): string { - return $this->searchIndexConfigService->getIndexName( - SearchIndexConfigService::CLASS_INDEX_PREFIX . $classDefinition->getName() - ); + return $this->searchIndexConfigService->getIndexName($classDefinition->getName(), true); } public function getElementType(): string diff --git a/src/Service/SearchIndex/IndexService/IndexService.php b/src/Service/SearchIndex/IndexService/IndexService.php index 250ed892..4fe5c214 100644 --- a/src/Service/SearchIndex/IndexService/IndexService.php +++ b/src/Service/SearchIndex/IndexService/IndexService.php @@ -120,25 +120,6 @@ public function deleteFromSpecificIndex(string $indexName, int $elementId): Inde return $this; } - public function updateAssetDependencies(Asset $asset): array - { - $elementsToUpdate = []; - foreach ($asset->getDependencies()->getRequiredBy() as $requiredByEntry) { - $element = null; - if ($requiredByEntry['type'] === 'object') { - $element = AbstractObject::getById($requiredByEntry['id']); - } - if ($requiredByEntry['type'] === 'asset') { - $element = Asset::getById($requiredByEntry['id']); - } - if ($element instanceof ElementInterface) { - $elementsToUpdate[] = $element; - } - } - - return $elementsToUpdate; - } - /** * @throws IndexDataException */ diff --git a/src/Service/SearchIndex/IndexService/IndexServiceInterface.php b/src/Service/SearchIndex/IndexService/IndexServiceInterface.php index 3999f12e..fb6298f3 100644 --- a/src/Service/SearchIndex/IndexService/IndexServiceInterface.php +++ b/src/Service/SearchIndex/IndexService/IndexServiceInterface.php @@ -33,6 +33,4 @@ public function updateIndexData(ElementInterface $element): IndexService; public function deleteFromIndex(ElementInterface $element): IndexService; public function deleteFromSpecificIndex(string $indexName, int $elementId): IndexService; - - public function updateAssetDependencies(Asset $asset): array; } diff --git a/src/Service/SearchIndex/SearchIndexConfigService.php b/src/Service/SearchIndex/SearchIndexConfigService.php index 46a5a7be..ec21e3cc 100644 --- a/src/Service/SearchIndex/SearchIndexConfigService.php +++ b/src/Service/SearchIndex/SearchIndexConfigService.php @@ -16,6 +16,7 @@ namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex; +use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\OpenSearch\OpenSearchService; use Psr\Log\LoggerAwareTrait; /** @@ -55,6 +56,30 @@ public function getIndexName(string $name, bool $isClass = false): string return $this->getIndexPrefix() . strtolower($name); } + /** + * return index name without any prefix or suffix + */ + public function getShortIndexName(string $name): string + { + if (str_starts_with($name, $this->getIndexPrefixWIthClassPrefix())) { + $name = substr($name, 0, strlen($this->getIndexPrefixWIthClassPrefix())); + } + + if (str_starts_with($name, $this->getIndexPrefix())) { + $name = substr($name, 0, strlen($this->getIndexPrefix())); + } + + if (str_ends_with($name, '-' . OpenSearchService::INDEX_VERSION_ODD)) { + $name = substr($name, strlen('-' . OpenSearchService::INDEX_VERSION_ODD)); + } + + if (str_ends_with($name, '-' . OpenSearchService::INDEX_VERSION_EVEN)) { + $name = substr($name, strlen('-' . OpenSearchService::INDEX_VERSION_EVEN)); + } + + return $name; + } + public function prefixIndexName(string $indexName): string { return $this->getIndexPrefix() . $indexName; @@ -103,4 +128,9 @@ public function getSystemFieldsSettings(string $elementType): array return $systemFieldsSettings; } + + private function getIndexPrefixWIthClassPrefix(): string + { + return $this->indexPrefix . self::CLASS_INDEX_PREFIX; + } } diff --git a/src/Service/SearchIndex/SearchIndexConfigServiceInterface.php b/src/Service/SearchIndex/SearchIndexConfigServiceInterface.php index ca8ebd8a..89e3bd33 100644 --- a/src/Service/SearchIndex/SearchIndexConfigServiceInterface.php +++ b/src/Service/SearchIndex/SearchIndexConfigServiceInterface.php @@ -26,6 +26,11 @@ interface SearchIndexConfigServiceInterface */ public function getIndexName(string $name, bool $isClass = false): string; + /** + * return index name without any prefix or suffix + */ + public function getShortIndexName(string $name): string; + public function prefixIndexName(string $indexName): string; public function getIndexPrefix(): string; diff --git a/tests/Functional/SearchIndex/IndexQueueTest.php b/tests/Functional/SearchIndex/IndexQueueTest.php index 04aa188f..10c47397 100644 --- a/tests/Functional/SearchIndex/IndexQueueTest.php +++ b/tests/Functional/SearchIndex/IndexQueueTest.php @@ -170,7 +170,7 @@ public function testDocumentDeleteWithQueue(): void public function testDataObjectDeleteWithQueue(): void { $object = TestHelper::createEmptyObject(); - $objectIndex = $this->searchIndexConfigService->getIndexName($object->getClassName()); + $objectIndex = $this->searchIndexConfigService->getIndexName($object->getClassName(), true); $this->consume(); $this->checkAndDeleteElement($object, $objectIndex);