Skip to content

Commit

Permalink
Handled different coverage of field criteria across search engines
Browse files Browse the repository at this point in the history
The Legacy and Solr search engines don't support the same fields as field criteria.
This delegates checking for that to SearchFeature objects specialized depending on the engine type.
  • Loading branch information
Bertrand Dunogier committed Jan 24, 2019
1 parent 6a6073f commit c851ec1
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/DependencyInjection/Factory/SearchFeaturesFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace EzSystems\EzPlatformGraphQL\DependencyInjection\Factory;

use eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider;

class SearchFeaturesFactory
{
/**
* @var \eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider
*/
private $configurationProvider;

/**
* @var \EzSystems\EzPlatformGraphQL\Search\SearchFeatures[]
*/
private $searchFeatures = [];

public function __construct(RepositoryConfigurationProvider $configurationProvider, array $searchFeatures)
{
$this->configurationProvider = $configurationProvider;
$this->searchFeatures = $searchFeatures;
}

public function build()
{
$searchEngine = $this->configurationProvider->getRepositoryConfig()['search']['engine'];

if (isset($this->searchFeatures[$searchEngine])) {
return $this->searchFeatures[$searchEngine];
} else {
throw new \InvalidArgumentException("Search engine not found");
}
}
}
17 changes: 17 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,20 @@ services:
- { name: "overblog_graphql.mutation", alias: "DeleteSection", method: "deleteSection" }

EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\SearchQueryMapper: ~

EzSystems\EzPlatformGraphQL\DependencyInjection\Factory\SearchFeaturesFactory:
arguments:
$configurationProvider: '@ezpublish.api.repository_configuration_provider'
$searchFeatures:
solr: '@EzSystems\EzPlatformGraphQL\Search\SolrSearchFeatures'
legacy: '@EzSystems\EzPlatformGraphQL\Search\LegacySearchFeatures'

EzSystems\EzPlatformGraphQL\Search\SearchFeatures:
factory: ['@EzSystems\EzPlatformGraphQL\DependencyInjection\Factory\SearchFeaturesFactory', build]

EzSystems\EzPlatformGraphQL\Search\LegacySearchFeatures:
arguments:
$converterRegistry: '@ezpublish.persistence.legacy.field_value_converter.registry'

EzSystems\EzPlatformGraphQL\Search\SolrSearchFeatures: ~

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
namespace EzSystems\EzPlatformGraphQL\Schema\Domain\Content\Worker\FieldDefinition;

use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
use EzSystems\EzPlatformGraphQL\Schema\Builder;
use EzSystems\EzPlatformGraphQL\Schema\Domain\Content\Worker\BaseWorker;
use EzSystems\EzPlatformGraphQL\Schema\Worker;
use EzSystems\EzPlatformGraphQL\Search\SearchFeatures;

/**
* Adds the field definition, if it is searchable, as a filter on the type's collection.
*/
class AddFieldDefinitionToCollectionFilters extends BaseWorker implements Worker
{
/**
* @var SearchFeatures
*/
private $searchFeatures;

public function __construct(SearchFeatures $searchFeatures)
{
$this->searchFeatures = $searchFeatures;
}

public function work(Builder $schema, array $args)
{
$domainGroupName = $this->getNameHelper()->domainGroupName($args['ContentTypeGroup']);
$domainContentCollectionField = $this->getNameHelper()->domainContentCollectionField($args['ContentType']);
$fieldDefinitionField = $this->getNameHelper()->fieldDefinitionField($args['FieldDefinition']);

$schema->addFieldToType(
$domainGroupName,
new Builder\Input\Field(
$domainContentCollectionField,
$this->getFilterType($args['FieldDefinition']),
['description' => 'Filter content based on the ' . $args['FieldDefinition']->identifier . ' field']
)
);
}

public function canWork(Builder $schema, array $args)
{
return
isset($args['FieldDefinition'])
&& $args['FieldDefinition'] instanceof FieldDefinition
& isset($args['ContentType'])
&& $args['ContentType'] instanceof ContentType
&& $this->searchFeatures->supportsFieldCriterion($args['FieldDefinition']);
}

/**
* @param ContentType $contentType
* @return string
*/
protected function getDomainContentName(ContentType $contentType): string
{
return $this->getNameHelper()->domainContentName($contentType);
}

/**
* @param FieldDefinition $fieldDefinition
* @return string
*/
protected function getFieldDefinitionField(FieldDefinition $fieldDefinition): string
{
return $this->getNameHelper()->fieldDefinitionField($fieldDefinition);
}

private function isSearchable(FieldDefinition $fieldDefinition): bool
{
return $fieldDefinition->isSearchable
// should only be verified if legacy is the current search engine
&& $this->converterRegistry->getConverter($fieldDefinition->fieldTypeIdentifier)->getIndexColumn() !== false;
}

private function getFilterType(FieldDefinition $fieldDefinition)
{
switch ($fieldDefinition->fieldTypeIdentifier)
{
case 'ezboolean':
return 'Boolean';
default:
return 'String';
}
}
}
23 changes: 23 additions & 0 deletions src/Search/LegacySearchFeatures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace EzSystems\EzPlatformGraphQL\Search;

use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry;

class LegacySearchFeatures implements SearchFeatures
{
/**
* @var \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry
*/
private $converterRegistry;

public function __construct(ConverterRegistry $converterRegistry)
{
$this->converterRegistry = $converterRegistry;
}

public function supportsFieldCriterion(FieldDefinition $fieldDefinition)
{
return $this->converterRegistry->getConverter($fieldDefinition->fieldTypeIdentifier)->getIndexColumn() !== false;
}
}
16 changes: 16 additions & 0 deletions src/Search/SearchFeatures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace EzSystems\EzPlatformGraphQL\Search;

use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;

interface SearchFeatures
{
/**
* Tests if search supports field filtering on $fieldDefinition.
*
* @param FieldDefinition $fieldDefinition
*
* @return bool
*/
public function supportsFieldCriterion(FieldDefinition $fieldDefinition);
}
12 changes: 12 additions & 0 deletions src/Search/SolrSearchFeatures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace EzSystems\EzPlatformGraphQL\Search;

use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;

class SolrSearchFeatures implements SearchFeatures
{
public function supportsFieldCriterion(FieldDefinition $fieldDefinition)
{
return true;
}
}

0 comments on commit c851ec1

Please sign in to comment.