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
@@ -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;
}
}