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 b226b409..04efc78c 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 @@ -34,7 +34,8 @@ $search->addModifier(new ParentIdFilter(1)) | Modifier | Modifier Category | Description | |-------------------------------------------------------------------------------------------------------------------------------------------------|-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------| | [ElementKeySearch](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/FullTextSearch/ElementKeySearch.php) | Full text search | Search by element key like in the studio UI with [wildcard support](#wildcard-support). | -| [WildcardSearch](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/FullTextSearch/WildcardSearch.php) | Full text search | Filter text fields based on search terms with [wildcard support](#wildcard-support) and [PQL field name resolution support](#pql-field-name-resolution). | +| [FullTextSearch](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/FullTextSearch/FullTextSearch.php) | Full text search | Search on all element fields by value with [simple query string syntax](https://opensearch.org/docs/latest/query-dsl/full-text/simple-query-string/#simple-query-string-syntax). | +| [WildcardSearch](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/FullTextSearch/WildcardSearch.php) | Full text search | Filter text fields based on search terms with [wildcard support](#wildcard-support) and [PQL field name resolution support](#pql-field-name-resolution). | ### Dependencies diff --git a/src/Model/OpenSearch/Query/SimpleQueryStringFilter.php b/src/Model/OpenSearch/Query/SimpleQueryStringFilter.php new file mode 100644 index 00000000..c7b48c9d --- /dev/null +++ b/src/Model/OpenSearch/Query/SimpleQueryStringFilter.php @@ -0,0 +1,48 @@ +value => [ + 'simple_query_string' => [ + 'query' => $this->term, + ], + ], + ]); + } + + public function getTerm(): string + { + return $this->term; + } + + public function toArrayAsSubQuery(): array + { + return [ + 'simple_query_string' => [ + 'query' => $this->term, + ], + ]; + } +} diff --git a/src/Model/Search/Modifier/FullTextSearch/FullTextSearch.php b/src/Model/Search/Modifier/FullTextSearch/FullTextSearch.php new file mode 100644 index 00000000..0f1b8739 --- /dev/null +++ b/src/Model/Search/Modifier/FullTextSearch/FullTextSearch.php @@ -0,0 +1,32 @@ +searchTerm; + } +} diff --git a/src/SearchIndexAdapter/OpenSearch/Search/Modifier/FullTextSearch/FullTextSearchHandlers.php b/src/SearchIndexAdapter/OpenSearch/Search/Modifier/FullTextSearch/FullTextSearchHandlers.php index a3762e59..de29adf1 100644 --- a/src/SearchIndexAdapter/OpenSearch/Search/Modifier/FullTextSearch/FullTextSearchHandlers.php +++ b/src/SearchIndexAdapter/OpenSearch/Search/Modifier/FullTextSearch/FullTextSearchHandlers.php @@ -20,8 +20,10 @@ use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\FieldCategory\SystemField; use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\OpenSearch\WildcardFilterMode; use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Modifier\SearchModifierContextInterface; +use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Query\SimpleQueryStringFilter; use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Query\WildcardFilter; 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\Service\Search\SearchService\SearchPqlFieldNameTransformationServiceInterface; @@ -80,4 +82,16 @@ public function handleWildcardSearch( ) ); } + + #[AsSearchModifierHandler] + public function handleMultiMatchSearch( + FullTextSearch $fullValueSearch, + SearchModifierContextInterface $context + ): void { + if (empty($fullValueSearch->getSearchTerm())) { + return; + } + + $context->getSearch()->addQuery(new SimpleQueryStringFilter($fullValueSearch->getSearchTerm())); + } } diff --git a/tests/Functional/Search/Modifier/FullTextSearch/FullTextSearchTest.php b/tests/Functional/Search/Modifier/FullTextSearch/FullTextSearchTest.php index 5e98bae3..dd9f1ed1 100644 --- a/tests/Functional/Search/Modifier/FullTextSearch/FullTextSearchTest.php +++ b/tests/Functional/Search/Modifier/FullTextSearch/FullTextSearchTest.php @@ -17,6 +17,7 @@ namespace Pimcore\Bundle\GenericDataIndexBundle\Tests\Functional\Search\Modifier\FullTextSearch; 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\Service\Search\SearchService\Asset\AssetSearchServiceInterface; use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\SearchProviderInterface; @@ -178,4 +179,41 @@ public function testWildcardSearch(): void $searchResult = $searchService->search($assetSearch); $this->assertEquals([], $searchResult->getIds()); } + + public function testFullTextSearch(): void + { + $asset = TestHelper::createImageAsset(); + $asset->setFilename('Asset 1.jpg')->setKey('123')->save(); + $asset2 = TestHelper::createImageAsset(); + $asset2->setFilename('Asset 2.jpg')->setKey('456')->save(); + + /** @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 FullTextSearch($asset->getKey())) + ; + $this->assertEquals([$asset->getId()], $searchService->search($assetSearch)->getIds()); + + $assetSearch = $searchProvider + ->createAssetSearch() + ->addModifier(new FullTextSearch($asset2->getKey())) + ; + $this->assertEquals([$asset2->getId()], $searchService->search($assetSearch)->getIds()); + + $assetSearch = $searchProvider + ->createAssetSearch() + ->addModifier(new FullTextSearch('asset')) + ; + $this->assertEquals([$asset->getId(), $asset2->getId()], $searchService->search($assetSearch)->getIds()); + + $assetSearch = $searchProvider + ->createAssetSearch() + ->addModifier(new FullTextSearch($asset2->getFilename())) + ; + $this->assertEquals([$asset2->getId()], $searchService->search($assetSearch)->getIds()); + } }