Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions doc/04_Searching_For_Data_In_Index/05_Search_Modifiers/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ If you want to use the Pimcore Query Language (PQL) as a developer to search for

#### 1. Search Modifier for the Generic Data Index search services

You can use the [PqlFilter](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/QueryLanguage/PqlFilter.php) search modifier to filter search results based on a PQL query. The `PqlFilter` search modifier can be used with the search services provided by the Generic Data Index bundle. Take a look at the [Search Services](../README.md) documentation for details.
You can use the [PqlFilter](https://github.com/pimcore/generic-data-index-bundle/blob/2.0/src/Model/Search/Modifier/QueryLanguage/PqlFilter.php) search modifier to filter search results based on a PQL query. The `PqlFilter` search modifier can be used with the search services provided by the Generic Data Index bundle. Take a look at the [Search Services](../README.md) documentation for details.

#### 2. Direct use of the PQL processor to get the search query

Expand Down
2 changes: 1 addition & 1 deletion doc/04_Searching_For_Data_In_Index/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The Generic Data Index bundle adds standardized and flexible services to search data from the search indices.

Each search is based on a search service (depending on the element type) and a search model defining the search query. The search models can be created with the [SearchProviderInterface](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Service/Search/SearchService/SearchProviderInterface.php)
Each search is based on a search service (depending on the element type) and a search model defining the search query. The search models can be created with the [SearchProviderInterface](https://github.com/pimcore/generic-data-index-bundle/blob/2.0/src/Service/Search/SearchService/SearchProviderInterface.php)

The regular way to search for assets, data objects or documents is to use the related search service.

Expand Down
41 changes: 41 additions & 0 deletions src/Model/Search/Modifier/Filter/Basic/BooleanFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);

/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
* @license Pimcore Open Core License (POCL)
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Basic;

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

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

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

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

public function isPqlFieldNameResolutionEnabled(): bool
{
return $this->enablePqlFieldNameResolution;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Pimcore\Bundle\GenericDataIndexBundle\Model\DefaultSearch\Query\BoolQuery;
use Pimcore\Bundle\GenericDataIndexBundle\Model\DefaultSearch\Query\TermFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\DefaultSearch\Query\TermsFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Basic\BooleanFilter;
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;
Expand Down Expand Up @@ -47,10 +48,10 @@ public function handleIdFilter(IdFilter $idFilter, SearchModifierContextInterfac
}

#[AsSearchModifierHandler]
public function handleIntegerFilter(IntegerFilter $idFilter, SearchModifierContextInterface $context): void
public function handleIntegerFilter(IntegerFilter $integerFilter, SearchModifierContextInterface $context): void
{
$fieldName = $idFilter->getFieldName();
if ($idFilter->isPqlFieldNameResolutionEnabled()) {
$fieldName = $integerFilter->getFieldName();
if ($integerFilter->isPqlFieldNameResolutionEnabled()) {
$fieldName = $this->fieldNameTransformationService->transformFieldnameForSearch(
$context->getOriginalSearch(),
$fieldName
Expand All @@ -60,7 +61,26 @@ public function handleIntegerFilter(IntegerFilter $idFilter, SearchModifierConte
$context->getSearch()->addQuery(
new TermFilter(
field: $fieldName,
term: $idFilter->getSearchTerm(),
term: $integerFilter->getSearchTerm(),
)
);
}

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

$context->getSearch()->addQuery(
new TermFilter(
field: $fieldName,
term: $booleanFilter->getSearchTerm(),
)
);
}
Expand Down
29 changes: 29 additions & 0 deletions tests/Functional/Search/Modifier/Filter/BasicFiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@

namespace Pimcore\Bundle\GenericDataIndexBundle\Tests\Functional\Search\Modifier\Filter;

use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Basic\BooleanFilter;
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\DataObject\DataObjectSearchServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\SearchProviderInterface;
use Pimcore\Tests\Support\Util\TestHelper;

Expand Down Expand Up @@ -144,4 +146,31 @@ public function testIntegerFilter()
$this->assertCount(1, $searchResult->getItems());
$this->assertEquals($asset->getId(), $searchResult->getItems()[0]->getId());
}

public function testBooleanFilter()
{
$objects = TestHelper::createEmptyObjects(count: 3);
$objects[1]->setPublished(false)->save();
$objects[2]->setPublished(false)->save();

/** @var DataObjectSearchServiceInterface $searchService */
$searchService = $this->tester->grabService(DataObjectSearchServiceInterface::class);
/** @var SearchProviderInterface $searchProvider */
$searchProvider = $this->tester->grabService(SearchProviderInterface::class);

$search = $searchProvider
->createDataObjectSearch()
->addModifier(new BooleanFilter('system_fields.published', true))
;
$searchResult = $searchService->search($search);
$this->assertCount(1, $searchResult->getItems());
$this->assertEquals($objects[0]->getId(), $searchResult->getItems()[0]->getId());

$search = $searchProvider
->createDataObjectSearch()
->addModifier(new BooleanFilter('system_fields.published', false))
;
$searchResult = $searchService->search($search);
$this->assertCount(2, $searchResult->getItems());
}
}