-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EZP-30951: Added missing support for searching (Not)Empty Field values (
#157) * Added missing features for Empty and Null field values * Fixups after CR * fixups * Removed changes for Null Searching * Fixups after CR * CS Fixups * Fixups after CR * Small change for FieldEmpty Vistor * Changes after CR * Fixups after CR * Updated composer.json kernel requirement
- Loading branch information
1 parent
27ca8b1
commit 0a9da38
Showing
5 changed files
with
222 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
lib/FieldMapper/ContentTranslationFieldMapper/ContentDocumentEmptyFields.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace EzSystems\EzPlatformSolrSearchEngine\FieldMapper\ContentTranslationFieldMapper; | ||
|
||
use eZ\Publish\Core\Persistence\FieldTypeRegistry; | ||
use eZ\Publish\Core\Search\Common\FieldNameGenerator; | ||
use eZ\Publish\SPI\Persistence\Content; | ||
use eZ\Publish\SPI\Persistence\Content\Type\Handler as ContentTypeHandler; | ||
use eZ\Publish\SPI\Search\Field; | ||
use eZ\Publish\SPI\Search\FieldType; | ||
use EzSystems\EzPlatformSolrSearchEngine\FieldMapper\ContentTranslationFieldMapper; | ||
|
||
/** | ||
* Indexes information on whether Content field is empty. | ||
*/ | ||
class ContentDocumentEmptyFields extends ContentTranslationFieldMapper | ||
{ | ||
public const IS_EMPTY_NAME = 'is_empty'; | ||
|
||
/** | ||
* @var \eZ\Publish\SPI\Persistence\Content\Type\Handler | ||
*/ | ||
private $contentTypeHandler; | ||
|
||
/** | ||
* @var \eZ\Publish\Core\Search\Common\FieldNameGenerator | ||
*/ | ||
private $fieldNameGenerator; | ||
|
||
/** | ||
* @var \eZ\Publish\Core\Persistence\FieldTypeRegistry | ||
*/ | ||
private $fieldTypeRegistry; | ||
|
||
/** | ||
* @param \eZ\Publish\SPI\Persistence\Content\Type\Handler $contentTypeHandler | ||
* @param \eZ\Publish\Core\Search\Common\FieldNameGenerator $fieldNameGenerator | ||
* @param \eZ\Publish\Core\Persistence\FieldTypeRegistry $fieldTypeRegistry | ||
*/ | ||
public function __construct( | ||
ContentTypeHandler $contentTypeHandler, | ||
FieldNameGenerator $fieldNameGenerator, | ||
FieldTypeRegistry $fieldTypeRegistry | ||
) { | ||
$this->contentTypeHandler = $contentTypeHandler; | ||
$this->fieldNameGenerator = $fieldNameGenerator; | ||
$this->fieldTypeRegistry = $fieldTypeRegistry; | ||
} | ||
|
||
/** | ||
* @param \eZ\Publish\SPI\Persistence\Content $content | ||
* @param string $languageCode | ||
* | ||
* @return bool | ||
*/ | ||
public function accept(Content $content, $languageCode) | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @param \eZ\Publish\SPI\Persistence\Content $content | ||
* @param string $languageCode | ||
* | ||
* @return \eZ\Publish\SPI\Search\Field[] | ||
*/ | ||
public function mapFields(Content $content, $languageCode) | ||
{ | ||
$fields = []; | ||
$contentType = $this->contentTypeHandler->load( | ||
$content->versionInfo->contentInfo->contentTypeId | ||
); | ||
|
||
foreach ($content->fields as $field) { | ||
if ($field->languageCode !== $languageCode) { | ||
continue; | ||
} | ||
|
||
foreach ($contentType->fieldDefinitions as $fieldDefinition) { | ||
if ($fieldDefinition->isRequired) { | ||
continue; | ||
} | ||
if ($fieldDefinition->id !== $field->fieldDefinitionId) { | ||
continue; | ||
} | ||
|
||
/** @var \eZ\Publish\Core\Persistence\FieldType $fieldType */ | ||
$fieldType = $this->fieldTypeRegistry->getFieldType($fieldDefinition->fieldType); | ||
$fields[] = new Field( | ||
$name = $this->fieldNameGenerator->getName( | ||
self::IS_EMPTY_NAME, | ||
$fieldDefinition->identifier | ||
), | ||
$fieldType->isEmptyValue($field->value), | ||
new FieldType\BooleanField() | ||
); | ||
} | ||
} | ||
|
||
return $fields; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace EzSystems\EzPlatformSolrSearchEngine\Query\Common\CriterionVisitor\Field; | ||
|
||
use eZ\Publish\SPI\Search\FieldType\BooleanField; | ||
use EzSystems\EzPlatformSolrSearchEngine\FieldMapper\ContentTranslationFieldMapper\ContentDocumentEmptyFields; | ||
use EzSystems\EzPlatformSolrSearchEngine\Query\CriterionVisitor; | ||
use EzSystems\EzPlatformSolrSearchEngine\Query\Common\CriterionVisitor\Field; | ||
use eZ\Publish\API\Repository\Values\Content\Query\Criterion; | ||
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; | ||
use eZ\Publish\Core\Search\Common\FieldValueMapper; | ||
use eZ\Publish\Core\Search\Common\FieldNameResolver; | ||
use eZ\Publish\Core\Search\Common\FieldNameGenerator; | ||
|
||
/** | ||
* Visits the IsFieldEmpty criterion. | ||
*/ | ||
final class FieldEmpty extends Field | ||
{ | ||
/** | ||
* @var \eZ\Publish\Core\Search\Common\FieldNameGenerator | ||
*/ | ||
private $fieldNameGenerator; | ||
|
||
/** | ||
* @param \eZ\Publish\Core\Search\Common\FieldNameResolver $fieldNameResolver | ||
* @param \eZ\Publish\Core\Search\Common\FieldValueMapper $fieldValueMapper | ||
* @param \eZ\Publish\Core\Search\Common\FieldNameGenerator $fieldNameGenerator | ||
*/ | ||
public function __construct( | ||
FieldNameResolver $fieldNameResolver, | ||
FieldValueMapper $fieldValueMapper, | ||
FieldNameGenerator $fieldNameGenerator | ||
) { | ||
parent::__construct($fieldNameResolver, $fieldValueMapper); | ||
|
||
$this->fieldNameGenerator = $fieldNameGenerator; | ||
} | ||
|
||
/** | ||
* Check if visitor is applicable to current criterion. | ||
*/ | ||
public function canVisit(Criterion $criterion): bool | ||
{ | ||
return $criterion instanceof Criterion\IsFieldEmpty; | ||
} | ||
|
||
/** | ||
* Map field value to a proper Solr representation. | ||
* | ||
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If no searchable fields are found for the given criterion target. | ||
* | ||
* @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion | ||
* @param \EzSystems\EzPlatformSolrSearchEngine\Query\CriterionVisitor $subVisitor | ||
* | ||
* @return string | ||
*/ | ||
public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null): string | ||
{ | ||
$searchFields = $this->getSearchFields($criterion); | ||
|
||
if (empty($searchFields)) { | ||
throw new InvalidArgumentException( | ||
'$criterion->target', | ||
"No searchable fields found for the given criterion target '{$criterion->target}'." | ||
); | ||
} | ||
|
||
$criterion->value = (array)$criterion->value; | ||
$queries = []; | ||
|
||
foreach ($searchFields as $name => $fieldType) { | ||
foreach ($criterion->value as $value) { | ||
$name = $this->fieldNameGenerator->getTypedName( | ||
$this->fieldNameGenerator->getName( | ||
ContentDocumentEmptyFields::IS_EMPTY_NAME, | ||
$criterion->target | ||
), | ||
new BooleanField() | ||
); | ||
$queries[] = $name . ':' . (int) $value; | ||
} | ||
} | ||
|
||
return '(' . implode(' OR ', $queries) . ')'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters