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

[Filters] Add filter to list items without dependencies #211

Draft
wants to merge 2 commits into
base: 1.x
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ $search->addModifier(new ParentIdFilter(1))

### Dependencies

| Modifier | Modifier Category | Description |
|------------------------------------------------------------------------------------------------------------------------------------------------|-------------------|-----------------------------------------------------------|
| [RequiresFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/Dependency/RequiresFilter.php) | Dependencies | Get all elements which the given element requires. |
| [RequiredByFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/Dependency/RequiredByFilter.php) | Dependencies | Get all elements which are required by the given element. |
| Modifier | Modifier Category | Description |
|------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------|---------------------------------------------------------|
| [RequiresFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/Dependency/RequiresFilter.php) | Dependencies | Get all elements which the given element requires. |
| [RequiredByFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/Dependency/RequiredByFilter.php) | Dependencies | Get all elements which are required by the given element. |
| [NoDependenciesFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/Dependency/NoDependenciesFilter.php) | Dependencies | Get all elements which have no dependencies. |


### Query Language
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Dependency;

use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\SearchModifierInterface;

final readonly class NoDependenciesFilter implements SearchModifierInterface
{
public function __construct(private ?ElementType $elementType = null)
{
}

public function getElementType(): ?ElementType
{
return $this->elementType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Query\BoolQuery;
use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Query\TermFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Query\TermsFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Dependency\NoDependenciesFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Dependency\RequiredByFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Dependency\RequiresFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Element\ElementSearchServiceInterface;
Expand Down Expand Up @@ -104,4 +105,23 @@ public function handleRequiresFilter(
])
);
}

#[AsSearchModifierHandler]
public function handleFilterWithoutDependencies(
NoDependenciesFilter $noDependenciesFilter,
SearchModifierContextInterface $context
): void {
$boolQuery = new BoolQuery();
$boolQuery->addCondition(
ConditionType::MUST_NOT->value,
[
ConditionType::EXISTS->value => [
'field' => SystemField::DEPENDENCIES->getPath(
$noDependenciesFilter->getElementType()?->getShortValue()
),
],
]
)->toArray(true);
$context->getSearch()->addQuery($boolQuery);
}
}
44 changes: 44 additions & 0 deletions tests/Functional/Search/Modifier/Filter/DependencyFiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
namespace Pimcore\Bundle\GenericDataIndexBundle\Tests\Functional\Search\Modifier\Filter;

use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Dependency\NoDependenciesFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Dependency\RequiredByFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Dependency\RequiresFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\AssetSearchServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\DataObject\DataObjectSearchServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\SearchProviderInterface;
use Pimcore\Model\DataObject\Unittest;
use Pimcore\Tests\Support\Util\TestHelper;
Expand Down Expand Up @@ -83,6 +85,48 @@ public function testDependencyFilters()
$this->assertIdArrayEquals([$object2->getId(), $object3->getId()], $searchResult->getIds());
}

// tests
public function testNoDependencyFilter()
{
/**
* @var Unittest $object1
* @var Unittest $object2
* @var Unittest $object3
* @var Unittest $object4
*/
$object1 = TestHelper::createEmptyObject();
$object2 = TestHelper::createEmptyObject();
$object3 = TestHelper::createEmptyObject();
$object4 = TestHelper::createEmptyObject();

$image1 = TestHelper::createImageAsset();

$object1
->setObjects([$object2, $object3])
->setImage($image1)
->save()
;

$object4->setObjects([$object1])->save();

/** @var DataObjectSearchServiceInterface $searchService */
$searchService = $this->tester->grabService('generic-data-index.test.service.data-object-search-service');
/** @var SearchProviderInterface $searchProvider */
$searchProvider = $this->tester->grabService(SearchProviderInterface::class);

$elementSearch = $searchProvider->createDataObjectSearch()->addModifier(new NoDependenciesFilter());
$searchResult = $searchService->search($elementSearch);
// Object 2 and 3 have no dependencies
$this->assertIdArrayEquals([$object2->getId(), $object3->getId()], $searchResult->getIds());

$elementSearch = $searchProvider->createDataObjectSearch()->addModifier(
new NoDependenciesFilter(ElementType::ASSET)
);
$searchResult = $searchService->search($elementSearch);
// Only Object 1 has no asset dependencies
$this->assertIdArrayEquals([$object2->getId(), $object3->getId(), $object4->getId()], $searchResult->getIds());
}

private function assertIdArrayEquals(array $ids1, array $ids2)
{
sort($ids1);
Expand Down