Skip to content

Commit ab1f79b

Browse files
authored
Update Bool filter to check if the boolean field is empty (#342)
* Update Bool filter to check if the boolean field is empty * Remove Test * Tests * Remove Tests again * Fix Logic * Apply php-cs-fixer changes * Fix Logic * Apply php-cs-fixer changes --------- Co-authored-by: martineiber <[email protected]>
1 parent f1ca3ca commit ab1f79b

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* This source file is available under the terms of the
6+
* Pimcore Open Core License (POCL)
7+
* Full copyright and license information is available in
8+
* LICENSE.md which is distributed with this source code.
9+
*
10+
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
11+
* @license Pimcore Open Core License (POCL)
12+
*/
13+
14+
namespace Pimcore\Bundle\GenericDataIndexBundle\Model\DefaultSearch\Query;
15+
16+
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\DefaultSearch\ConditionType;
17+
18+
final class BoolExistsQuery extends BoolQuery implements AsSubQueryInterface
19+
{
20+
public function __construct(
21+
private readonly string $field
22+
) {
23+
parent::__construct([
24+
ConditionType::FILTER->value => [
25+
'bool' => [
26+
'must_not' => [
27+
'exists' => ['field' => $field],
28+
],
29+
],
30+
],
31+
]);
32+
}
33+
34+
public function getField(): string
35+
{
36+
return $this->field;
37+
}
38+
39+
public function toArrayAsSubQuery(): array
40+
{
41+
return [
42+
'bool' => [
43+
'must_not' => [
44+
'exists' => ['field' => $this->field],
45+
],
46+
],
47+
];
48+
}
49+
}

src/Model/Search/Modifier/Filter/Basic/BooleanFilter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
{
2020
public function __construct(
2121
private string $fieldName,
22-
private bool $searchTerm,
22+
private null|bool $searchTerm,
2323
private bool $enablePqlFieldNameResolution = true,
2424
) {
2525
}
@@ -29,7 +29,7 @@ public function getFieldName(): string
2929
return $this->fieldName;
3030
}
3131

32-
public function getSearchTerm(): bool
32+
public function getSearchTerm(): null|bool
3333
{
3434
return $this->searchTerm;
3535
}

src/SearchIndexAdapter/DefaultSearch/Search/Modifier/Filter/BasicFilters.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Pimcore\Bundle\GenericDataIndexBundle\Attribute\Search\AsSearchModifierHandler;
1717
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\FieldCategory\SystemField;
1818
use Pimcore\Bundle\GenericDataIndexBundle\Model\DefaultSearch\Modifier\SearchModifierContextInterface;
19+
use Pimcore\Bundle\GenericDataIndexBundle\Model\DefaultSearch\Query\BoolExistsQuery;
1920
use Pimcore\Bundle\GenericDataIndexBundle\Model\DefaultSearch\Query\BoolQuery;
2021
use Pimcore\Bundle\GenericDataIndexBundle\Model\DefaultSearch\Query\TermFilter;
2122
use Pimcore\Bundle\GenericDataIndexBundle\Model\DefaultSearch\Query\TermsFilter;
@@ -77,11 +78,19 @@ public function handleBooleanFilter(BooleanFilter $booleanFilter, SearchModifier
7778
);
7879
}
7980

80-
$context->getSearch()->addQuery(
81-
new TermFilter(
81+
$query = new BoolExistsQuery(
82+
field: $fieldName,
83+
);
84+
85+
if ($booleanFilter->getSearchTerm() !== null) {
86+
$query = new TermFilter(
8287
field: $fieldName,
8388
term: $booleanFilter->getSearchTerm(),
84-
)
89+
);
90+
}
91+
92+
$context->getSearch()->addQuery(
93+
$query
8594
);
8695
}
8796

0 commit comments

Comments
 (0)