Skip to content
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/Service/SearchIndex/GlobalIndexAliasService.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private function getAliasesForAllClasses(): array
$classes = $this->connection->fetchFirstColumn('select name from classes');

return array_map(function (string $class) {
return $this->searchIndexConfigService->getIndexName($class);
return $this->searchIndexConfigService->getIndexName($class, true);
}, $classes);
}

Expand Down
11 changes: 9 additions & 2 deletions src/Service/SearchIndex/IndexEntityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ public function __construct(

public function getByEntityName(string $entityName): IndexEntity
{
$isClass = false;
$indexType = $this->getIndexType($entityName);

if ($indexType === IndexType::DATA_OBJECT) {
$isClass = $this->elementService->classDefinitionExists($entityName);
}

return new IndexEntity(
$entityName,
$this->searchIndexConfigService->getIndexName($entityName),
$this->getIndexType($entityName)
$this->searchIndexConfigService->getIndexName($entityName, $isClass),
$indexType
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -65,15 +66,17 @@ public function getIndexNameShortByElement(ElementInterface $element): string
public function getIndexNameShort(mixed $context): string
{
return match (true) {
$context instanceof ClassDefinition => $context->getName(),
$context instanceof ClassDefinition => SearchIndexConfigService::CLASS_INDEX_PREFIX . $context->getName(),
$context === IndexName::DATA_OBJECT->value => $context,
default => IndexName::DATA_OBJECT_FOLDER->value,
};
}

public function getIndexNameByClassDefinition(ClassDefinition $classDefinition): string
{
return $this->searchIndexConfigService->getIndexName($classDefinition->getName());
return $this->searchIndexConfigService->getIndexName(
SearchIndexConfigService::CLASS_INDEX_PREFIX . $classDefinition->getName()
);
}

public function getElementType(): string
Expand Down
8 changes: 7 additions & 1 deletion src/Service/SearchIndex/SearchIndexConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ final class SearchIndexConfigService implements SearchIndexConfigServiceInterfac

public const SYSTEM_FIELD_DATA_OBJECT = 'data_object';

public const CLASS_INDEX_PREFIX = 'data-object_';

public function __construct(
private readonly string $indexPrefix,
private readonly array $indexSettings,
Expand All @@ -44,8 +46,12 @@ public function __construct(
/**
* returns index name for given class name
*/
public function getIndexName(string $name): string
public function getIndexName(string $name, bool $isClass = false): string
{
if ($isClass) {
return $this->getIndexPrefix() . self::CLASS_INDEX_PREFIX . strtolower($name);
}

return $this->getIndexPrefix() . strtolower($name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface SearchIndexConfigServiceInterface
/**
* returns index name for given class name
*/
public function getIndexName(string $name): string;
public function getIndexName(string $name, bool $isClass = false): string;

public function prefixIndexName(string $indexName): string;

Expand Down
37 changes: 20 additions & 17 deletions tests/Functional/SearchIndex/DataObjectBasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

namespace Functional\SearchIndex;

use Exception;
use OpenSearch\Common\Exceptions\Missing404Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexName;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\DataObject\DataObjectSearchServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\SearchProviderInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\SearchIndexConfigServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\ElementTypeAdapter\DataObjectTypeAdapter;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SettingsStoreServiceInterface;
use Pimcore\Db;
use Pimcore\Model\DataObject\ClassDefinition\Data\Input;
Expand All @@ -34,13 +34,11 @@ class DataObjectBasicTest extends \Codeception\Test\Unit
*/
protected $tester;

private SearchIndexConfigServiceInterface $searchIndexConfigService;
private DataObjectTypeAdapter $dataObjectTypeAdapter;

protected function _before()
{
$this->searchIndexConfigService = $this->tester->grabService(
SearchIndexConfigServiceInterface::class
);
$this->dataObjectTypeAdapter = $this->tester->grabService(DataObjectTypeAdapter::class);
$this->tester->enableSynchronousProcessing();
$this->tester->clearQueue();
}
Expand All @@ -55,10 +53,14 @@ protected function _after()
}

// tests

/**
* @throws Exception
*/
public function testIndexingWithInheritanceSynchronous()
{
$object = $this->createObjectWithInheritance();
$indexName = $this->searchIndexConfigService->getIndexName($object->getClassName());
$indexName = $this->dataObjectTypeAdapter->getAliasIndexName($object->getClass());

// check indexed
$response = $this->tester->checkIndexEntry($object->getId(), $indexName);
Expand All @@ -81,7 +83,7 @@ public function testIndexingWithInheritanceAsynchronous()
$this->tester->disableSynchronousProcessing();
// create object
$object = $this->createObjectWithInheritance();
$indexName = $this->searchIndexConfigService->getIndexName($object->getClassName());
$indexName = $this->dataObjectTypeAdapter->getAliasIndexName($object->getClass());

// check indexed
$this->expectException(Missing404Exception::class);
Expand All @@ -102,7 +104,7 @@ public function testIndexingWithoutInheritanceSynchronous()
{
$object = TestHelper::createEmptyObject();
$this->assertFalse($object->getClass()->getAllowInherit());
$indexName = $this->searchIndexConfigService->getIndexName($object->getClassName());
$indexName = $this->dataObjectTypeAdapter->getAliasIndexName($object->getClass());

// check indexed
$response = $this->tester->checkIndexEntry($object->getId(), $indexName);
Expand All @@ -126,7 +128,7 @@ public function testIndexingWithoutInheritanceAsynchronous()
// create object
$object = TestHelper::createEmptyObject();
$this->assertFalse($object->getClass()->getAllowInherit());
$indexName = $this->searchIndexConfigService->getIndexName($object->getClassName());
$indexName = $this->dataObjectTypeAdapter->getAliasIndexName($object->getClass());

// check indexed
$this->expectException(Missing404Exception::class);
Expand All @@ -146,7 +148,7 @@ public function testIndexingWithoutInheritanceAsynchronous()
public function testFolderIndexing()
{
$object = TestHelper::createObjectFolder();
$indexName = $this->searchIndexConfigService->getIndexName(IndexName::DATA_OBJECT_FOLDER->value);
$indexName = $this->dataObjectTypeAdapter->getAliasIndexName();

// check indexed
$response = $this->tester->checkIndexEntry($object->getId(), $indexName);
Expand All @@ -155,7 +157,7 @@ public function testFolderIndexing()
$object->setKey('my-test-folder');
$object->save();

$indexName = $this->searchIndexConfigService->getIndexName(IndexName::DATA_OBJECT_FOLDER->value);
$indexName = $this->dataObjectTypeAdapter->getAliasIndexName();
$response = $this->tester->checkIndexEntry($object->getId(), $indexName);
$this->assertEquals('my-test-folder', $response['_source']['system_fields']['key']);

Expand Down Expand Up @@ -222,8 +224,8 @@ public function testSettingsStoreMapping()
public function testClassDefinitionMapping(): void
{
$object = TestHelper::createEmptyObject('', true, true, MappingTest::class);
$index = $this->searchIndexConfigService->getIndexName($object->getClassName());
$class = $object->getClass();
$index = $this->dataObjectTypeAdapter->getAliasIndexName($class);
$originalFields = $class->getFieldDefinitions();

$input = new Input();
Expand All @@ -232,16 +234,17 @@ public function testClassDefinitionMapping(): void
$class->save();
$this->tester->runCommand('messenger:consume', ['--limit'=>2], ['pimcore_generic_data_index_queue']);

$indexName = $this->tester->getIndexName($object->getClassName());
$indexName = $this->tester->getIndexName($class->getName(), true);
$mapping = $this->tester->getIndexMapping($index);

$standardFields = $mapping[$indexName]['mappings']['properties']['standard_fields']['properties'];
$this->assertArrayHasKey('mappingTest', $standardFields);

$class->setFieldDefinitions($originalFields);
$class->save();
$this->tester->runCommand('messenger:consume', ['--limit'=>2], ['pimcore_generic_data_index_queue']);

$indexName = $this->tester->getIndexName($object->getClassName());
$indexName = $this->tester->getIndexName($class->getName(), true);
$mapping = $this->tester->getIndexMapping($index);
$standardFields = $mapping[$indexName]['mappings']['properties']['standard_fields']['properties'];
$this->assertArrayNotHasKey('mappingTest', $standardFields);
Expand All @@ -265,7 +268,7 @@ private function classDefinitionIconChangeTest(Concrete $object, ?string $newIco

$this->tester->runCommand('messenger:consume', ['--limit'=>1], ['pimcore_generic_data_index_queue']);

$indexName = $this->tester->getIndexName($object->getClassName());
$indexName = $this->dataObjectTypeAdapter->getAliasIndexName($class);
$response = $this->tester->checkIndexEntry($object->getId(), $indexName);
$updatedIcon = $response['_source']['system_fields']['classDefinitionIcon'];

Expand All @@ -291,7 +294,7 @@ private function createObjectWithInheritance(): Concrete
$class->save();
$object->save();

$this->assertTrue($object->getClass()->getAllowInherit());
$this->assertTrue($class->getAllowInherit());

return $object;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/SearchIndex/IndexQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function testElementDeleteWithQueue(): void
$object = TestHelper::createEmptyObject('', false);
$this->checkAndDeleteElement(
$object,
$this->searchIndexConfigService->getIndexName($object->getClassName())
$this->searchIndexConfigService->getIndexName($object->getClassName(), true)
);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Support/Helper/GenericDataIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ public function clearQueue()
);
}

public function getIndexName(string $name): string
public function getIndexName(string $name, bool $isClass = false): string
{
$searchIndexConfigService = $this->grabService(SearchIndexConfigServiceInterface::class);
$indexName = $searchIndexConfigService->getIndexName($name);
$indexName = $searchIndexConfigService->getIndexName($name, $isClass);
$client = $this->getIndexSearchClient();
$alias = $client->indices()->getAlias([
'name' => $indexName,
Expand Down
Loading