Skip to content

Commit c851ec1

Browse files
author
Bertrand Dunogier
committed
Handled different coverage of field criteria across search engines
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.
1 parent 6a6073f commit c851ec1

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace EzSystems\EzPlatformGraphQL\DependencyInjection\Factory;
3+
4+
use eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider;
5+
6+
class SearchFeaturesFactory
7+
{
8+
/**
9+
* @var \eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider
10+
*/
11+
private $configurationProvider;
12+
13+
/**
14+
* @var \EzSystems\EzPlatformGraphQL\Search\SearchFeatures[]
15+
*/
16+
private $searchFeatures = [];
17+
18+
public function __construct(RepositoryConfigurationProvider $configurationProvider, array $searchFeatures)
19+
{
20+
$this->configurationProvider = $configurationProvider;
21+
$this->searchFeatures = $searchFeatures;
22+
}
23+
24+
public function build()
25+
{
26+
$searchEngine = $this->configurationProvider->getRepositoryConfig()['search']['engine'];
27+
28+
if (isset($this->searchFeatures[$searchEngine])) {
29+
return $this->searchFeatures[$searchEngine];
30+
} else {
31+
throw new \InvalidArgumentException("Search engine not found");
32+
}
33+
}
34+
}

src/Resources/config/services.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,20 @@ services:
2020
- { name: "overblog_graphql.mutation", alias: "DeleteSection", method: "deleteSection" }
2121

2222
EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\SearchQueryMapper: ~
23+
24+
EzSystems\EzPlatformGraphQL\DependencyInjection\Factory\SearchFeaturesFactory:
25+
arguments:
26+
$configurationProvider: '@ezpublish.api.repository_configuration_provider'
27+
$searchFeatures:
28+
solr: '@EzSystems\EzPlatformGraphQL\Search\SolrSearchFeatures'
29+
legacy: '@EzSystems\EzPlatformGraphQL\Search\LegacySearchFeatures'
30+
31+
EzSystems\EzPlatformGraphQL\Search\SearchFeatures:
32+
factory: ['@EzSystems\EzPlatformGraphQL\DependencyInjection\Factory\SearchFeaturesFactory', build]
33+
34+
EzSystems\EzPlatformGraphQL\Search\LegacySearchFeatures:
35+
arguments:
36+
$converterRegistry: '@ezpublish.persistence.legacy.field_value_converter.registry'
37+
38+
EzSystems\EzPlatformGraphQL\Search\SolrSearchFeatures: ~
39+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
namespace EzSystems\EzPlatformGraphQL\Schema\Domain\Content\Worker\FieldDefinition;
3+
4+
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
5+
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
6+
use EzSystems\EzPlatformGraphQL\Schema\Builder;
7+
use EzSystems\EzPlatformGraphQL\Schema\Domain\Content\Worker\BaseWorker;
8+
use EzSystems\EzPlatformGraphQL\Schema\Worker;
9+
use EzSystems\EzPlatformGraphQL\Search\SearchFeatures;
10+
11+
/**
12+
* Adds the field definition, if it is searchable, as a filter on the type's collection.
13+
*/
14+
class AddFieldDefinitionToCollectionFilters extends BaseWorker implements Worker
15+
{
16+
/**
17+
* @var SearchFeatures
18+
*/
19+
private $searchFeatures;
20+
21+
public function __construct(SearchFeatures $searchFeatures)
22+
{
23+
$this->searchFeatures = $searchFeatures;
24+
}
25+
26+
public function work(Builder $schema, array $args)
27+
{
28+
$domainGroupName = $this->getNameHelper()->domainGroupName($args['ContentTypeGroup']);
29+
$domainContentCollectionField = $this->getNameHelper()->domainContentCollectionField($args['ContentType']);
30+
$fieldDefinitionField = $this->getNameHelper()->fieldDefinitionField($args['FieldDefinition']);
31+
32+
$schema->addFieldToType(
33+
$domainGroupName,
34+
new Builder\Input\Field(
35+
$domainContentCollectionField,
36+
$this->getFilterType($args['FieldDefinition']),
37+
['description' => 'Filter content based on the ' . $args['FieldDefinition']->identifier . ' field']
38+
)
39+
);
40+
}
41+
42+
public function canWork(Builder $schema, array $args)
43+
{
44+
return
45+
isset($args['FieldDefinition'])
46+
&& $args['FieldDefinition'] instanceof FieldDefinition
47+
& isset($args['ContentType'])
48+
&& $args['ContentType'] instanceof ContentType
49+
&& $this->searchFeatures->supportsFieldCriterion($args['FieldDefinition']);
50+
}
51+
52+
/**
53+
* @param ContentType $contentType
54+
* @return string
55+
*/
56+
protected function getDomainContentName(ContentType $contentType): string
57+
{
58+
return $this->getNameHelper()->domainContentName($contentType);
59+
}
60+
61+
/**
62+
* @param FieldDefinition $fieldDefinition
63+
* @return string
64+
*/
65+
protected function getFieldDefinitionField(FieldDefinition $fieldDefinition): string
66+
{
67+
return $this->getNameHelper()->fieldDefinitionField($fieldDefinition);
68+
}
69+
70+
private function isSearchable(FieldDefinition $fieldDefinition): bool
71+
{
72+
return $fieldDefinition->isSearchable
73+
// should only be verified if legacy is the current search engine
74+
&& $this->converterRegistry->getConverter($fieldDefinition->fieldTypeIdentifier)->getIndexColumn() !== false;
75+
}
76+
77+
private function getFilterType(FieldDefinition $fieldDefinition)
78+
{
79+
switch ($fieldDefinition->fieldTypeIdentifier)
80+
{
81+
case 'ezboolean':
82+
return 'Boolean';
83+
default:
84+
return 'String';
85+
}
86+
}
87+
}

src/Search/LegacySearchFeatures.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
namespace EzSystems\EzPlatformGraphQL\Search;
3+
4+
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
5+
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry;
6+
7+
class LegacySearchFeatures implements SearchFeatures
8+
{
9+
/**
10+
* @var \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry
11+
*/
12+
private $converterRegistry;
13+
14+
public function __construct(ConverterRegistry $converterRegistry)
15+
{
16+
$this->converterRegistry = $converterRegistry;
17+
}
18+
19+
public function supportsFieldCriterion(FieldDefinition $fieldDefinition)
20+
{
21+
return $this->converterRegistry->getConverter($fieldDefinition->fieldTypeIdentifier)->getIndexColumn() !== false;
22+
}
23+
}

src/Search/SearchFeatures.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace EzSystems\EzPlatformGraphQL\Search;
3+
4+
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
5+
6+
interface SearchFeatures
7+
{
8+
/**
9+
* Tests if search supports field filtering on $fieldDefinition.
10+
*
11+
* @param FieldDefinition $fieldDefinition
12+
*
13+
* @return bool
14+
*/
15+
public function supportsFieldCriterion(FieldDefinition $fieldDefinition);
16+
}

src/Search/SolrSearchFeatures.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace EzSystems\EzPlatformGraphQL\Search;
3+
4+
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
5+
6+
class SolrSearchFeatures implements SearchFeatures
7+
{
8+
public function supportsFieldCriterion(FieldDefinition $fieldDefinition)
9+
{
10+
return true;
11+
}
12+
}

0 commit comments

Comments
 (0)