Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Deleting objects/assets does not update generic data index #204

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/EventSubscriber/AssetIndexUpdateSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ public function deleteAsset(AssetEvent $event): void
processSynchronously: $this->synchronousProcessing->isEnabled()
)
->commit();

$this->queueMessagesDispatcher->dispatchQueueMessages();
}
}
2 changes: 1 addition & 1 deletion src/EventSubscriber/DataObjectIndexUpdateSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function getSubscribedEvents(): array
return [
DataObjectEvents::POST_UPDATE => 'updateDataObject',
DataObjectEvents::POST_ADD => 'updateDataObject',
DataObjectEvents::PRE_DELETE => 'deleteDataObject',
DataObjectEvents::POST_DELETE => 'deleteDataObject',
];
}

Expand Down
5 changes: 3 additions & 2 deletions src/Service/SearchIndex/IndexQueue/EnqueueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,14 @@ public function enqueueDocuments(): EnqueueService
*/
public function enqueueRelatedItemsOnUpdate(
ElementInterface $element,
bool $includeElement
bool $includeElement,
string $operation
): void {
$subQuery = $this->typeAdapterService
->getTypeAdapter($element)
->getRelatedItemsOnUpdateQuery(
element: $element,
operation: IndexQueueOperation::UPDATE->value,
operation: $operation,
operationTime: $this->timeService->getCurrentMillisecondTimestamp(),
includeElement: $includeElement,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public function enqueueDocuments(): self;
*/
public function enqueueRelatedItemsOnUpdate(
ElementInterface $element,
bool $includeElement
bool $includeElement,
string $operation
): void;

public function dispatchQueueMessages(bool $synchronously = false): void;
Expand Down
26 changes: 21 additions & 5 deletions src/Service/SearchIndex/IndexQueueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function __construct(
private readonly IndexQueueRepository $indexQueueRepository,
private readonly EnqueueServiceInterface $enqueueService,
private readonly ElementServiceInterface $elementService,
private readonly SearchIndexConfigServiceInterface $searchIndexConfigService
) {
}

Expand All @@ -63,7 +64,8 @@ public function updateIndexQueue(

$this->enqueueService->enqueueRelatedItemsOnUpdate(
element: $element,
includeElement: !$processSynchronously
includeElement: !$processSynchronously,
operation: $operation
);

if ($element instanceof Asset) {
Expand Down Expand Up @@ -101,10 +103,7 @@ public function handleIndexQueueEntries(array $entries): void
$entry->getElementId(),
$entry->getElementType()
));
$element = $this->elementService->getElementByType($entry->getElementId(), $entry->getElementType());
if ($element) {
$this->doHandleIndexData($element, $entry->getOperation());
}
$this->handleEntryByOperation($entry->getOperation(), $entry);
}

$this->bulkOperationService->commit();
Expand Down Expand Up @@ -141,6 +140,23 @@ private function doHandleIndexData(ElementInterface $element, string $operation)
}
}

private function handleEntryByOperation(string $operation, IndexQueue $entry): void
{
if ($operation === IndexQueueOperation::DELETE->value) {
$this->indexService->deleteFromSpecificIndex(
$this->searchIndexConfigService->getIndexName($entry->getElementIndexName()),
$entry->getElementId()
);

return;
}

$element = $this->elementService->getElementByType($entry->getElementId(), $entry->getElementType());
if ($element) {
$this->doHandleIndexData($element, $entry->getOperation());
}
}

/**
* @throws IndexDataException
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Service/SearchIndex/IndexService/IndexService.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public function deleteFromIndex(ElementInterface $element): IndexService

$elementId = $element->getId();

return $this->deleteFromSpecificIndex($indexName, $elementId);
}

public function deleteFromSpecificIndex(string $indexName, int $elementId): IndexService
{
$this->bulkOperationService->addDeletion(
$indexName,
$elementId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ 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;
}
68 changes: 56 additions & 12 deletions tests/Functional/SearchIndex/IndexQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,34 @@

namespace Functional\SearchIndex;

use Codeception\Test\Unit;
use Exception;
use OpenSearch\Common\Exceptions\Missing404Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexName;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexQueueOperation;
use Pimcore\Bundle\GenericDataIndexBundle\Repository\IndexQueueRepository;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\SearchIndexConfigServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Tests\IndexTester;
use Pimcore\Db;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Tests\Support\Util\TestHelper;

class IndexQueueTest extends \Codeception\Test\Unit
class IndexQueueTest extends Unit
{
/**
* @var \Pimcore\Bundle\GenericDataIndexBundle\Tests\IndexTester
*/
protected $tester;
protected IndexTester $tester;

private SearchIndexConfigServiceInterface $searchIndexConfigService;

private const ASSET_INDEX_NAME = 'asset';

private const DOCUMENT_INDEX_NAME = 'document';

protected function _before()
{
$this->searchIndexConfigService = $this->tester->grabService(
SearchIndexConfigServiceInterface::class
);
$this->tester->disableSynchronousProcessing();
$this->tester->clearQueue();
}
Expand Down Expand Up @@ -94,11 +104,7 @@ public function testIndexQueueRepository(): void

public function testAssetSaveNotEnqueued(): void
{
/**
* @var SearchIndexConfigServiceInterface $searchIndexConfigService
*/
$searchIndexConfigService = $this->tester->grabService(SearchIndexConfigServiceInterface::class);
$indexName = $searchIndexConfigService->getIndexName('asset');
$indexName = $this->searchIndexConfigService->getIndexName(self::ASSET_INDEX_NAME);

$asset = TestHelper::createImageAsset();

Expand All @@ -112,7 +118,7 @@ public function testAssetSaveProcessQueue(): void
* @var SearchIndexConfigServiceInterface $searchIndexConfigService
*/
$searchIndexConfigService = $this->tester->grabService(SearchIndexConfigServiceInterface::class);
$indexName = $searchIndexConfigService->getIndexName('asset');
$indexName = $searchIndexConfigService->getIndexName(self::ASSET_INDEX_NAME);

$asset = TestHelper::createImageAsset();

Expand All @@ -123,8 +129,46 @@ public function testAssetSaveProcessQueue(): void
)
);

$this->tester->runCommand('messenger:consume', ['--limit'=>2], ['pimcore_generic_data_index_queue']);
$this->consume();
$result = $this->tester->checkIndexEntry($asset->getId(), $indexName);
$this->assertEquals($asset->getId(), $result['_source']['system_fields']['id']);
}

/**
* @throws Exception
*/
public function testElementDeleteWithQueue(): void
{
$this->checkAndDeleteElement(
TestHelper::createImageAsset(),
$this->searchIndexConfigService->getIndexName(self::ASSET_INDEX_NAME)
);

$this->checkAndDeleteElement(
TestHelper::createEmptyDocument(),
$this->searchIndexConfigService->getIndexName(self::DOCUMENT_INDEX_NAME)
);

$object = TestHelper::createEmptyObject('', false);
$this->checkAndDeleteElement(
$object,
$this->searchIndexConfigService->getIndexName($object->getClassName())
);
}

private function checkAndDeleteElement(ElementInterface $element, string $indexName): void
{
$this->consume();
$this->tester->checkIndexEntry($element->getId(), $indexName);

$element->delete();
$this->consume();
$this->expectException(Missing404Exception::class);
$this->tester->checkIndexEntry($element->getId(), $indexName);
}

private function consume(): void
{
$this->tester->runCommand('messenger:consume', ['--limit'=>2], ['pimcore_generic_data_index_queue']);
}
}
Loading