From 2e8578e71aec3b44aed931049c7d64186b93e328 Mon Sep 17 00:00:00 2001 From: Martin Eiber Date: Thu, 7 Nov 2024 11:50:21 +0100 Subject: [PATCH] [Grid] [Filter] Add Fulltext Search Filter (#543) * Add Fulltext search filter. * Update docs * Apply php-cs-fixer changes * Add test. * Apply php-cs-fixer changes --------- Co-authored-by: martineiber --- config/data_index_filters.yaml | 3 + doc/03_Grid.md | 1 + src/DataIndex/Filter/FullTextFilter.php | 50 ++++++++++++++ src/DataIndex/Query/AssetQuery.php | 8 +++ src/DataIndex/Query/DataObjectQuery.php | 8 +++ src/DataIndex/Query/DocumentQuery.php | 8 +++ src/DataIndex/Query/QueryInterface.php | 2 + src/Grid/Column/ColumnType.php | 1 + .../DataIndex/Filter/FullTextFilterTest.php | 66 +++++++++++++++++++ 9 files changed, 147 insertions(+) create mode 100644 src/DataIndex/Filter/FullTextFilter.php create mode 100644 tests/Unit/DataIndex/Filter/FullTextFilterTest.php diff --git a/config/data_index_filters.yaml b/config/data_index_filters.yaml index 6879e0e1..0294e7b1 100644 --- a/config/data_index_filters.yaml +++ b/config/data_index_filters.yaml @@ -47,6 +47,9 @@ services: Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\IntegerFilter: tags: [ 'pimcore.studio_backend.open_search.filter' ] + Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\FullTextFilter: + tags: [ 'pimcore.studio_backend.open_search.filter' ] + # DataObject Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\DataObject\ClassNameFilter: tags: [ 'pimcore.studio_backend.open_search.data_object.filter' ] diff --git a/doc/03_Grid.md b/doc/03_Grid.md index 7aa1d575..4d960856 100644 --- a/doc/03_Grid.md +++ b/doc/03_Grid.md @@ -45,6 +45,7 @@ Available filters are: | system.pql | string | PQL Query | false | | system.id | integer | | false | | system.integer | integer | | true | +| system.fulltext | string | | false | diff --git a/src/DataIndex/Filter/FullTextFilter.php b/src/DataIndex/Filter/FullTextFilter.php new file mode 100644 index 00000000..44ab6126 --- /dev/null +++ b/src/DataIndex/Filter/FullTextFilter.php @@ -0,0 +1,50 @@ +getSimpleColumnFilterByType(ColumnType::SYSTEM_FULLTEXT->value); + + if (!$filter) { + return $query; + } + + if (!is_string($filter->getFilterValue())) { + throw new InvalidArgumentException('Filter value for the fulltext filter must be a string'); + } + + $query->filterFullText($filter->getFilterValue()); + + return $query; + } +} diff --git a/src/DataIndex/Query/AssetQuery.php b/src/DataIndex/Query/AssetQuery.php index b969ef5c..145689c3 100644 --- a/src/DataIndex/Query/AssetQuery.php +++ b/src/DataIndex/Query/AssetQuery.php @@ -27,6 +27,7 @@ use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Tree\PathFilter; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Tree\TagFilter; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\FullTextSearch\ElementKeySearch; +use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\FullTextSearch\FullTextSearch; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\FullTextSearch\WildcardSearch; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\QueryLanguage\PqlFilter; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\OrderByField; @@ -182,4 +183,11 @@ public function filterInteger(string $field, int $value): QueryInterface return $this; } + + public function filterFullText(string $value): QueryInterface + { + $this->search->addModifier(new FullTextSearch($value)); + + return $this; + } } diff --git a/src/DataIndex/Query/DataObjectQuery.php b/src/DataIndex/Query/DataObjectQuery.php index da05d229..cad9da7e 100644 --- a/src/DataIndex/Query/DataObjectQuery.php +++ b/src/DataIndex/Query/DataObjectQuery.php @@ -26,6 +26,7 @@ use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Tree\PathFilter; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Tree\TagFilter; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\FullTextSearch\ElementKeySearch; +use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\FullTextSearch\FullTextSearch; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\QueryLanguage\PqlFilter; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\Tree\OrderByFullPath; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\Tree\OrderByIndexField; @@ -161,4 +162,11 @@ public function filterInteger(string $field, int $value): QueryInterface return $this; } + + public function filterFullText(string $value): QueryInterface + { + $this->search->addModifier(new FullTextSearch($value)); + + return $this; + } } diff --git a/src/DataIndex/Query/DocumentQuery.php b/src/DataIndex/Query/DocumentQuery.php index 4fd4f85d..e6b1ffd6 100644 --- a/src/DataIndex/Query/DocumentQuery.php +++ b/src/DataIndex/Query/DocumentQuery.php @@ -26,6 +26,7 @@ use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Tree\PathFilter; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Tree\TagFilter; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\FullTextSearch\ElementKeySearch; +use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\FullTextSearch\FullTextSearch; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\FullTextSearch\WildcardSearch; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\QueryLanguage\PqlFilter; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\OrderByField; @@ -161,4 +162,11 @@ public function filterInteger(string $field, int $value): QueryInterface return $this; } + + public function filterFullText(string $value): QueryInterface + { + $this->search->addModifier(new FullTextSearch($value)); + + return $this; + } } diff --git a/src/DataIndex/Query/QueryInterface.php b/src/DataIndex/Query/QueryInterface.php index 5a94aab5..18cfb720 100644 --- a/src/DataIndex/Query/QueryInterface.php +++ b/src/DataIndex/Query/QueryInterface.php @@ -49,4 +49,6 @@ public function filterByPql(string $pqlQuery): self; public function setUser(UserInterface $user): self; public function filterInteger(string $field, int $value): self; + + public function filterFullText(string $value): self; } diff --git a/src/Grid/Column/ColumnType.php b/src/Grid/Column/ColumnType.php index fec5ee0a..61dca753 100644 --- a/src/Grid/Column/ColumnType.php +++ b/src/Grid/Column/ColumnType.php @@ -28,6 +28,7 @@ enum ColumnType: string case SYSTEM_DATETIME = 'system.datetime'; case SYSTEM_TAG = 'system.tag'; case SYSTEM_PQL_QUERY = 'system.pql'; + case SYSTEM_FULLTEXT = 'system.fulltext'; case METADATA_SELECT = 'metadata.select'; case METADATA_INPUT = 'metadata.input'; case METADATA_DATE = 'metadata.date'; diff --git a/tests/Unit/DataIndex/Filter/FullTextFilterTest.php b/tests/Unit/DataIndex/Filter/FullTextFilterTest.php new file mode 100644 index 00000000..de03ae7e --- /dev/null +++ b/tests/Unit/DataIndex/Filter/FullTextFilterTest.php @@ -0,0 +1,66 @@ +makeEmpty(SimpleColumnFiltersParameterInterface::class, [ + 'getSimpleColumnFilterByType' => $columnFilter, + ]); + + $query = $this->makeEmpty(QueryInterface::class); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Filter value for the fulltext filter must be a string'); + + $filter = new FullTextFilter(); + $filter->apply($parameter, $query); + } + + public function testIfFilterFullTextIsCalled(): void + { + $columnFilter = new SimpleColumnFilter('system.fulltext', 'term'); + $parameter = $this->makeEmpty(SimpleColumnFiltersParameterInterface::class, [ + 'getSimpleColumnFilterByType' => $columnFilter, + ]); + + $query = $this->makeEmpty(QueryInterface::class, [ + 'filterFullText' => Expected::once(function ($term) { + $this->assertSame('term', $term); + + return $this->makeEmpty(QueryInterface::class); + }), + ]); + + $filter = new FullTextFilter(); + $filter->apply($parameter, $query); + } +}