init
This commit is contained in:
+135
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stemcell\Catalog\Plugin\Amasty\ElasticSearch;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Search\GetRequestQuery;
|
||||
use Magento\Framework\Search\RequestInterface;
|
||||
|
||||
class AddCategoryOptionSelfExcludingAggregation
|
||||
{
|
||||
public const ATTRIBUTE_CODE = 'category_option';
|
||||
public const BUCKET_NAME = 'category_option_bucket';
|
||||
public const SCOPE_NAME = 'category_option_all_scope';
|
||||
public const FILTER_NAME = 'category_option_without_self_filter';
|
||||
|
||||
private const BUCKET_SIZE = 1000;
|
||||
|
||||
/**
|
||||
* Add the self-excluding category option aggregation.
|
||||
*
|
||||
* The aggregation applies the search query and every active filter except
|
||||
* category_option. The main query remains unchanged and still filters hits.
|
||||
*
|
||||
* @param GetRequestQuery $subject
|
||||
* @param array $result
|
||||
* @param RequestInterface $request
|
||||
* @return array
|
||||
*/
|
||||
public function afterExecute(
|
||||
GetRequestQuery $subject,
|
||||
array $result,
|
||||
RequestInterface $request
|
||||
): array {
|
||||
if (!isset($result['body']['query'], $result['body']['aggregations'][self::BUCKET_NAME])) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$aggregationQuery = $this->getUnscoredQuery($result['body']['query']);
|
||||
$categoryOptionFilterRemoved = false;
|
||||
$aggregationQuery = $this->removeCategoryOptionFilter(
|
||||
$aggregationQuery,
|
||||
$categoryOptionFilterRemoved
|
||||
);
|
||||
|
||||
if (!$categoryOptionFilterRemoved) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$result['body']['aggregations'][self::SCOPE_NAME] = [
|
||||
'global' => new \stdClass(),
|
||||
'aggregations' => [
|
||||
self::FILTER_NAME => [
|
||||
'filter' => $aggregationQuery ?: ['match_all' => new \stdClass()],
|
||||
'aggregations' => [
|
||||
self::BUCKET_NAME => [
|
||||
'terms' => [
|
||||
'field' => self::ATTRIBUTE_CODE,
|
||||
'size' => self::BUCKET_SIZE,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the query without its scoring wrapper.
|
||||
*
|
||||
* Relevance rules can wrap the actual query in script_score. Aggregations do
|
||||
* not use scores, so only the wrapped query is needed here.
|
||||
*
|
||||
* @param array $query
|
||||
* @return array
|
||||
*/
|
||||
private function getUnscoredQuery(array $query): array
|
||||
{
|
||||
if (isset($query['script_score']['query']) && is_array($query['script_score']['query'])) {
|
||||
return $query['script_score']['query'];
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove category_option term filters recursively.
|
||||
*
|
||||
* The search query and all other active filters are preserved.
|
||||
*
|
||||
* @param array $query
|
||||
* @param bool $removed
|
||||
* @return array
|
||||
*/
|
||||
private function removeCategoryOptionFilter(array $query, bool &$removed): array
|
||||
{
|
||||
foreach (['term', 'terms'] as $filterType) {
|
||||
if (isset($query[$filterType])
|
||||
&& is_array($query[$filterType])
|
||||
&& array_key_exists(self::ATTRIBUTE_CODE, $query[$filterType])
|
||||
) {
|
||||
$removed = true;
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($query as $key => $value) {
|
||||
if (!is_array($value)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (array_is_list($value)) {
|
||||
$children = [];
|
||||
foreach ($value as $child) {
|
||||
if (!is_array($child)) {
|
||||
$children[] = $child;
|
||||
continue;
|
||||
}
|
||||
|
||||
$child = $this->removeCategoryOptionFilter($child, $removed);
|
||||
if ($child !== []) {
|
||||
$children[] = $child;
|
||||
}
|
||||
}
|
||||
$query[$key] = $children;
|
||||
} else {
|
||||
$query[$key] = $this->removeCategoryOptionFilter($value, $removed);
|
||||
}
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stemcell\Catalog\Plugin\Amasty\ElasticSearch;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Search\GetResponse\GetAggregations;
|
||||
use Magento\Framework\Search\RequestInterface;
|
||||
|
||||
class UseCategoryOptionSelfExcludingAggregation
|
||||
{
|
||||
/**
|
||||
* Replace the regular category_option bucket.
|
||||
*
|
||||
* The replacement is produced by the self-excluding aggregation in the same
|
||||
* OpenSearch request.
|
||||
*
|
||||
* @param GetAggregations $subject
|
||||
* @param array $result
|
||||
* @param RequestInterface $request
|
||||
* @param array $elasticResponse
|
||||
* @return array
|
||||
*/
|
||||
public function afterExecute(
|
||||
GetAggregations $subject,
|
||||
array $result,
|
||||
RequestInterface $request,
|
||||
array $elasticResponse
|
||||
): array {
|
||||
$bucket = $elasticResponse['aggregations']
|
||||
[AddCategoryOptionSelfExcludingAggregation::SCOPE_NAME]
|
||||
[AddCategoryOptionSelfExcludingAggregation::FILTER_NAME]
|
||||
[AddCategoryOptionSelfExcludingAggregation::BUCKET_NAME]
|
||||
['buckets'] ?? null;
|
||||
|
||||
if (!is_array($bucket)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$values = [];
|
||||
foreach ($bucket as $item) {
|
||||
if (!array_key_exists('key', $item)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$values[$item['key']] = [
|
||||
'value' => $item['key'],
|
||||
'count' => (int)($item['doc_count'] ?? 0),
|
||||
];
|
||||
}
|
||||
|
||||
$result[AddCategoryOptionSelfExcludingAggregation::BUCKET_NAME] = $values;
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user