ltsecurityinc安防产品在线商城代码

This commit is contained in:
2026-09-14 17:47:10 +08:00
parent 57d8444c15
commit 6a3c9c5bd8
14539 changed files with 1775310 additions and 0 deletions
+132
View File
@@ -0,0 +1,132 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search;
use Amasty\ElasticSearch\Model\Client\ClientRepositoryInterface;
use Amasty\ElasticSearch\Model\Search\GetResponse\GetAggregations;
use Elasticsearch\Common\Exceptions\BadRequest400Exception;
use Magento\Framework\Registry as CoreRegistry;
use Magento\Framework\Search\AdapterInterface;
use Magento\Framework\Search\RequestInterface;
class Adapter implements AdapterInterface
{
public const REQUEST_QUERY = 'amasty_elastic_query';
public const HITS = 'hits';
public const PRODUCTS = 'products';
/**
* @var GetRequestQuery
*/
private $getRequestQuery;
/**
* @var GetResponse
*/
private $getElasticResponse;
/**
* @var GetAggregations
*/
private $getAggregations;
/**
* @var ClientRepositoryInterface
*/
private $clientRepository;
/**
* @var CoreRegistry
*/
private $registry;
/**
* @var Logger
*/
private $logger;
public function __construct(
ClientRepositoryInterface $clientRepository,
GetAggregations $getAggregations,
GetRequestQuery $getRequestQuery,
GetResponse $getElasticResponse,
CoreRegistry $registry,
Logger $logger
) {
$this->getAggregations = $getAggregations;
$this->getRequestQuery = $getRequestQuery;
$this->getElasticResponse = $getElasticResponse;
$this->clientRepository = $clientRepository;
$this->registry = $registry;
$this->logger = $logger;
}
/**
* @param RequestInterface $request
* @return \Magento\Framework\Search\Response\QueryResponse|mixed
* @throws \Elasticsearch\Common\Exceptions\Missing404Exception
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function query(RequestInterface $request)
{
$client = $this->clientRepository->get();
if (!$client->ping()) {
return $this->getElasticResponse->execute([], [], 0);
}
try {
$requestQuery = $this->getRequestQuery->execute($request);
$elasticResponse = $client->search($requestQuery);
} catch (BadRequest400Exception $e) {
$this->logger->logError($e->getMessage());
return $this->getElasticResponse->execute([], [], 0);
} catch (\Exception $e) {
return $this->getElasticResponse->execute([], [], 0);
}
$elasticDocuments = $elasticResponse['hits']['hits'] ?? [];
$elasticTotal = $elasticResponse['hits']['total']['value'] ?? $elasticResponse['hits']['total'] ?? 0;
$this->registry->unregister(self::REQUEST_QUERY);
$this->registry->register(self::REQUEST_QUERY, $requestQuery['body']['query']);
$aggregations = $this->getAggregations->execute($request, $elasticResponse);
$this->registry->unregister(self::REQUEST_QUERY);
$responseQuery = $this->getElasticResponse->execute($elasticDocuments, $aggregations, $elasticTotal);
$this->logger->log($request, $responseQuery, $requestQuery, $elasticResponse);
return $responseQuery;
}
/**
* @param RequestInterface $request
* @return array
* @throws \Elasticsearch\Common\Exceptions\Missing404Exception
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function queryAdvancedSearchProduct(RequestInterface $request)
{
$client = $this->clientRepository->get();
$requestQuery = $this->getRequestQuery->execute($request);
unset($requestQuery['aggregations']);
$requestQuery['body']['_source'] = ['amasty_xsearch_fulltext'];
$elasticResponse = $client->search($requestQuery);
$products = [];
if (!empty($elasticResponse['hits']['hits'])) {
foreach ($elasticResponse['hits']['hits'] as $index => $product) {
if (!empty($product['_source']['amasty_xsearch_fulltext'])) {
$id = $product['_source']['amasty_xsearch_fulltext']['entity_id'] ?? $product['_id'];
$products[$id] = $product['_source']['amasty_xsearch_fulltext'];
}
}
}
$hits = $elasticResponse['hits']['total']['value'] ?? $elasticResponse['hits']['total'] ?? 0;
return [self::HITS => $hits, self::PRODUCTS => $products];
}
}
+286
View File
@@ -0,0 +1,286 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search;
use Amasty\ElasticSearch\Model\Client\ClientRepositoryInterface;
use Magento\CatalogSearch\Model\Indexer\Fulltext;
use Magento\Framework\Search\Dynamic\EntityStorage;
class DataProvider implements \Magento\Framework\Search\Dynamic\DataProviderInterface
{
private const DEFAULT_AGGREGATION_FIELD = 'price';
/**
* @var \Magento\Catalog\Model\Layer\Filter\Price\Range
*/
private $range;
/**
* @var \Magento\Framework\Search\Dynamic\IntervalFactory
*/
private $intervalFactory;
/**
* @var \Magento\Framework\App\ScopeResolverInterface
*/
private $scopeResolver;
/**
* @var ClientRepositoryInterface
*/
private $clientRepository;
/**
* @var string
*/
private $indexerId;
/**
* @var \Amasty\ElasticSearch\Model\Config
*/
private $config;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;
/**
* @var GetRequestQuery\GetAggregations\FieldMapper
*/
private $fieldMapper;
/**
* @var \Magento\Customer\Model\Session
*/
private $customerSession;
/**
* @var \Magento\Framework\Registry
*/
private $registry;
/**
* @var string
*/
private $aggregationFieldName;
public function __construct(
ClientRepositoryInterface $clientRepository,
\Magento\Catalog\Model\Layer\Filter\Price\Range $range,
\Magento\Framework\Search\Dynamic\IntervalFactory $intervalFactory,
\Magento\Framework\App\ScopeResolverInterface $scopeResolver,
\Amasty\ElasticSearch\Model\Config $config,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Amasty\ElasticSearch\Model\Search\GetRequestQuery\GetAggregations\FieldMapper $fieldMapper,
\Magento\Customer\Model\Session $customerSession,
\Magento\Framework\Registry $registry,
$indexerId = Fulltext::INDEXER_ID,
$aggregationFieldName = self::DEFAULT_AGGREGATION_FIELD
) {
$this->range = $range;
$this->intervalFactory = $intervalFactory;
$this->scopeResolver = $scopeResolver;
$this->clientRepository = $clientRepository;
$this->config = $config;
$this->storeManager = $storeManager;
$this->fieldMapper = $fieldMapper;
$this->customerSession = $customerSession;
$this->registry = $registry;
$this->indexerId = $indexerId;
$this->aggregationFieldName = $aggregationFieldName;
}
/**
* @return array|int
*/
public function getRange()
{
return $this->range->getPriceRange();
}
/**
* @param EntityStorage $entityStorage
* @return array
*/
public function getAggregations(EntityStorage $entityStorage)
{
$query = $this->generateAggregationsQuery($entityStorage);
$queryResult = $this->clientRepository->get()->search($query);
$result['std'] = !empty($queryResult['aggregations']['prices']['std_deviation'])
? $queryResult['aggregations']['prices']['std_deviation'] : 0;
foreach (['count', 'max', 'min'] as $aggregator) {
$result[$aggregator] = !empty($queryResult['aggregations']['prices'][$aggregator])
? $queryResult['aggregations']['prices'][$aggregator] : 0;
}
return $result;
}
/**
* @param EntityStorage $entityStorage
* @return array
*/
private function generateAggregationsQuery(EntityStorage $entityStorage)
{
$query = $this->generateGeneralQuery($entityStorage);
$query['body']['aggregations'] = [
'prices' => [
'extended_stats' => [
'field' => $this->fieldMapper->mapFieldName($this->aggregationFieldName),
],
],
];
return $query;
}
/**
* @param EntityStorage $entityStorage
* @param $fieldName
* @param $range
* @param array $dimensions
* @return array
*/
private function generateAggregationQuery(EntityStorage $entityStorage, $fieldName, $range, $dimensions = [])
{
$query = $this->generateGeneralQuery($entityStorage, $dimensions);
$query['body']['aggregations'] = [
'prices' => [
'histogram' => [
'field' => $fieldName,
'interval' => $range,
],
],
];
return $query;
}
/**
* @param EntityStorage $entityStorage
* @param array $dimensions
* @return array
*/
private function generateGeneralQuery(EntityStorage $entityStorage, array $dimensions = [])
{
$dimension = current($dimensions);
$storeId = false !== $dimension
? $this->scopeResolver->getScope($dimension->getValue())->getId()
: $this->storeManager->getStore()->getId();
$requestQuery = [
'index' => $this->config->getIndexName($this->indexerId, $storeId),
'type' => $this->config->getEntityType(),
'body' => [
'stored_fields' => [
'_id',
'_score',
]
]
];
$query = $this->registry->registry(Adapter::REQUEST_QUERY);
if (!$query) {
$entityIds = $entityStorage->getSource();
$query = [
'bool' => [
'must' => [
['terms' => ['_id' => $entityIds]]
]
]
];
}
$requestQuery['body']['query'] = $query;
return $requestQuery;
}
/**
* @param \Magento\Framework\Search\Request\BucketInterface $bucket
* @param array $dimensions
* @param EntityStorage $entityStorage
* @return \Magento\Framework\Search\Dynamic\IntervalInterface
*/
public function getInterval(
\Magento\Framework\Search\Request\BucketInterface $bucket,
array $dimensions,
EntityStorage $entityStorage
) {
$dimension = current($dimensions);
$storeId = $this->scopeResolver->getScope($dimension->getValue())->getId();
return $this->intervalFactory->create(
[
'entityIds' => $entityStorage->getSource(),
'storeId' => $storeId,
'fieldName' => 'price_' . $this->customerSession->getCustomerGroupId(),
]
);
}
/**
* @param \Magento\Framework\Search\Request\BucketInterface $bucket
* @param array $dimensions
* @param int $range
* @param EntityStorage $entityStorage
* @return array
*/
public function getAggregation(
\Magento\Framework\Search\Request\BucketInterface $bucket,
array $dimensions,
$range,
EntityStorage $entityStorage
) {
$result = [];
$fieldName = $this->fieldMapper->mapFieldName($bucket->getField());
$query = $this->generateAggregationQuery($entityStorage, $fieldName, $range, $dimensions);
$queryResult = $this->clientRepository->get()->search($query);
foreach ($queryResult['aggregations']['prices']['buckets'] as $bucket) {
$key = (int)($bucket['key'] / $range + 1);
if ($bucket['doc_count']) {
$result[$key] = $bucket['doc_count'];
}
}
return $result;
}
/**
* @param int $range
* @param array $ranges
* @return array
*/
public function prepareData($range, array $ranges)
{
$result = [];
if (!empty($ranges)) {
$lastIndex = array_keys($ranges);
$lastIndex = $lastIndex[count($lastIndex) - 1];
foreach ($ranges as $index => $count) {
$fromPrice = $index == 1 ? '' : ($index - 1) * $range;
$toPrice = $index * $range;
$result[] = [
'from' => $fromPrice,
'to' => $toPrice,
'count' => $count,
];
}
}
return $result;
}
}
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search;
use Magento\CatalogSearch\Model\Indexer\Fulltext;
use Magento\Framework\ObjectManagerInterface;
class DataProviderFactory
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var string
*/
private $instanceName;
public function __construct(
ObjectManagerInterface $objectManager,
$instanceName = DataProvider::class
) {
$this->objectManager = $objectManager;
$this->instanceName = $instanceName;
}
public function create(
string $indexerId = Fulltext::INDEXER_ID,
?string $aggregationFieldName = null
): DataProvider {
return $this->objectManager->create(
$this->instanceName,
[
'indexerId' => $indexerId,
'aggregationFieldName' => $aggregationFieldName
]
);
}
}
@@ -0,0 +1,130 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search\Dynamic;
use Magento\Framework\Search\Dynamic\Algorithm as MagentoAlgorithm;
use Magento\Framework\Search\Dynamic\IntervalInterface;
/**
* Class Algorithm
* Class implemented for fixing magento bug, related to notice "Undefined index in Search\Dynamic\Algorithm"
*/
class Algorithm extends MagentoAlgorithm
{
/**
* @param int $quantileNumber
* @param IntervalInterface $interval
* @return array|false
*/
protected function _findValueSeparator($quantileNumber, IntervalInterface $interval)
{
if ($quantileNumber < 1 || $quantileNumber >= $this->getIntervalsNumber()) {
return null;
}
$values = [];
$quantileInterval = $this->_getQuantileInterval($quantileNumber);
$intervalValuesCount = $quantileInterval[1] - $quantileInterval[0] + 1;
$offset = $quantileInterval[0];
if ($this->_lastValueLimiter[0] !== null) {
$offset -= $this->_lastValueLimiter[0];
}
if ($offset < 0) {
$intervalValuesCount += $offset;
$values = array_slice(
$this->_values,
$this->_lastValueLimiter[0] + $offset - $this->_quantileInterval[0],
-$offset
);
$offset = 0;
}
$lowerValue = $this->_lastValueLimiter[1];
if ($this->_lowerLimit !== null) {
$lowerValue = max($lowerValue, $this->_lowerLimit);
}
if ($intervalValuesCount >= 0) {
$values = array_merge(
$values,
$interval->load($intervalValuesCount + 1, $offset, $lowerValue, $this->_upperLimit)
);
}
$bestRoundValue = [];
if (isset($values[0]) && isset($values[$intervalValuesCount - 1])) {
$lastValue = $values[$intervalValuesCount - 1];
if ($lastValue == $values[0]) {
if ($quantileNumber == 1 && $offset) {
$additionalValues = $interval->loadPrevious($lastValue, $quantileInterval[0], $this->_lowerLimit);
if ($additionalValues) {
$quantileInterval[0] -= count($additionalValues);
$values = array_merge($additionalValues, $values);
$bestRoundValue = $this->_findRoundValue(
$values[0] + self::MIN_POSSIBLE_VALUE / 10,
$lastValue,
false
);
}
}
if ($quantileNumber == $this->getIntervalsNumber() - 1) {
$valuesCount = count($values);
if ($values[$valuesCount - 1] > $lastValue) {
$additionalValues = [$values[$valuesCount - 1]];
} else {
$additionalValues = $interval->loadNext(
$lastValue,
$this->_count - $quantileInterval[0] - count($values),
$this->_upperLimit
);
}
if ($additionalValues) {
$quantileInterval[1] = $quantileInterval[0] + count($values) - 1;
if ($values[$valuesCount - 1] <= $lastValue) {
$quantileInterval[1] += count($additionalValues);
$values = array_merge($values, $additionalValues);
}
$upperBestRoundValue = $this->_findRoundValue(
$lastValue + self::MIN_POSSIBLE_VALUE / 10,
$values[count($values) - 1],
false
);
$this->_mergeRoundValues($bestRoundValue, $upperBestRoundValue);
}
}
} else {
$bestRoundValue = $this->_findRoundValue(
$values[0] + self::MIN_POSSIBLE_VALUE / 10,
$lastValue
);
}
}
$this->_quantileInterval = $quantileInterval;
$this->_values = $values;
if (empty($bestRoundValue)) {
$this->_skippedQuantilesUpperLimits[$quantileNumber] = $quantileInterval[1];
return $bestRoundValue;
}
$valuesCount = count($values);
if ($values[$valuesCount - 1] > $lastValue) {
$this->_lastValueLimiter = [$quantileInterval[0] + $valuesCount - 1, $values[$valuesCount - 1]];
}
ksort($bestRoundValue, SORT_NUMERIC);
foreach ($bestRoundValue as $index => &$bestRoundValueValues) {
if (empty($bestRoundValueValues)) {
unset($bestRoundValue[$index]);
} else {
sort($bestRoundValueValues);
}
}
return array_reverse($bestRoundValue);
}
}
@@ -0,0 +1,204 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search;
use Amasty\ElasticSearch\Api\RelevanceRuleRepositoryInterface;
use Amasty\ElasticSearch\Model\Indexer\Data\External\RelevanceBoostDataMapper;
use Amasty\ElasticSearch\Model\Search\GetRequestQuery\InjectSubqueryInterface;
use Amasty\ElasticSearch\Model\Search\GetRequestQuery\SortingProvider;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Search\Request\Query\BoolExpression as BoolQuery;
use Magento\Framework\Search\Request\Query\Filter as FilterQuery;
use Magento\Framework\Search\Request\Query\Match as MatchQuery;
use Magento\Framework\Search\Request\QueryInterface;
use Magento\Framework\Search\RequestInterface;
class GetRequestQuery
{
public const QUERY_CONDITION_MUST_NOT = 'must_not';
/**
* @var \Amasty\ElasticSearch\Model\Config
*/
private $config;
/**
* @var GetRequestQuery\GetAggregations
*/
private $getAggregations;
/**
* @var InjectSubqueryInterface[]
*/
private $subqueryInjectors;
/**
* @var SortingProvider
*/
private $sortingProvider;
/**
* @var array
*/
private $requestNamesForApplyRelevanceRules;
/**
* @var string[]
*/
private $booleanMethods = [
BoolQuery::QUERY_CONDITION_MUST => 'getMust',
BoolQuery::QUERY_CONDITION_SHOULD => 'getShould',
self::QUERY_CONDITION_MUST_NOT => 'getMustNot'
];
/**
* @var RelevanceRuleRepositoryInterface
*/
private $relevanceRuleRepository;
public function __construct(
\Amasty\ElasticSearch\Model\Config $config,
GetRequestQuery\GetAggregations $getAggregations,
SortingProvider $sortingProvider,
RelevanceRuleRepositoryInterface $relevanceRuleRepository,
array $subqueryInjectors,
array $requestNamesForApplyRelevanceRules = []
) {
$this->config = $config;
$this->subqueryInjectors = $subqueryInjectors;
$this->getAggregations = $getAggregations;
$this->sortingProvider = $sortingProvider;
$this->requestNamesForApplyRelevanceRules = array_unique($requestNamesForApplyRelevanceRules);
$this->relevanceRuleRepository = $relevanceRuleRepository;
}
/**
* @param RequestInterface $request
* @return array
* @throws LocalizedException
*/
public function execute(RequestInterface $request): array
{
$storeId = (int) current($request->getDimensions())->getValue();
$query = [
'index' => $this->config->getIndexName('product', $storeId),
'type' => $this->config->getEntityType(),
'body' => [
'from' => $request->getFrom(),
'size' => $request->getSize(),
'stored_fields' => ['_id', '_score'],
'sort' => $this->sortingProvider->execute($request),
'query' => $this->getQuery($request)
],
];
$aggregations = $this->getAggregations->execute($request);
if ($aggregations) {
$query['body']['aggregations'] = $aggregations;
}
if (isset($query['body']['query']['script_score']['query']['bool']['should'])) {
$query['body']['query']['script_score']['query']['bool']['minimum_should_match'] = 1;
}
if (isset($query['body']['query']['bool']['should'])) {
$query['body']['query']['bool']['minimum_should_match'] = 1;
}
$query['track_total_hits'] = true;
return $query;
}
private function getQuery(RequestInterface $request): array
{
if ($this->isCanApplyRelevanceSorting($request) && $this->isNeedApplyRelevanceSorting()) {
$query = [
'script_score' => [
'query' => $this->processQuery($request->getQuery(), [], BoolQuery::QUERY_CONDITION_MUST),
'script' => [
'source' => "params['_source']['" . RelevanceBoostDataMapper::ATTRIBUTE_NAME . "']*_score"
]
]
];
} else {
$query = $this->processQuery($request->getQuery(), [], BoolQuery::QUERY_CONDITION_MUST);
}
return $query;
}
public function isCanApplyRelevanceSorting(RequestInterface $request): bool
{
return in_array($request->getName(), $this->requestNamesForApplyRelevanceRules);
}
private function isNeedApplyRelevanceSorting(): bool
{
return $this->relevanceRuleRepository->getActiveRules()->getSize() !== 0;
}
/**
* @param QueryInterface $request
* @param array $elasticQuery
* @param $conditionType
* @return array
* @throws LocalizedException
*/
private function processQuery(QueryInterface $request, array $elasticQuery, $conditionType)
{
switch ($request->getType()) {
case QueryInterface::TYPE_BOOL:
/** @var BoolQuery $request */
foreach ($this->booleanMethods as $conditionType => $method) {
foreach ($request->{$method}() as $subQuery) {
$elasticQuery = $this->processQuery($subQuery, $elasticQuery, $conditionType);
}
}
break;
case QueryInterface::TYPE_MATCH:
/** @var MatchQuery $request */
$addSubquery = $this->getSubqueryInjectorByType('match');
$elasticQuery = $addSubquery->execute($elasticQuery, $request, $conditionType);
break;
case QueryInterface::TYPE_FILTER:
/** @var FilterQuery $request */
if ($request->getReferenceType() === FilterQuery::REFERENCE_QUERY) {
$elasticQuery = $this->processQuery($request->getReference(), $elasticQuery, $conditionType);
} elseif ($request->getReferenceType() === FilterQuery::REFERENCE_FILTER) {
$addSubquery = $this->getSubqueryInjectorByType($request->getReference()->getType());
$elasticQuery = $addSubquery->execute($elasticQuery, $request, $conditionType);
} else {
throw new LocalizedException(
__("Builder for %1 reference type doesn't exist", $request->getReferenceType())
);
}
break;
default:
throw new LocalizedException(__("Processor for %1 type doesn't exist", $request->getType()));
}
return $elasticQuery;
}
/**
* @param string $filterType
* @return InjectSubqueryInterface
* @throws LocalizedException
*/
private function getSubqueryInjectorByType($filterType)
{
if (!array_key_exists($filterType, $this->subqueryInjectors)) {
throw new LocalizedException(__("'%1' filter type is not supported", $filterType));
}
return $this->subqueryInjectors[$filterType];
}
}
@@ -0,0 +1,67 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search\GetRequestQuery;
use Magento\Framework\Search\Request\BucketInterface;
use Amasty\ElasticSearch\Model\Search\GetRequestQuery\GetAggregations\FieldMapper;
class GetAggregations
{
/**
* @var FieldMapper
*/
private $fieldMapper;
public function __construct(FieldMapper $fieldMapper)
{
$this->fieldMapper = $fieldMapper;
}
/**
* @param \Magento\Framework\Search\RequestInterface $request
* @return array
*/
public function execute(\Magento\Framework\Search\RequestInterface $request)
{
$aggregationsQuery = [];
foreach ($request->getAggregation() as $bucket) {
$aggregationsQuery = array_merge_recursive($aggregationsQuery, $this->buildBucket($bucket));
}
return $aggregationsQuery;
}
/**
* @param BucketInterface $bucket
* @return array
*/
private function buildBucket(BucketInterface $bucket)
{
if ($bucket->getType() == BucketInterface::TYPE_TERM) {
return [
$bucket->getName() => [
'terms' => [
'field' => $this->fieldMapper->mapFieldName($bucket->getField()),
'size' => 1000,
],
],
];
} elseif ($bucket->getType() == BucketInterface::TYPE_DYNAMIC) {
return [
$bucket->getName() => [
'extended_stats' => [
'field' => $this->fieldMapper->mapFieldName($bucket->getField()),
],
],
];
}
return [];
}
}
@@ -0,0 +1,47 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search\GetRequestQuery\GetAggregations;
class FieldMapper
{
public const PRICE_ATTRIBUTE = 'price';
/**
* @var \Magento\Customer\Model\Session
*/
private $customerSession;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;
public function __construct(
\Magento\Customer\Model\Session $customerSession,
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
$this->customerSession = $customerSession;
$this->storeManager = $storeManager;
}
/**
* @param string $fieldName
* @return string
*/
public function mapFieldName($fieldName)
{
switch ($fieldName) {
case self::PRICE_ATTRIBUTE:
return $fieldName . '_' . $this->customerSession->getCustomerGroupId();
case 'sku':
// fix filter search
return 'sku_value';
}
return $fieldName;
}
}
@@ -0,0 +1,50 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search\GetRequestQuery;
use Magento\Framework\Search\Request\QueryInterface;
use Amasty\ElasticSearch\Model\Search\GetRequestQuery\GetAggregations\FieldMapper;
class InjectFilterRangeQuery implements InjectSubqueryInterface
{
/**
* @var FieldMapper
*/
private $fieldMapper;
public function __construct(FieldMapper $fieldMapper)
{
$this->fieldMapper = $fieldMapper;
}
/**
* @inheritdoc
*/
public function execute(array $elasticQuery, QueryInterface $request, $conditionType)
{
/** @var \Magento\Framework\Search\Request\Query\Filter $request */
/** @var \Magento\Framework\Search\Request\Filter\Range $filter */
$filter = $request->getReference();
$fieldName = $this->fieldMapper->mapFieldName($filter->getField());
$filterQuery = [];
if ($filter->getFrom()) {
$filterQuery['range'][$fieldName]['gte'] = $filter->getFrom();
}
if ($filter->getTo()) {
$filterQuery['range'][$fieldName]['lte'] = $filter->getTo();
}
if (!isset($elasticQuery['bool'][$conditionType])) {
$elasticQuery['bool'][$conditionType] = [];
}
$elasticQuery['bool'][$conditionType][] = $filterQuery;
return $elasticQuery;
}
}
@@ -0,0 +1,51 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search\GetRequestQuery;
use Magento\Framework\Search\Request\QueryInterface;
use Amasty\ElasticSearch\Model\Search\GetRequestQuery\GetAggregations\FieldMapper;
class InjectFilterTermQuery implements InjectSubqueryInterface
{
/**
* @var FieldMapper
*/
private $fieldMapper;
public function __construct(FieldMapper $fieldMapper)
{
$this->fieldMapper = $fieldMapper;
}
/**
* @inheritdoc
*/
public function execute(array $elasticQuery, QueryInterface $request, $conditionType)
{
/** @var \Magento\Framework\Search\Request\Filter\Term $filter */
$filter = $request->getReference();
$fieldName = $this->fieldMapper->mapFieldName($filter->getField());
if (!isset($elasticQuery['bool'][$conditionType])) {
$elasticQuery['bool'][$conditionType] = [];
}
$filterValue = $filter->getValue();
$filterType = is_array($filterValue) ? 'terms' : 'term';
if (is_array($filterValue) && array_key_exists('nin', $filterValue)) {
$filterValue = $filterValue['nin'];
}
$elasticQuery['bool'][$conditionType][] = [
$filterType => [
$fieldName => $filterValue
]
];
return $elasticQuery;
}
}
@@ -0,0 +1,47 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search\GetRequestQuery;
use Magento\Framework\Search\Request\QueryInterface;
use Amasty\ElasticSearch\Model\Search\GetRequestQuery\GetAggregations\FieldMapper;
class InjectFilterWildcardQuery implements InjectSubqueryInterface
{
/**
* @var FieldMapper
*/
private $fieldMapper;
public function __construct(FieldMapper $fieldMapper)
{
$this->fieldMapper = $fieldMapper;
}
/**
* @inheritdoc
*/
public function execute(array $elasticQuery, QueryInterface $request, $conditionType)
{
/** @var \Magento\Framework\Search\Request\Filter\Term $filter */
$filter = $request->getReference();
$fieldName = $this->fieldMapper->mapFieldName($filter->getField());
if (!isset($elasticQuery['bool'][$conditionType])) {
$elasticQuery['bool'][$conditionType] = [];
}
$value = str_replace('/', '\/', preg_quote($filter->getValue()));
$elasticQuery['bool'][$conditionType][] = [
'wildcard' => [
$fieldName => '*' . $value . '*'
]
];
return $elasticQuery;
}
}
@@ -0,0 +1,318 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search\GetRequestQuery;
use Amasty\ElasticSearch\Model\Config\QuerySettings;
use Amasty\ElasticSearch\Model\GetNonTextAttributes;
use Amasty\ElasticSearch\Model\Source\WildcardMode;
use Magento\Framework\Search\Request\QueryInterface;
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
use Magento\Store\Model\StoreManager;
use Amasty\ElasticSearch\Model\ResourceModel\StopWord\CollectionFactory;
class InjectMatchQuery implements InjectSubqueryInterface
{
private const MIN_CHAR_FOR_MORE_SPELLING_CORRECTION = 6;
/**
* @var \Amasty\ElasticSearch\Model\Config
*/
private $config;
/**
* @var GetNonTextAttributes
*/
private $getNonTextAttributes;
/**
* @var ServicePreprocessor\PreprocessorInterface[]
*/
private $services;
/**
* @var AttributeCollectionFactory
*/
private $attributeCollectionFactory;
/**
* @var StoreManager
*/
private $storeManager;
/**
* @var CollectionFactory
*/
private $stopWordCollectionFactory;
/**
* @var array
*/
private $selectAttributes = [];
/**
* @var array
*/
private $excludedAttributes = [];
/**
* @var null
*/
private $stopWords = null;
public function __construct(
\Amasty\ElasticSearch\Model\Config $config,
GetNonTextAttributes $getNonTextAttributes,
AttributeCollectionFactory $attributeCollectionFactory,
StoreManager $storeManager,
CollectionFactory $collectionFactory,
array $services = []
) {
$this->services = $services;
$this->getNonTextAttributes = $getNonTextAttributes;
$this->config = $config;
$this->attributeCollectionFactory = $attributeCollectionFactory;
$this->storeManager = $storeManager;
$this->stopWordCollectionFactory = $collectionFactory;
$this->_construct();
}
/**
* @return void
*/
protected function _construct()
{
$attributeCollection = $this->attributeCollectionFactory->create();
$attributeCollection
->getSelect()
->setPart('columns', [])
->columns(['frontend_input', 'attribute_code']);
foreach ($attributeCollection as $attribute) {
if (in_array($attribute->getFrontendInput(), ['select', 'multiselect'], true)) {
$this->selectAttributes[] = $attribute->getAttributeCode();
} elseif ($attribute->getFrontendInput() === 'boolean') {
$this->excludedAttributes[] = $attribute->getAttributeCode();
}
}
$this->excludedAttributes = array_merge($this->excludedAttributes, $this->getNonTextAttributes->execute());
}
/**
* @inheritdoc
*/
public function execute(array $elasticQuery, QueryInterface $request, $conditionType)
{
$requestValue = $this->processServices($request->getValue());
$requestValue = ['condition' => $conditionType, 'value' => $requestValue];
$conditionQuery = $this->getConditionsByMatches($request, $requestValue);
foreach ($conditionQuery as $subCondition) {
$elasticQuery['bool'][$subCondition['condition']][]= $subCondition['body'];
}
return $elasticQuery;
}
/**
* @param string $requestValue
* @return string
*/
private function processServices($requestValue)
{
$requestValue = strip_tags($requestValue);
$pattern = '/(\+|-|\/|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|\*|\?|:|\\\)/';
$requestValue = preg_replace($pattern, '\\\$1', $requestValue);
foreach ($this->services as $service) {
$requestValue = $service->process($requestValue);
}
return $requestValue;
}
private function getConditionsByMatches(QueryInterface $request, array $requestValue): array
{
$conditions = [];
foreach ($request->getMatches() as $match) {
$queryConfig = $this->config->getQuerySettingByAttributeCode($match['field']);
if (in_array($match['field'], $this->excludedAttributes, true) || empty($queryConfig)) {
continue;
}
$field = $this->getFieldName($match['field']);
$value = $this->getValue($requestValue['value'], $queryConfig);
$conditions[] = $this->getCondition($field, $value, $match, $requestValue, $queryConfig);
if ($queryConfig[QuerySettings::WILDCARD]) {
$value = $this->getValue($requestValue['value'], $queryConfig, true);
$conditions[] = $this->getCondition($field, $value, $match, $requestValue, $queryConfig);
}
if ($this->config->hasStemming()) {
$stemmingValue = $this->getValue($requestValue['value'], $queryConfig, true);
$conditions[] = $this->getCondition(
$field,
$stemmingValue,
$match,
$requestValue,
$queryConfig,
true
);
}
}
return $conditions;
}
private function getCondition(
string $field,
string $value,
array $match,
array $requestValue,
array $queryConfig,
bool $hasStemming = false
): array {
$stemming = $hasStemming ? '.stemming' : '';
return [
'body' => [
'query_string' => [
'default_field' => $field . $stemming,
'query' => $value,
'boost' => pow(2, $match['boost'] ?? 1),
'default_operator' => $queryConfig[QuerySettings::COMBINING] ? 'AND' : 'OR'
],
],
'condition' => $requestValue['condition']
];
}
/**
* @param string $name
* @return string
*/
private function getFieldName($name)
{
if (in_array($name, $this->selectAttributes, true)) {
$name .= '_value';
}
return $name;
}
private function getValue(
string $value,
array $queryConfig,
bool $skipWildCardModification = false
): string {
$wildcardType = (string)$this->config->getModuleConfig('catalog/wildcard_mode');
$wildMinChars = (int)$this->config->getModuleConfig('catalog/wildcard_symbols');
$spellMinChars = (int)$this->config->getModuleConfig('catalog/spellcorrection_symbols');
$wildcard = (bool)$queryConfig[QuerySettings::WILDCARD];
$spellCorrection = (bool)$queryConfig[QuerySettings::SPELLING];
$value = array_filter(explode(' ', $value));
$stopWords = $this->getStopWords($value);
$queryWords = array_udiff($value, $stopWords, 'strcasecmp');
if (!$this->config->useCustomAnalyzer($this->storeManager->getStore()->getId())
&& !$skipWildCardModification
) {
$queryWords = $this->processQueryWords(
$queryWords,
$wildcard,
$wildMinChars,
$wildcardType,
$spellCorrection,
$spellMinChars
);
}
return implode(' ', $queryWords);
}
/**
* @param array $words
* @return array
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function removeStopWords($words = [])
{
$stopWords = $this->getStopWords($words);
foreach ($words as $key => $word) {
if (trim($word) !== '' && $this->insensitiveInArray($word, $stopWords, true)) {
unset($words[$key]);
}
}
return $words;
}
/**
* @param array $words
* @return array
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function getStopWords(array $words = [])
{
$storeId = $this->storeManager->getStore()->getId();
if ($this->stopWords === null
&& !$this->config->getUsePredefinedStopwords($storeId)
) {
$this->stopWords = [];
$words = array_diff($words, [';']);//fix multiple query error
$collection = $this->stopWordCollectionFactory->create()
->addStoreFilter($storeId)
->addTermsFilter($words);
foreach ($collection as $stopWord) {
$this->stopWords[$stopWord->getId()] = $stopWord->getTerm();
}
}
return $this->stopWords ? : [];
}
/**
* @param $needle
* @param $haystack
* @param $strict
* @return bool
*/
private function insensitiveInArray($needle, $haystack, $strict)
{
return in_array(strtolower($needle), array_map('strtolower', $haystack), $strict);
}
private function processQueryWords(
array $queryWords,
bool $wildcard,
int $wildMinChars,
string $wildcardType,
bool $spellCorrection,
int $spellMinChars
): array {
foreach ($queryWords as &$term) {
if ($wildcard && (mb_strlen($term) >= $wildMinChars)) {
switch ($wildcardType) {
case WildcardMode::BOTH:
$term = '*' . $term . '*';
break;
case WildcardMode::PREFIX:
$term = '*' . $term;
break;
case WildcardMode::SUFFIX:
$term .= '*';
break;
}
} elseif ($spellCorrection && (mb_strlen($term) >= $spellMinChars)) {
$term .= mb_strlen($term) >= self::MIN_CHAR_FOR_MORE_SPELLING_CORRECTION ? '~2' : '~1';
}
}
return $queryWords;
}
}
@@ -0,0 +1,21 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search\GetRequestQuery;
use Magento\Framework\Search\Request\QueryInterface;
interface InjectSubqueryInterface
{
/**
* @param QueryInterface $request
* @param array $elasticQuery
* @param string $conditionType
* @return array
*/
public function execute(array $elasticQuery, QueryInterface $request, $conditionType);
}
@@ -0,0 +1,17 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search\GetRequestQuery\ServicePreprocessor;
interface PreprocessorInterface
{
/**
* @param string $query
* @return string
*/
public function process($query);
}
@@ -0,0 +1,53 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search\GetRequestQuery\ServicePreprocessor;
use Amasty\ElasticSearch\Api\StopWordRepositoryInterface;
class Stopwords implements PreprocessorInterface
{
/**
* @var StopWordRepositoryInterface
*/
private $stopWordRepository;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
StopWordRepositoryInterface $stopWordRepository
) {
$this->stopWordRepository = $stopWordRepository;
$this->storeManager = $storeManager;
}
/**
* @param string $query
* @return string
*/
public function process($query)
{
$storeId = $this->storeManager->getStore()->getId();
$stopWords = $this->stopWordRepository->getArrayListByStoreId($storeId);
$origQuery = $query;
// remove stop words from query
$query = explode(' ', $query);
$query = implode(' ', array_diff($query, $stopWords));
$query = trim($query);
if (!$query) {
$query = $origQuery;//return query if query is only from stop words
}
return $query;
}
}
@@ -0,0 +1,57 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search\GetRequestQuery\ServicePreprocessor;
use Amasty\ElasticSearch\Api\SynonymRepositoryInterface;
class Synonyms implements PreprocessorInterface
{
public const SYNONYM_DELEMITER = '$';
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;
/**
* @var SynonymRepositoryInterface
*/
private $synonymRepository;
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
SynonymRepositoryInterface $synonymRepository
) {
$this->storeManager = $storeManager;
$this->synonymRepository = $synonymRepository;
}
/**
* @param string $query
* @return string
*/
public function process($query)
{
if (!$query) {
return $query;
}
$storeId = $this->storeManager->getStore()->getId();
$synonymsArray = $this->synonymRepository->getSynonymsByQuery($query, $storeId);
if (!empty($synonymsArray)) {
$synonyms = [];
foreach ($synonymsArray as $synonymPart) {
$synonyms [] = implode(self::SYNONYM_DELEMITER, $synonymPart);
}
$query = implode(' ', $synonyms);
}
return $query;
}
}
@@ -0,0 +1,162 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search\GetRequestQuery;
use Amasty\ElasticSearch\Model\Indexer\Data\External\RelevanceBoostDataMapper;
use Amasty\ElasticSearch\Utils\AttributeUtil;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Customer\Model\Session;
use Magento\Eav\Model\Config;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Registry;
use Magento\Framework\Search\RequestInterface;
use Magento\Store\Model\StoreManager;
class SortingProvider
{
public const DEFAULT_SORTING = 'relevance';
public const DEFAULT_DIRECTION = 'desc';
/**
* List of fields that need to skipp by default.
* @var array
*/
private $skippedFields = ['entity_id'];
/**
* Default mapping for special fields.
* @var array
*/
private $map = ['relevance' => '_score'];
/**
* @var \Magento\Eav\Model\Config
*/
private $eavConfig;
/**
* @var \Magento\Customer\Model\Session
*/
private $customerSession;
/**
* @var Registry
*/
private $coreRegistry;
/**
* @var StoreManager
*/
private $storeManager;
/**
* @var AttributeUtil
*/
private $attributeUtil;
public function __construct(
Config $eavConfig,
Session $customerSession,
Registry $registry,
StoreManager $storeManager,
array $skippedFields = [],
array $map = [],
?AttributeUtil $attributeUtil = null // TODO: move to not optional argument and remove OM
) {
$this->eavConfig = $eavConfig;
$this->customerSession = $customerSession;
$this->coreRegistry = $registry;
$this->storeManager = $storeManager;
$this->skippedFields = array_merge($this->skippedFields, $skippedFields);
$this->map = array_merge($this->map, $map);
$this->attributeUtil = $attributeUtil ?? ObjectManager::getInstance()->get(AttributeUtil::class);
}
/**
* @param RequestInterface $request
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute(RequestInterface $request)
{
$sortings = [];
foreach ($this->getRequestedSorting($request) as $item) {
if (!$item['field'] || in_array($item['field'], $this->skippedFields)) {
continue;
}
$attributeCode = $item['field'];
$fieldName = $this->getFieldName($attributeCode);
$sortings[] = [
$fieldName => [
'order' => strtolower($item['direction'])
]
];
}
return $sortings;
}
/**
* @param RequestInterface $request
* @return array
*/
public function getRequestedSorting(RequestInterface $request)
{
$result = [];
if (method_exists($request, 'getSort') && $request->getSort()) {
$result = $request->getSort();
}
if (empty($result)) {
$result[] = ['field' => self::DEFAULT_SORTING, 'direction' => self::DEFAULT_DIRECTION];
}
return $result;
}
private function getFieldName(string $attributeCode): string
{
if ($attributeCode === 'price') {
$customerGroupId = $this->customerSession->getCustomerGroupId();
return 'price_' . $customerGroupId;
}
if ($attributeCode === 'position') {
$categoryId = $this->coreRegistry->registry('current_category')
? $this->coreRegistry->registry('current_category')->getId()
: $this->storeManager->getStore()->getRootCategoryId();
return 'category_position_' . $categoryId;
}
if (isset($this->map[$attributeCode])) {
return $this->map[$attributeCode];
}
$attribute = $this->eavConfig->getAttribute(
ProductAttributeInterface::ENTITY_TYPE_CODE,
$attributeCode
);
if ($this->attributeUtil->isComplexAttribute($attribute)) {
return $attributeCode . '_value';
}
if ($attribute->getUsedForSortBy()
&& !in_array($attribute->getBackendType(), ['int', 'smallint', 'decimal'], true)
) {
return $attributeCode . '.sort_' . $attributeCode;
}
return $attributeCode;
}
}
+121
View File
@@ -0,0 +1,121 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search;
use Magento\Framework\Api\Search\DocumentInterface;
use Magento\Framework\Api\CustomAttributesDataInterface;
use Magento\Framework\Api\AttributeInterface;
class GetResponse
{
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager
) {
$this->objectManager = $objectManager;
}
/**
* @param array $elasticDocuments
* @param array $rawAggregations
* @param int $elasticTotal
* @return mixed
*/
public function execute(array $elasticDocuments, array $rawAggregations, $elasticTotal = 0)
{
$documents = $this->getDocuments($elasticDocuments);
$aggregations = $this->getAggregations($rawAggregations);
return $this->objectManager->create(
\Magento\Framework\Search\Response\QueryResponse::class,
[
'documents' => $documents,
'aggregations' => $aggregations,
'total' => $elasticTotal ? $elasticTotal : count($documents)
]
);
}
/**
* @param array[] $elasticDocuments
* @return \Magento\Framework\Api\Search\Document[]
*/
private function getDocuments(array $elasticDocuments)
{
$documents = [];
foreach ($elasticDocuments as $rawDocument) {
$attributes = [];
$documentId = null;
foreach ($rawDocument as $fieldName => $value) {
if ($fieldName === '_id') {
$documentId = $value;
} elseif ($fieldName === '_score') {
$attributes['score'] = $this->objectManager->create(
\Magento\Framework\Api\AttributeValue::class,
[
'data' => [
AttributeInterface::ATTRIBUTE_CODE => $fieldName,
AttributeInterface::VALUE => $value
]
]
);
}
}
$documents[] = $this->objectManager->create(
\Magento\Framework\Api\Search\Document::class,
[
'data' => [
DocumentInterface::ID => $documentId,
CustomAttributesDataInterface::CUSTOM_ATTRIBUTES => $attributes
]
]
);
}
return $documents;
}
/**
* @param array[] $rawAggregations
* @return \Magento\Framework\Search\Response\Aggregation
*/
private function getAggregations(array $rawAggregations)
{
$buckets = [];
foreach ($rawAggregations as $bucketName => $aggregation) {
$values = [];
foreach ($aggregation as $aggregationName => $metrics) {
$values[] = $this->objectManager->create(
\Magento\Framework\Search\Response\Aggregation\Value::class,
[
'value' => $aggregationName,
'metrics' => $metrics,
]
);
}
$buckets[$bucketName] = $this->objectManager->create(
\Magento\Framework\Search\Response\Bucket::class,
[
'name' => $bucketName,
'values' => $values
]
);
}
return $this->objectManager->create(
\Magento\Framework\Search\Response\Aggregation::class,
['buckets' => $buckets]
);
}
}
@@ -0,0 +1,152 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search\GetResponse;
use Amasty\ElasticSearch\Model\Search\DataProviderFactory;
use Magento\CatalogSearch\Model\Indexer\Fulltext;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Search\Dynamic\Algorithm\Repository;
use Magento\Framework\Search\Dynamic\EntityStorage;
use Magento\Framework\Search\Dynamic\EntityStorageFactory;
use Magento\Framework\Search\Request\BucketInterface;
use Magento\Framework\Search\Request\Dimension;
class GetAggregations
{
/**
* @var Repository
*/
private $dynamicAlgorithmRepository;
/**
* @var EntityStorageFactory
*/
private $dynamicEntityStorageFactory;
/**
* @var DataProviderFactory
*/
private $dataProviderFactory;
public function __construct(
Repository $dynamicAlgorithmRepository,
EntityStorageFactory $dynamicEntityStorageFactory,
DataProviderFactory $dataProviderFactory
) {
$this->dynamicAlgorithmRepository = $dynamicAlgorithmRepository;
$this->dynamicEntityStorageFactory = $dynamicEntityStorageFactory;
$this->dataProviderFactory = $dataProviderFactory;
}
/**
* @param \Magento\Framework\Search\RequestInterface $request
* @param array $elasticResponse
* @return array
* @throws LocalizedException
*/
public function execute(\Magento\Framework\Search\RequestInterface $request, array $elasticResponse)
{
$aggregations = [];
$buckets = $request->getAggregation();
foreach ($buckets as $bucket) {
switch ($bucket->getType()) {
case BucketInterface::TYPE_DYNAMIC:
$aggregations[$bucket->getName()] = $this
->getDynamicBucket($bucket, $request->getDimensions(), $elasticResponse);
break;
case BucketInterface::TYPE_TERM:
$aggregations[$bucket->getName()] = $this->getTermBucket($bucket, $elasticResponse);
break;
default:
throw new LocalizedException(__("Incorrect Bucket type: %1.", $bucket->getType()));
}
}
return $aggregations;
}
/**
* @param BucketInterface $bucket
* @param array $elasticResponse
* @return array
*/
public function getTermBucket(BucketInterface $bucket, array $elasticResponse)
{
$values = [];
foreach ($elasticResponse['aggregations'][$bucket->getName()]['buckets'] as $resultBucket) {
$values[$resultBucket['key']] = [
'value' => $resultBucket['key'],
'count' => $resultBucket['doc_count'],
];
}
return $values;
}
/**
* @param BucketInterface $bucket
* @param Dimension[] $dimensions
* @param array $elasticResponse
* @return array
* @throws LocalizedException
*/
public function getDynamicBucket(BucketInterface $bucket, array $dimensions, array $elasticResponse)
{
$dataProvider = $this->dataProviderFactory->create(
Fulltext::INDEXER_ID,
$bucket->getField()
);
$algorithm = $this->dynamicAlgorithmRepository->get(
$bucket->getMethod(),
['dataProvider' => $dataProvider]
);
$data = $algorithm->getItems(
$bucket,
$dimensions,
$this->convertQuerytoEntity($elasticResponse)
);
return $this->processFromToData($data);
}
/**
* @param array $data
* @return array
*/
private function processFromToData($data)
{
$result = [];
foreach ($data as $value) {
$from = is_numeric($value['from']) ? $value['from'] : '';
$to = is_numeric($value['to']) ? $value['to'] : '';
unset($value['from'], $value['to']);
$fromToValue = "{$from}_{$to}";
// phpcs:ignore
$result[$fromToValue] = array_merge(['value' => $fromToValue], $value);
}
return $result;
}
/**
* @param array $queryResult
* @return EntityStorage
*/
private function convertQuerytoEntity(array $queryResult)
{
$documentIds = [];
if (isset($queryResult['hits']['hits']) && !empty($queryResult['hits']['hits'])) {
foreach ($queryResult['hits']['hits'] as $document) {
$documentIds[] = $document['_id'];
}
}
return $this->dynamicEntityStorageFactory->create($documentIds);
}
}
@@ -0,0 +1,287 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search\GetResponse\GetAggregations;
use Amasty\ElasticSearch\Model\Search\Adapter;
use Magento\Framework\Search\Dynamic\IntervalInterface;
use Amasty\ElasticSearch\Model\Config;
use Magento\CatalogSearch\Model\Indexer\Fulltext;
use Amasty\ElasticSearch\Model\Client\ClientRepositoryInterface;
class Interval implements IntervalInterface
{
/**
* Minimal possible value
*/
public const DELTA = 0.005;
/**
* @var Config
*/
private $clientConfig;
/**
* @var ClientRepositoryInterface
*/
private $clientRepository;
/**
* @var \Magento\Framework\Registry
*/
private $registry;
/**
* @var string
*/
private $fieldName;
/**
* @var string
*/
private $storeId;
/**
* @var array
*/
private $entityIds;
public function __construct(
Config $clientConfig,
ClientRepositoryInterface $clientRepository,
\Magento\Framework\Registry $registry,
$fieldName,
$storeId,
$entityIds
) {
$this->clientConfig = $clientConfig;
$this->clientRepository = $clientRepository;
$this->registry = $registry;
$this->fieldName = $fieldName;
$this->storeId = $storeId;
$this->entityIds = $entityIds;
}
/**
* @return array
*/
private function getFilterMustTerms()
{
$query = $this->registry->registry(Adapter::REQUEST_QUERY);
if ($query && !empty($query['bool']['must'])) {
$filterMustTerms = $query['bool']['must'];
} else {
$filterMustTerms = [
[
'terms' => [
'_id' => $this->entityIds,
],
]
];
}
return $filterMustTerms;
}
/**
* {@inheritdoc}
*/
public function load($limit, $offset = null, $lower = null, $upper = null)
{
$fromValue = ($lower) ? ['gte' => $lower - self::DELTA] : [];
$toValue = ($upper) ? ['lt' => $upper - self::DELTA] : [];
$client = $this->clientRepository->get();
$range = [];
if ($lower !== null && $upper!== null) {
$range = [
[
'range' => [
$this->fieldName => array_merge($fromValue, $toValue),
],
],
];
}
$requestQuery = [
'index' => $client->getIndexName(Fulltext::INDEXER_ID, $this->storeId),
'type' => $this->clientConfig->getEntityType(),
'body' => [
'stored_fields' => [
'_id'
],
'query' => [
'bool' => [
'must' => [
'match_all' => ['boost' => 1]
],
'filter' => [
'bool' => [
'must' => array_merge($this->getFilterMustTerms(), $range),
],
],
],
],
'sort' => [
$this->fieldName,
],
'size' => $limit,
],
];
if ($offset) {
$requestQuery['body']['from'] = $offset;
}
$queryResult = $client->search($requestQuery);
return $this->convertToFloat($queryResult['hits']['hits']);
}
/**
* {@inheritdoc}
*/
public function loadPrevious($data, $index, $lower = null)
{
$fromValue = ($lower) ? ['gte' => $lower - self::DELTA] : [];
$toValue = ($data) ? ['lt' => $data - self::DELTA] : [];
$client = $this->clientRepository->get();
$range = [];
if ($fromValue || $toValue) {
$range = [
[
'range' => [
$this->fieldName => array_merge($fromValue, $toValue),
],
]
];
}
$requestQuery = [
'index' => $client->getIndexName(Fulltext::INDEXER_ID, $this->storeId),
'type' => $this->clientConfig->getEntityType(),
'body' => [
'stored_fields' => [
'_id'
],
'query' => [
'bool' => [
'must' => [
'match_all' => ['boost' => 1]
],
'filter' => [
'bool' => [
'must' => array_merge($this->getFilterMustTerms(), $range),
],
],
],
],
'sort' => [
$this->fieldName,
],
],
];
$queryResult = $client->search($requestQuery);
$offset = $queryResult['hits']['total']['value'] ?? $queryResult['hits']['total'] ?? 0;
if (!$offset) {
return false;
}
return $this->load($index - $offset + 1, $offset - 1, $lower);
}
/**
* {@inheritdoc}
*/
public function loadNext($data, $rightIndex, $upper = null)
{
$fromValue = ($data) ? ['gte' => $data - self::DELTA] : [];
$toValue = ($data) ? ['lt' => $data - self::DELTA] : [];
$client = $this->clientRepository->get();
$range = [];
if ($fromValue || $toValue) {
$range = [
'range' => [
$this->fieldName => array_merge($fromValue, $toValue),
],
];
}
$requestCountQuery = [
'index' => $client->getIndexName(Fulltext::INDEXER_ID, $this->storeId),
'type' => $this->clientConfig->getEntityType(),
'body' => [
'stored_fields' => [
'_id'
],
'query' => [
'bool' => [
'must' => [
'match_all' => ['boost' => 1]
],
'filter' => [
'bool' => [
'must' => [
[
'terms' => [
'_id' => $this->entityIds,
],
],
$range
],
],
],
],
],
'sort' => [
$this->fieldName,
],
],
];
$queryCountResult = $client->search($requestCountQuery);
$offset = $queryCountResult['hits']['total']['value'] ?? $queryCountResult['hits']['total'] ?? 0;
if (!$offset) {
return false;
}
$fromValue = ['gte' => $data - self::DELTA];
if ($upper !== null) {
$toValue = ['lt' => $data - self::DELTA];
}
$requestQuery = $requestCountQuery;
$requestCountQuery['body']['query']['filtered']['filter']['bool']['must']['range'] =
[$this->fieldName => array_merge($fromValue, $toValue)];
$requestCountQuery['body']['from'] = $offset - 1;
$requestCountQuery['body']['size'] = $rightIndex - $offset + 1;
$queryResult = $this->clientRepository->get()->search($requestQuery);
return array_reverse($this->convertToFloat($queryResult['hits']['hits']));
}
/**
* @param array $hits
* @param string $fieldName
*
* @return float[]
*/
private function convertToFloat($hits)
{
$returnPrices = [];
foreach ($hits as $hit) {
$returnPrices[] = (float) $hit['sort'][0];
}
return $returnPrices;
}
}
+188
View File
@@ -0,0 +1,188 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search;
use Magento\Framework\Search\RequestInterface;
use Magento\Framework\Search\Response\QueryResponse;
use Amasty\Base\Debug\Log;
class Logger
{
public const LOG_FILE_NAME_DEFAULT = 'amasty_elastic.log';
public const LOG_FILE_NAME_BUCKETS = 'amasty_elastic_buckets.log';
public const LOG_DEPTH_ARRAY = 10;
public const LOG_DEPTH_OBJECT = 10;
/**
* @var \Amasty\ElasticSearch\Model\Config
*/
private $config;
/**
* @var \Amasty\ElasticSearch\Model\Config\Backend\Debug
*/
private $debugConfig;
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;
public function __construct(
\Amasty\ElasticSearch\Model\Config $config,
\Amasty\ElasticSearch\Model\Config\Backend\Debug $debugConfig,
\Psr\Log\LoggerInterface $logger
) {
$this->config = $config;
$this->debugConfig = $debugConfig;
$this->logger = $logger;
}
/**
* @param RequestInterface $request
* @param QueryResponse $response
* @param array $rawRequestQuery
* @param array $rawResponseQuery
* @return $this
*/
public function log(
RequestInterface $request = null,
QueryResponse $response = null,
array $rawRequestQuery = [],
array $rawResponseQuery = []
) {
if ($this->isLogActive()) {
Log::setLogFile(self::LOG_FILE_NAME_DEFAULT);
Log::setArrayDepthLevel(self::LOG_DEPTH_ARRAY);
Log::setObjectDepthLevel(self::LOG_DEPTH_OBJECT);
$buckets = $request->getAggregation();
if (count($buckets) > 1) {
$this->logRequest($request, $response, $rawRequestQuery, $rawResponseQuery);
} elseif ($this->logAllRequests()) {
Log::setLogFile(self::LOG_FILE_NAME_BUCKETS);
$this->logRequest($request, $response, $rawRequestQuery, $rawResponseQuery);
}
}
return $this;
}
/**
* @param RequestInterface $request
* @param QueryResponse $response
* @param array $rawRequestQuery
* @param array $rawResponseQuery
* @return $this
*/
private function logRequest(
RequestInterface $request = null,
QueryResponse $response = null,
array $rawRequestQuery = [],
array $rawResponseQuery = []
) {
if ($this->logRequestObject()) {
Log::execute(['Request Object' => $request]);
}
if ($this->logRawRequest()) {
Log::execute(['Raw Request' => $rawRequestQuery]);
}
if ($this->logRawResponse()) {
Log::execute(['Raw Response' => $rawResponseQuery]);
}
if ($this->logResponseObject()) {
Log::execute(['Response Object' => $response]);
}
return $this;
}
/**
* @return bool
*/
private function logAllRequests()
{
return (bool)$this->config->getDebugConfig('log_buckets_alert_requests');
}
/**
* @return bool
*/
private function isLogActive()
{
if ($value = $this->config->getDebugConfig('use_debug')) {
return $this->debugConfig->isActive($value);
}
return false;
}
/**
* @return bool
*/
private function logRequestObject()
{
return (bool)$this->config->getDebugConfig('log_request_object');
}
/**
* @return bool
*/
private function logRawRequest()
{
return (bool)$this->config->getDebugConfig('log_raw_request');
}
/**
* @return bool
*/
private function logRawResponse()
{
return (bool)$this->config->getDebugConfig('log_raw_response');
}
/**
* @return bool
*/
private function logResponseObject()
{
return (bool)$this->config->getDebugConfig('log_response_object');
}
/**
* @param string $message
* @param array $context
* @return void|null
*/
public function logCritical($message, array $context = [])
{
return $this->logger->critical($message, $context);
}
/**
* @param string $message
* @param array $context
* @return void|null
*/
public function logDebug($message, array $context = [])
{
return $this->logger->debug($message, $context);
}
/**
* @param string $message
* @param array $context
* @return void|null
*/
public function logError($message, array $context = [])
{
return $this->logger->error($message, $context);
}
}
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Search\SubQuery;
use Magento\Framework\Stdlib\StringUtils;
class Helper
{
public const QUERY_VAR_NAME = 'sub_query';
/**
* @var string|null
*/
private $queryText;
/**
* @var StringUtils
*/
private $stringUtils;
public function __construct(StringUtils $stringUtils)
{
$this->stringUtils = $stringUtils;
}
public function setQueryText(string $queryText): void
{
$this->queryText = $this->stringUtils->cleanString($queryText);
}
public function getQueryText(): ?string
{
return $this->queryText;
}
public function getQueryParamName(): string
{
return static::QUERY_VAR_NAME;
}
}