Skip to content

Commit

Permalink
add new modifier to filter based on the value across all fields (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukmzig authored Nov 7, 2024
1 parent dfbfa25 commit b7c9ea2
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions src/Model/OpenSearch/Query/SimpleQueryStringFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\OpenSearch\Query;

use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\OpenSearch\ConditionType;

final class SimpleQueryStringFilter extends BoolQuery implements AsSubQueryInterface
{
public function __construct(
private readonly string $term
) {
parent::__construct([
ConditionType::FILTER->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,
],
];
}
}
32 changes: 32 additions & 0 deletions src/Model/Search/Modifier/FullTextSearch/FullTextSearch.php
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\FullTextSearch;

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

final readonly class FullTextSearch implements SearchModifierInterface
{
public function __construct(
private string $searchTerm
) {
}

public function getSearchTerm(): string
{
return $this->searchTerm;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit b7c9ea2

Please sign in to comment.