From c78e1f679db2bcde3caecf98b5409604bac6aeb0 Mon Sep 17 00:00:00 2001 From: lukmzig Date: Tue, 5 Nov 2024 15:26:00 +0100 Subject: [PATCH 1/2] add new modifier to filter based on the integer fields --- .../05_Search_Modifiers/README.md | 5 ++- .../Modifier/Filter/Basic/IntegerFilter.php | 44 +++++++++++++++++++ .../Search/Modifier/Filter/BasicFilters.php | 28 +++++++++++- .../Modifier/Filter/BasicFiltersTest.php | 22 ++++++++++ 4 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 src/Model/Search/Modifier/Filter/Basic/IntegerFilter.php diff --git a/doc/04_Searching_For_Data_In_Index/05_Search_Modifiers/README.md b/doc/04_Searching_For_Data_In_Index/05_Search_Modifiers/README.md index 7d00e0d1..b226b409 100644 --- a/doc/04_Searching_For_Data_In_Index/05_Search_Modifiers/README.md +++ b/doc/04_Searching_For_Data_In_Index/05_Search_Modifiers/README.md @@ -16,6 +16,7 @@ $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) | @@ -23,8 +24,8 @@ $search->addModifier(new ParentIdFilter(1)) | [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). | diff --git a/src/Model/Search/Modifier/Filter/Basic/IntegerFilter.php b/src/Model/Search/Modifier/Filter/Basic/IntegerFilter.php new file mode 100644 index 00000000..0bc0e0bf --- /dev/null +++ b/src/Model/Search/Modifier/Filter/Basic/IntegerFilter.php @@ -0,0 +1,44 @@ +fieldName; + } + + public function getSearchTerm(): int + { + return $this->searchTerm; + } + + public function isPqlFieldNameResolutionEnabled(): bool + { + return $this->enablePqlFieldNameResolution; + } +} diff --git a/src/SearchIndexAdapter/OpenSearch/Search/Modifier/Filter/BasicFilters.php b/src/SearchIndexAdapter/OpenSearch/Search/Modifier/Filter/BasicFilters.php index 5783fed5..d9caba0a 100644 --- a/src/SearchIndexAdapter/OpenSearch/Search/Modifier/Filter/BasicFilters.php +++ b/src/SearchIndexAdapter/OpenSearch/Search/Modifier/Filter/BasicFilters.php @@ -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 { @@ -41,6 +48,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 diff --git a/tests/Functional/Search/Modifier/Filter/BasicFiltersTest.php b/tests/Functional/Search/Modifier/Filter/BasicFiltersTest.php index 0743a293..770319a8 100644 --- a/tests/Functional/Search/Modifier/Filter/BasicFiltersTest.php +++ b/tests/Functional/Search/Modifier/Filter/BasicFiltersTest.php @@ -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; @@ -127,4 +128,25 @@ 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()); + } } From 0ce31b1c1939c7b037611536767f4bde82b2ebb9 Mon Sep 17 00:00:00 2001 From: lukmzig Date: Tue, 5 Nov 2024 14:29:20 +0000 Subject: [PATCH 2/2] Apply php-cs-fixer changes --- .../OpenSearch/Search/Modifier/Filter/BasicFilters.php | 2 +- tests/Functional/Search/Modifier/Filter/BasicFiltersTest.php | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/SearchIndexAdapter/OpenSearch/Search/Modifier/Filter/BasicFilters.php b/src/SearchIndexAdapter/OpenSearch/Search/Modifier/Filter/BasicFilters.php index d9caba0a..6a3a9b05 100644 --- a/src/SearchIndexAdapter/OpenSearch/Search/Modifier/Filter/BasicFilters.php +++ b/src/SearchIndexAdapter/OpenSearch/Search/Modifier/Filter/BasicFilters.php @@ -48,7 +48,7 @@ public function handleIdFilter(IdFilter $idFilter, SearchModifierContextInterfac ) ); } - + #[AsSearchModifierHandler] public function handleIntegerFilter(IntegerFilter $idFilter, SearchModifierContextInterface $context): void { diff --git a/tests/Functional/Search/Modifier/Filter/BasicFiltersTest.php b/tests/Functional/Search/Modifier/Filter/BasicFiltersTest.php index 770319a8..f9eb1229 100644 --- a/tests/Functional/Search/Modifier/Filter/BasicFiltersTest.php +++ b/tests/Functional/Search/Modifier/Filter/BasicFiltersTest.php @@ -129,8 +129,6 @@ public function testIdsFilter() $this->assertCount(0, $searchResult->getItems()); } - - public function testIntegerFilter() { $asset = TestHelper::createImageAsset();