Skip to content

Commit

Permalink
[Improvement] Add a new search modifier to allow filtering based on t…
Browse files Browse the repository at this point in the history
…he integer values (#246)

* add new modifier to filter based on the integer fields

* Apply php-cs-fixer changes

---------

Co-authored-by: lukmzig <[email protected]>
  • Loading branch information
lukmzig and lukmzig authored Nov 6, 2024
1 parent dafe781 commit dfbfa25
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ $search->addModifier(new ParentIdFilter(1))
|--------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [IdFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/Basic/IdFilter.php) | Basic filters | Filter by element ID |
| [IdsFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/Basic/IdsFilter.php) | Basic filters | Filter by multiple element IDs |
| [IntegerFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/Basic/IntegerFilter.php) | Basic filters | Filter integer fields based on the value with [PQL field name resolution support](#pql-field-name-resolution) |
| [ExcludeFoldersFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/Basic/ExcludeFoldersFilter.php) | Basic filters | Exclude folders from search result |
| [ParentIdsFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/Tree/ParentIdsFilter.php) | Tree related filters | Filter by parent ID |
| [PathFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/Tree/PathFilter.php) | Tree related filters | Filter by path (depending on use case for all levels or direct children only and with or without the parent item included) |
| [TagFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/Tree/TagFilter.php) | Tree related filters | Filter by tag IDs (it is also possible to include child tags) |
| [AssetMetaDataFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/Asset/AssetMetaDataFilter.php) | Asset filters | Filter by asset meta data attribute. The format of the `$data` which needs to be passed depends on the type of the meta data attribute and is handled by its [field definition adapter](https://github.com/pimcore/generic-data-index-bundle/tree/1.x/src/SearchIndexAdapter/OpenSearch/Asset/FieldDefinitionAdapter). |
| [WorkspaceQuery](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/Workspaces/WorkspaceQuery.php) | Workspace related filters | Filter based on the user workspaces and permissions for a defined element type (this query is added to the asset/document/data object search by default) |
| [ElementWorkspacesQuery](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/Workspaces/WorkspaceQuery.php) | Workspace related filters | Filter based on the user workspaces and permissions respecting all element types (this query is added to the element search by default) |
| [MultiSelectFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/FieldType/MultiSelectFilter.php) | Field type filters | Filter text fields by a list of exact strings. Supports [PQL field name resolution](#pql-field-name-resolution). |
| [DateFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/FieldType/DateFilter.php) | Field type filters | Filter date fields based on an exact date or a range of dates. Supports [PQL field name resolution](#pql-field-name-resolution). |
| [MultiSelectFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/FieldType/MultiSelectFilter.php) | Field type filters | Filter text fields by a list of exact strings. Supports [PQL field name resolution](#pql-field-name-resolution). |
| [DateFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Filter/FieldType/DateFilter.php) | Field type filters | Filter date fields based on an exact date or a range of dates. Supports [PQL field name resolution](#pql-field-name-resolution). |



Expand Down
44 changes: 44 additions & 0 deletions src/Model/Search/Modifier/Filter/Basic/IntegerFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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\Basic;

use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\SearchModifierInterface;

final readonly class IntegerFilter implements SearchModifierInterface
{
public function __construct(
private string $fieldName,
private int $searchTerm,
private bool $enablePqlFieldNameResolution = true,
) {
}

public function getFieldName(): string
{
return $this->fieldName;
}

public function getSearchTerm(): int
{
return $this->searchTerm;
}

public function isPqlFieldNameResolutionEnabled(): bool
{
return $this->enablePqlFieldNameResolution;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Basic\ExcludeFoldersFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Basic\IdFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Basic\IdsFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Basic\IntegerFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\SearchPqlFieldNameTransformationServiceInterface;

/**
* @internal
*/
final class BasicFilters
final readonly class BasicFilters
{
public function __construct(
private SearchPqlFieldNameTransformationServiceInterface $fieldNameTransformationService,
) {
}

#[AsSearchModifierHandler]
public function handleIdFilter(IdFilter $idFilter, SearchModifierContextInterface $context): void
{
Expand All @@ -42,6 +49,25 @@ public function handleIdFilter(IdFilter $idFilter, SearchModifierContextInterfac
);
}

#[AsSearchModifierHandler]
public function handleIntegerFilter(IntegerFilter $idFilter, SearchModifierContextInterface $context): void
{
$fieldName = $idFilter->getFieldName();
if ($idFilter->isPqlFieldNameResolutionEnabled()) {
$fieldName = $this->fieldNameTransformationService->transformFieldnameForSearch(
$context->getOriginalSearch(),
$fieldName
);
}

$context->getSearch()->addQuery(
new TermFilter(
field: $fieldName,
term: $idFilter->getSearchTerm(),
)
);
}

#[AsSearchModifierHandler]
public function handleIdsFilter(IdsFilter $idsFilter, SearchModifierContextInterface $context): void
{
Expand Down
20 changes: 20 additions & 0 deletions tests/Functional/Search/Modifier/Filter/BasicFiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Basic\ExcludeFoldersFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Basic\IdFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Basic\IdsFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Basic\IntegerFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\AssetSearchServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\SearchProviderInterface;
use Pimcore\Tests\Support\Util\TestHelper;
Expand Down Expand Up @@ -127,4 +128,23 @@ public function testIdsFilter()
$searchResult = $searchService->search($assetSearch);
$this->assertCount(0, $searchResult->getItems());
}

public function testIntegerFilter()
{
$asset = TestHelper::createImageAsset();

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

$assetSearch = $searchProvider
->createAssetSearch()
->addModifier(new IntegerFilter('system_fields.userOwner', $asset->getUserOwner()))
->addModifier(new IntegerFilter('system_fields.userModification', $asset->getUserModification()))
;
$searchResult = $searchService->search($assetSearch);
$this->assertCount(1, $searchResult->getItems());
$this->assertEquals($asset->getId(), $searchResult->getItems()[0]->getId());
}
}

0 comments on commit dfbfa25

Please sign in to comment.