-
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.
- Loading branch information
Showing
91 changed files
with
4,404 additions
and
26 deletions.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?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; | ||
|
||
use eZ\Publish\API\Repository\Values\Content\Query\Aggregation; | ||
|
||
interface AggregationVisitor | ||
{ | ||
/** | ||
* Check if visitor is applicable to current aggreagtion. | ||
*/ | ||
public function canVisit(Aggregation $aggregation, array $languageFilter): bool; | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function visit( | ||
AggregationVisitor $dispatcherVisitor, | ||
Aggregation $aggregation, | ||
array $languageFilter | ||
): array; | ||
} |
60 changes: 60 additions & 0 deletions
60
lib/Query/Common/AggregationVisitor/AbstractRangeAggregationVisitor.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,60 @@ | ||
<?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\AggregationVisitor; | ||
|
||
use DateTimeInterface; | ||
use eZ\Publish\API\Repository\Values\Content\Query\Aggregation\AbstractRangeAggregation; | ||
use eZ\Publish\API\Repository\Values\Content\Query\Aggregation; | ||
use EzSystems\EzPlatformSolrSearchEngine\Query\AggregationVisitor; | ||
|
||
abstract class AbstractRangeAggregationVisitor implements AggregationVisitor | ||
{ | ||
/** | ||
* @param \eZ\Publish\API\Repository\Values\Content\Query\Aggregation\AbstractRangeAggregation $aggregation | ||
*/ | ||
public function visit( | ||
AggregationVisitor $dispatcherVisitor, | ||
Aggregation $aggregation, | ||
array $languageFilter | ||
): array { | ||
$field = $this->getTargetField($aggregation); | ||
|
||
$rangeFacets = []; | ||
foreach ($aggregation->getRanges() as $range) { | ||
$from = $this->formatRangeValue($range->getFrom()); | ||
$to = $this->formatRangeValue($range->getTo()); | ||
|
||
$rangeFacets["${from}_${to}"] = [ | ||
'type' => 'query', | ||
'q' => sprintf('%s:[%s TO %s}', $field, $from, $to), | ||
]; | ||
} | ||
|
||
return [ | ||
'type' => 'query', | ||
'q' => '*:*', | ||
'facet' => $rangeFacets, | ||
]; | ||
} | ||
|
||
abstract protected function getTargetField(AbstractRangeAggregation $aggregation): string; | ||
|
||
private function formatRangeValue($value): string | ||
{ | ||
if ($value === null) { | ||
return '*'; | ||
} | ||
|
||
if ($value instanceof DateTimeInterface) { | ||
return $value->format('Y-m-d\\TH:i:s\\Z'); | ||
} | ||
|
||
return (string)$value; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
lib/Query/Common/AggregationVisitor/AbstractStatsAggregationVisitor.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,40 @@ | ||
<?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\AggregationVisitor; | ||
|
||
use eZ\Publish\API\Repository\Values\Content\Query\Aggregation\AbstractStatsAggregation; | ||
use eZ\Publish\API\Repository\Values\Content\Query\Aggregation; | ||
use EzSystems\EzPlatformSolrSearchEngine\Query\AggregationVisitor; | ||
|
||
abstract class AbstractStatsAggregationVisitor implements AggregationVisitor | ||
{ | ||
/** | ||
* @param \eZ\Publish\API\Repository\Values\Content\Query\Aggregation\AbstractStatsAggregation $aggregation | ||
*/ | ||
public function visit( | ||
AggregationVisitor $dispatcherVisitor, | ||
Aggregation $aggregation, | ||
array $languageFilter | ||
): array { | ||
$field = $this->getTargetField($aggregation); | ||
|
||
return [ | ||
'type' => 'query', | ||
'q' => '*:*', | ||
'facet' => [ | ||
'sum' => "sum($field)", | ||
'min' => "min($field)", | ||
'max' => "max($field)", | ||
'avg' => "avg($field)", | ||
], | ||
]; | ||
} | ||
|
||
abstract protected function getTargetField(AbstractStatsAggregation $aggregation): string; | ||
} |
34 changes: 34 additions & 0 deletions
34
lib/Query/Common/AggregationVisitor/AbstractTermAggregationVisitor.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,34 @@ | ||
<?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\AggregationVisitor; | ||
|
||
use eZ\Publish\API\Repository\Values\Content\Query\Aggregation\AbstractTermAggregation; | ||
use eZ\Publish\API\Repository\Values\Content\Query\Aggregation; | ||
use EzSystems\EzPlatformSolrSearchEngine\Query\AggregationVisitor; | ||
|
||
abstract class AbstractTermAggregationVisitor implements AggregationVisitor | ||
{ | ||
/** | ||
* @param \eZ\Publish\API\Repository\Values\Content\Query\Aggregation\AbstractTermAggregation $aggregation | ||
*/ | ||
public function visit( | ||
AggregationVisitor $dispatcherVisitor, | ||
Aggregation $aggregation, | ||
array $languageFilter | ||
): array { | ||
return [ | ||
'type' => 'terms', | ||
'field' => $this->getTargetField($aggregation), | ||
'limit' => $aggregation->getLimit(), | ||
'mincount' => $aggregation->getMinCount(), | ||
]; | ||
} | ||
|
||
abstract protected function getTargetField(AbstractTermAggregation $aggregation): string; | ||
} |
19 changes: 19 additions & 0 deletions
19
lib/Query/Common/AggregationVisitor/AggregationFieldResolver.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,19 @@ | ||
<?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\AggregationVisitor; | ||
|
||
use eZ\Publish\API\Repository\Values\Content\Query\Aggregation; | ||
|
||
/** | ||
* Resolves search index field name used for aggregation. | ||
*/ | ||
interface AggregationFieldResolver | ||
{ | ||
public function resolveTargetField(Aggregation $aggregation): string; | ||
} |
49 changes: 49 additions & 0 deletions
49
...mmon/AggregationVisitor/AggregationFieldResolver/ContentFieldAggregationFieldResolver.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,49 @@ | ||
<?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\AggregationVisitor\AggregationFieldResolver; | ||
|
||
use eZ\Publish\API\Repository\Values\Content\Query\Aggregation\FieldAggregation; | ||
use eZ\Publish\API\Repository\Values\Content\Query\Aggregation; | ||
use eZ\Publish\Core\Search\Common\FieldNameResolver; | ||
use EzSystems\EzPlatformSolrSearchEngine\Query\Common\AggregationVisitor\AggregationFieldResolver; | ||
use RuntimeException; | ||
|
||
final class ContentFieldAggregationFieldResolver implements AggregationFieldResolver | ||
{ | ||
/** @var \eZ\Publish\Core\Search\Common\FieldNameResolver */ | ||
private $fieldNameResolver; | ||
|
||
/** @var string */ | ||
private $searchFieldName; | ||
|
||
public function __construct(FieldNameResolver $fieldNameResolver, string $searchFieldName) | ||
{ | ||
$this->fieldNameResolver = $fieldNameResolver; | ||
$this->searchFieldName = $searchFieldName; | ||
} | ||
|
||
public function resolveTargetField(Aggregation $aggregation): string | ||
{ | ||
if (!($aggregation instanceof FieldAggregation)) { | ||
throw new RuntimeException('Expected instance of ' . FieldAggregation::class . ' , got ' . get_class($aggregation)); | ||
} | ||
|
||
$searchFieldName = $this->fieldNameResolver->getAggregationFieldName( | ||
$aggregation->getContentTypeIdentifier(), | ||
$aggregation->getFieldDefinitionIdentifier(), | ||
$this->searchFieldName | ||
); | ||
|
||
if ($searchFieldName === null) { | ||
throw new RuntimeException('No searchable fields found for the provided aggregation target'); | ||
} | ||
|
||
return $searchFieldName; | ||
} | ||
} |
Oops, something went wrong.