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,46 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\Structure;
use Amasty\ElasticSearch\Api\Data\Indexer\Structure\AnalyzerBuilderInterface;
use Amasty\ElasticSearch\Model\Indexer\Structure\AnalyserBuilderFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
class AnalyserBuilder implements AnalyzerBuilderInterface
{
/**
* @var AnalyserBuilderFactory
*/
private $analyserBuilderFactory;
/**
* @var \Amasty\ElasticSearch\Model\Config
*/
private $config;
public function __construct(
AnalyserBuilderFactory $analyserBuilderFactory,
\Amasty\ElasticSearch\Model\Config $config
) {
$this->analyserBuilderFactory = $analyserBuilderFactory;
$this->config = $config;
}
/**
* @param int $storeId
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function build($storeId)
{
$analyser = $this->analyserBuilderFactory->create(
$this->config->getAnalyzerType($storeId)
);
return $analyser->build($storeId);
}
}
@@ -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\Indexer\Structure;
use Amasty\ElasticSearch\Api\Data\Indexer\Structure\AnalyzerBuilderInterface;
use Amasty\ElasticSearch\Model\Indexer\Structure\AnalyzerBuilder\DefaultBuilder;
use Amasty\ElasticSearch\Model\Source\CustomAnalyzer;
use Magento\Framework\ObjectManagerInterface;
/**
* Class AnalyserBuilder
*/
class AnalyserBuilderFactory
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var CustomAnalyzersMetaInfoProvider
*/
private $customAnalyzersMetaInfoProvider;
public function __construct(
ObjectManagerInterface $objectManager,
CustomAnalyzersMetaInfoProvider $customAnalyzersMetaInfoProvider
) {
$this->objectManager = $objectManager;
$this->customAnalyzersMetaInfoProvider = $customAnalyzersMetaInfoProvider;
}
/**
* @param array $data
* @return AnalyzerBuilderInterface
*/
public function create($type): AnalyzerBuilderInterface
{
return $this->objectManager->create($this->getBuilderInstanceName($type));
}
/**
* @param string $type
* @return string
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function getBuilderInstanceName($type): string
{
return $type === CustomAnalyzer::DISABLED
? DefaultBuilder::class : $this->customAnalyzersMetaInfoProvider->getAnalyzerClass($type);
}
}
@@ -0,0 +1,210 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\Structure\AnalyzerBuilder;
use Amasty\ElasticSearch\Api\Data\Indexer\Structure\AnalyzerBuilderInterface;
use Amasty\ElasticSearch\Model\Client\ClientRepositoryInterface;
use Amasty\ElasticSearch\Model\Config;
class DefaultBuilder implements AnalyzerBuilderInterface
{
public const WORD_DELIMITER_VERSION_5 = 'word_delimiter';
public const WORD_DELIMITER_VERSION_6 = 'word_delimiter_graph';
public const LOWERCASE_NORMALIZER = 'useLowercase';
/**
* @var EntityCollectionProvider
*/
private $entityCollectionProvider;
/**
* @var Config
*/
private $config;
/**
* @var ClientRepositoryInterface
*/
private $clientRepository;
public function __construct(
EntityCollectionProvider $entityCollectionProvider,
Config $config,
ClientRepositoryInterface $clientRepository
) {
$this->entityCollectionProvider = $entityCollectionProvider;
$this->config = $config;
$this->clientRepository = $clientRepository;
}
/**
* @param int $storeId
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function build($storeId)
{
$analyser = [
'analyzer' => [
//"the A*b-1^2 O'Neil's" => ["ab12", "oneil"]
'default' => [
'type' => 'custom',
'tokenizer' => 'whitespace',
'filter' => [
'stop_filter',
"graph_synonyms",
'lowercase',
$this->getWordDelimiterFilter()
],
],
//"the A*b-1^2 O'Neil's" => ["a*b-1^2", "o'neil's"]
'allow_spec_chars' => [
'type' => 'custom',
'tokenizer' => 'whitespace',
'filter' => [
'stop_filter',
'graph_synonyms',
'lowercase'
],
],
'stem' => [
'type' => 'custom',
'tokenizer' => 'whitespace',
'filter' => [
'stop_filter',
'lowercase'
]
]
],
'filter' => [
$this->getWordDelimiterFilter() => [
// https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-word-delimiter-graph-tokenfilter.html
'type' => $this->getWordDelimiterFilter(),
'catenate_all' => true,
'catenate_words' => false,
'catenate_numbers' => false,
//^ catenate all
'generate_word_parts' => false,
'generate_number_parts' => false,
'split_on_case_change' => false,
'preserve_original' => true,
'split_on_numerics' => false,
],
'stop_filter' => [
"type" => "stop",
"stopwords" => $this->getStopWords($storeId)
],
"graph_synonyms" => [
"type" => "synonym_graph",
"lenient" => true,
"synonyms" => $this->getSynonyms($storeId)
]
],
"normalizer" => [
self::LOWERCASE_NORMALIZER => [
"type" => "custom",
"filter" => ["lowercase"]
]
]
];
if ($stemmingData = $this->getStemmingData($storeId)) {
$analyser['filter']['stemming'] = [
'type' => 'stemmer',
'language' => $stemmingData
];
$analyser['analyzer']['stem']['filter'][] = 'stemming';
}
$charMappings = $this->config->getCharMappings($storeId);
if ($charMappings) {
$analyser['char_filter']['mapped_char_filter'] = [
'type' => 'mapping',
'mappings' => $charMappings
];
$analyser['analyzer']['default']['char_filter'] = 'mapped_char_filter';
$analyser['analyzer']['allow_spec_chars']['char_filter'] = 'mapped_char_filter';
}
return $analyser;
}
/**
* @param $storeId
* @return array|string
*/
private function getStopWords($storeId)
{
$usePredefined = $this->config->getUsePredefinedStopwords($storeId);
if ($usePredefined) {
return sprintf(
'_%s_',
$this->config->getStopWordsLanguage($storeId)
);
} else {
$stopWords = [];
$collection = $this->entityCollectionProvider->getStopWordCollectionFactory()->create();
$collection->addStoreFilter($storeId);
foreach ($collection as $stopWord) {
$stopWords[] = preg_replace('/\s*/', '', $stopWord->getTerm());
}
if (!count($stopWords)) {
$stopWords = '_none_';
}
}
return $stopWords;
}
/**
* @param $storeId
* @return string
*/
private function getStemmingData($storeId)
{
$usePredefined = $this->config->getUsePredefinedStemming($storeId);
if ($usePredefined) {
return $this->config->getStemmedLanguage($storeId);
}
return '';
}
/**
* @param $storeId
* @return array
*/
private function getSynonyms($storeId)
{
$synonyms = [];
$collection = $this->entityCollectionProvider->getSynonymCollectionFactory()->create();
$collection->addStoreFilter($storeId);
foreach ($collection as $synonym) {
$synonyms[] = trim($synonym->getTerm());
}
return $synonyms ?: ['']; //can't pass empty array to elastic 5.x
}
/**
* @return string
*/
private function getWordDelimiterFilter()
{
$elasticVersion = $this->clientRepository->get()->getCurrentEngineVersion();
if (version_compare($elasticVersion, '5.0.0', '>=')
&& version_compare($elasticVersion, '6.0.0', '<=')
) {
return self::WORD_DELIMITER_VERSION_5;
}
return self::WORD_DELIMITER_VERSION_6;
}
}
@@ -0,0 +1,48 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\Structure\AnalyzerBuilder;
use Amasty\ElasticSearch\Model\ResourceModel\StopWord\CollectionFactory as StopWordCollectionFactory;
use Amasty\ElasticSearch\Model\ResourceModel\Synonym\CollectionFactory as SynoymCollectionFactory;
class EntityCollectionProvider
{
/**
* @var StopWordCollectionFactory
*/
private $stopWordCollectionFactory;
/**
* @var SynoymCollectionFactory
*/
private $synoymCollectionFactory;
public function __construct(
StopWordCollectionFactory $stopWordCollectionFactory,
SynoymCollectionFactory $synoymCollectionFactory
) {
$this->stopWordCollectionFactory = $stopWordCollectionFactory;
$this->synoymCollectionFactory = $synoymCollectionFactory;
}
/**
* @return StopWordCollectionFactory
*/
public function getStopWordCollectionFactory()
{
return $this->stopWordCollectionFactory;
}
/**
* @return SynoymCollectionFactory
*/
public function getSynonymCollectionFactory()
{
return $this->synoymCollectionFactory;
}
}
@@ -0,0 +1,173 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\Structure\AnalyzerBuilder;
use Amasty\ElasticSearch\Api\Data\Indexer\Structure\AnalyzerBuilderInterface;
use Amasty\ElasticSearch\Model\Indexer\Structure\AnalyzerBuilder\EntityCollectionProvider;
use Amasty\ElasticSearch\Model\Source\KuromojiReadingForm;
class Kuromoji implements AnalyzerBuilderInterface
{
/**
* @var EntityCollectionProvider
*/
private $entityCollectionProvider;
/**
* @var \Amasty\ElasticSearch\Model\Config
*/
private $config;
public function __construct(
EntityCollectionProvider $entityCollectionProvider,
\Amasty\ElasticSearch\Model\Config $config
) {
$this->entityCollectionProvider = $entityCollectionProvider;
$this->config = $config;
}
/**
* @param int $storeId
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function build($storeId)
{
$filters = $this->getFilters($storeId);
$analyzerFilters = array_merge(array_keys($filters), ['lowercase']);
$tokenizer = $this->getTokenizer($storeId);
$analyser = [
'analyzer' => [
'default' => [
'type' => 'custom',
'tokenizer' => key($tokenizer),
'filter' => $analyzerFilters
],
'stem' => [
'type' => 'custom',
'tokenizer' => 'whitespace',
'filter' => [
'stop_filter',
'lowercase'
]
]
],
'tokenizer' => $tokenizer,
'filter' => $this->getFilters($storeId)
];
return $analyser;
}
private function getFilters($storeId)
{
$filters = [
'stop_filter' => [
"type" => "ja_stop",
"stopwords" => $this->getStopWords($storeId)
],
"graph_synonyms" => [
"type" => "synonym_graph",
"lenient" => true,
"synonyms" => $this->getSynonyms($storeId)
]
];
if ($stemmingData = $this->getStemmingData($storeId)) {
$filters['stemming'] = [
'type' => 'kuromoji_stemmer',
'minimum_length' => 4
];
}
if ($readingForm = $this->config->getKuromojiReadingForm($storeId)) {
switch ($readingForm) {
case KuromojiReadingForm::ROMAJI:
$filters['romaji_readingform'] = [
'type' => 'kuromoji_readingform',
'use_romaji' => true
];
break;
case KuromojiReadingForm::KATAKANA:
$filters['romaji_readingform'] = [
'type' => 'kuromoji_readingform',
'use_romaji' => false
];
break;
}
}
return $filters;
}
/**
* @param int $storeId
* @return array
*/
private function getTokenizer($storeId)
{
return [
'kuromoji_custom_tokenizer' => [
'type' => 'kuromoji_tokenizer',
'mode' => $this->config->getKoromojiTokenMode($storeId),
]
];
}
/**
* @param $storeId
* @return array|string
*/
private function getStopWords($storeId)
{
$usePredefined = $this->config->getUsePredefinedStopwords($storeId);
if ($usePredefined) {
return "_japanese_";
} else {
$stopWords = [];
$collection = $this->entityCollectionProvider->getStopWordCollectionFactory()->create();
$collection->addStoreFilter($storeId);
foreach ($collection as $stopWord) {
$stopWords[] = preg_replace('/\s*/u', '-', $stopWord->getTerm());
}
if (!count($stopWords)) {
$stopWords = '_none_';
}
}
return $stopWords;
}
/**
* @param $storeId
* @return array|string
*/
private function getStemmingData($storeId)
{
return $this->config->getUsePredefinedStemming($storeId);
}
/**
* @param $storeId
* @return array
*/
private function getSynonyms($storeId)
{
$synonyms = [];
$collection = $this->entityCollectionProvider->getSynonymCollectionFactory()->create();
$collection->addStoreFilter($storeId);
foreach ($collection as $synonym) {
$synonyms[] = trim($synonym->getTerm());
}
return $synonyms ?: ['']; //can't pass empty array to elastic 5.x
}
}
@@ -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\Indexer\Structure\AnalyzerBuilder;
use Amasty\ElasticSearch\Api\Data\Indexer\Structure\AnalyzerBuilderInterface;
use Amasty\ElasticSearch\Model\Indexer\Structure\AnalyzerBuilder\EntityCollectionProvider;
use Magento\Framework\App\Config\ScopeConfigInterface;
class Nori implements AnalyzerBuilderInterface
{
/**
* @var EntityCollectionProvider
*/
private $entityCollectionProvider;
/**
* @var \Amasty\ElasticSearch\Model\Config
*/
private $config;
public function __construct(
EntityCollectionProvider $entityCollectionProvider,
\Amasty\ElasticSearch\Model\Config $config
) {
$this->entityCollectionProvider = $entityCollectionProvider;
$this->config = $config;
}
/**
* @param int $storeId
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function build($storeId)
{
$filters = $this->getFilters($storeId);
$defaultFilters = [];
if ($readingForm = $this->config->getUseNoriReadingForm($storeId)) {
$defaultFilters[] = 'nori_readingform';
}
$analyzerFilters = array_merge($defaultFilters, array_keys($filters), ['lowercase']);
$tokenizer = $this->getTokenizer($storeId);
$analyser = [
'analyzer' => [
'default' => [
'type' => 'custom',
'tokenizer' => key($tokenizer),
'filter' => $analyzerFilters
]
],
'tokenizer' => $tokenizer,
'filter' => $filters
];
return $analyser;
}
private function getFilters($storeId)
{
$filters = [
'stop_filter' => [
"type" => "stop",
"stopwords" => $this->getStopWords($storeId)
],
"graph_synonyms" => [
"type" => "synonym_graph",
"lenient" => true,
"synonyms" => $this->getSynonyms($storeId)
]
];
return $filters;
}
/**
* @param int $storeId
* @return array
*/
private function getTokenizer($storeId)
{
return [
'nori_custom_tokenizer' => [
'type' => 'nori_tokenizer',
'mode' => $this->config->getNoriTokenMode($storeId),
]
];
}
/**
* @param $storeId
* @return array|string
*/
private function getStopWords($storeId)
{
$stopWords = [];
$collection = $this->entityCollectionProvider->getStopWordCollectionFactory()->create();
$collection->addStoreFilter($storeId);
foreach ($collection as $stopWord) {
$stopWords[] = preg_replace('/\s*/u', '-', $stopWord->getTerm());
}
if (!count($stopWords)) {
$stopWords = '_none_';
}
return $stopWords;
}
/**
* @param $storeId
* @return array
*/
private function getSynonyms($storeId)
{
$synonyms = [];
$collection = $this->entityCollectionProvider->getSynonymCollectionFactory()->create();
$collection->addStoreFilter($storeId);
foreach ($collection as $synonym) {
$synonyms[] = trim($synonym->getTerm());
}
return $synonyms ?: ['']; //can't pass empty array to elastic 5.x
}
}
@@ -0,0 +1,96 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\Structure\AnalyzerBuilder;
use Amasty\ElasticSearch\Api\Data\Indexer\Structure\AnalyzerBuilderInterface;
use Amasty\ElasticSearch\Model\Indexer\Structure\AnalyzerBuilder\EntityCollectionProvider;
class Smartcn implements AnalyzerBuilderInterface
{
/**
* @var EntityCollectionProvider
*/
private $entityCollectionProvider;
public function __construct(EntityCollectionProvider $entityCollectionProvider)
{
$this->entityCollectionProvider = $entityCollectionProvider;
}
/**
* @param int $storeId
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function build($storeId)
{
$analyser = [
'analyzer' => [
'default' => [
'type' => 'custom',
'tokenizer' => 'smartcn_tokenizer',
'filter' => [
'stop_filter',
"graph_synonyms",
'lowercase'
],
]
],
'filter' => [
'stop_filter' => [
"type" => "stop",
"stopwords" => $this->getStopWords($storeId)
],
"graph_synonyms" => [
"type" => "synonym_graph",
"lenient" => true,
"synonyms" => $this->getSynonyms($storeId)
]
],
];
return $analyser;
}
/**
* @param $storeId
* @return array|string
*/
private function getStopWords($storeId)
{
$stopWords = [];
$collection = $this->entityCollectionProvider->getStopWordCollectionFactory()->create();
$collection->addStoreFilter($storeId);
foreach ($collection as $stopWord) {
$stopWords[] = preg_replace('/\s*/u', '-', $stopWord->getTerm());
}
if (!count($stopWords)) {
$stopWords = '_none_';
}
return $stopWords;
}
/**
* @param $storeId
* @return array
*/
private function getSynonyms($storeId)
{
$synonyms = [];
$collection = $this->entityCollectionProvider->getSynonymCollectionFactory()->create();
$collection->addStoreFilter($storeId);
foreach ($collection as $synonym) {
$synonyms[] = trim($synonym->getTerm());
}
return $synonyms ?: ['']; //can't pass empty array to elastic 5.x
}
}
@@ -0,0 +1,70 @@
<?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\Indexer\Structure;
use Magento\Framework\Exception\NoSuchEntityException;
/**
* Endpoint to extend the module by adding a custom analyzer
*
* @api
*/
class CustomAnalyzersMetaInfoProvider
{
/**
* @var array[]
*/
private $customAnalyzersMetaInfo = [];
public function __construct(
array $customAnalyzersMetaInfo = []
) {
$this->customAnalyzersMetaInfo = $customAnalyzersMetaInfo;
}
/**
* @return array
*/
public function getAllAnalyzers(): array
{
return array_keys($this->customAnalyzersMetaInfo);
}
/**
* @param string $analyzerAlias
* @return string
* @throws NoSuchEntityException
*/
public function getAnalyzerClass(string $analyzerAlias): string
{
if (!isset($this->customAnalyzersMetaInfo[$analyzerAlias]['class'])) {
throw new NoSuchEntityException(__('Mapping not found for custom analyzer alias %1', $analyzerAlias));
}
return $this->customAnalyzersMetaInfo[$analyzerAlias]['class'];
}
/**
* @param string $analyzerAlias
* @return string
* @throws NoSuchEntityException
*/
public function getAnalyzerLabel(string $analyzerAlias): string
{
if (!isset($this->customAnalyzersMetaInfo[$analyzerAlias]['label'])) {
throw new NoSuchEntityException(
__('Invalid mapping for custom analyzer %1. Label not found.', $analyzerAlias)
);
}
return $this->customAnalyzersMetaInfo[$analyzerAlias]['label'];
}
}
@@ -0,0 +1,27 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\Structure\DynamicTemplate;
class CategoryPosition
{
/**
* @param $storeId
* @return array
*/
public function map($storeId)
{
return [
'match' => 'category_position_*',
'match_mapping_type' => 'string',
'mapping' => [
'type' => 'integer',
'index' => true,
]
];
}
}
@@ -0,0 +1,39 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\Structure;
class DynamicTemplateMapper
{
/**
* @var array
*/
private $mappers = [];
public function __construct(array $mappers = [])
{
foreach ($mappers as $key => $mapper) {
if (method_exists($mapper, 'map')) {
$this->mappers[$key] = $mapper;
}
}
}
/**
* @param int $storeId
* @return array
*/
public function map($storeId)
{
$mappedData = [];
foreach ($this->mappers as $key => $mapper) {
$mappedData[$key] = $mapper->map($storeId);
}
return $mappedData;
}
}
@@ -0,0 +1,41 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\Structure;
use Amasty\ElasticSearch\Api\Data\Indexer\Structure\EntityBuilderInterface;
class EntityBuilder
{
/**
* @var EntityBuilderInterface[]
*/
private $entityBuilders;
public function __construct(
array $entityBuilders = []
) {
$this->entityBuilders = $entityBuilders;
}
/**
* @param string $indexerId
* @return array
*/
public function build($indexerId)
{
$fieldArray = [];
if (isset($this->entityBuilders[$indexerId]) && is_array($this->entityBuilders[$indexerId])) {
foreach ($this->entityBuilders[$indexerId] as $builder) {
// phpcs:ignore Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge
$fieldArray = array_merge($fieldArray, $builder->buildEntityFields());
}
}
return $fieldArray;
}
}
@@ -0,0 +1,39 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\Structure\EntityBuilder\External;
use Amasty\ElasticSearch\Api\Data\Indexer\Structure\EntityBuilderInterface;
class Finder implements EntityBuilderInterface
{
/**
* @return array
*/
public function buildEntityFields()
{
$result = [];
foreach ($this->getExtraValueFields() as $code => $type) {
$result[$code . '_value'] = ['type' => $type];
}
return $result;
}
/**
* Get fields for +'_value' replication.
*
* Keep it public in a case of customizations via plugin
* @return string[]
*/
public function getExtraValueFields()
{
return [
'sku' => 'keyword'
];
}
}
@@ -0,0 +1,167 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\Structure\EntityBuilder;
use Amasty\ElasticSearch\Api\Data\Indexer\Structure\EntityBuilderInterface;
use Amasty\ElasticSearch\Model\Config as ConfigProvider;
use Amasty\ElasticSearch\Model\Indexer\Structure\AnalyzerBuilder\DefaultBuilder;
use Amasty\ElasticSearch\Utils\AttributeUtil;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;
use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Framework\App\ObjectManager;
class Product implements EntityBuilderInterface
{
public const ATTRIBUTE_TYPE_TEXT = 'text';
public const ATTRIBUTE_TYPE_KEYWORD = 'keyword';
public const ATTRIBUTE_TYPE_FLOAT = 'float';
public const ATTRIBUTE_TYPE_INT = 'integer';
public const ATTRIBUTE_TYPE_DATE = 'date';
public const DEFAULT_ATTRIBUTE_CODE_ARRAY = ['category_ids', 'visibility'];
/**
* @var Config
*/
private $eavConfig;
/**
* @var array
*/
private $customerGroupIds;
/**
* @var ConfigProvider
*/
private $configProvider;
/**
* @var AttributeUtil
*/
private $attributeUtil;
public function __construct(
Config $eavConfig,
ConfigProvider $configProvider,
CollectionFactory $customerGroupCollectionFactory,
?AttributeUtil $attributeUtil = null // TODO: move to not optional argument and remove OM
) {
$this->eavConfig = $eavConfig;
$this->configProvider = $configProvider;
$customerGroupCollection = $customerGroupCollectionFactory->create();
$this->customerGroupIds = $customerGroupCollection->getAllIds();
$this->attributeUtil = $attributeUtil ?? ObjectManager::getInstance()->get(AttributeUtil::class);
}
/**
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function buildEntityFields()
{
$attributeCodes = $this->eavConfig->getEntityAttributeCodes(ProductAttributeInterface::ENTITY_TYPE_CODE);
$allAttributes = [];
$useCustomAnalyzer = $this->configProvider->useCustomAnalyzer();
foreach ($attributeCodes as $attributeCode) {
$attribute = $this->eavConfig->getAttribute(ProductAttributeInterface::ENTITY_TYPE_CODE, $attributeCode);
$attributeType = $this->getAttributeType($attribute);
$allAttributes[$attributeCode] = ['type' => $attributeType];
if ($attributeCode == "category_ids") {
$allAttributes[$attributeCode] = [
'type' => self::ATTRIBUTE_TYPE_INT
];
} elseif ($attributeCode == "sku") {
$allAttributes[$attributeCode]['fielddata'] = true;
}
if ($allAttributes[$attributeCode]['type'] == self::ATTRIBUTE_TYPE_DATE) {
$allAttributes[$attributeCode]['format'] = 'yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis';
} elseif ($attributeCode == "price") {
foreach ($this->customerGroupIds as $groupId) {
$allAttributes[$attributeCode . '_' . $groupId] = ['type' => self::ATTRIBUTE_TYPE_FLOAT];
}
}
if (!$this->attributeUtil->isComplexAttribute($attribute)
&& $attribute->getUsedForSortBy()
&& $attributeType == self::ATTRIBUTE_TYPE_TEXT
) {
$textAttributeConfig = [
'type' => self::ATTRIBUTE_TYPE_KEYWORD,
'index' => false,
];
if (!$useCustomAnalyzer) {
$textAttributeConfig['normalizer'] = DefaultBuilder::LOWERCASE_NORMALIZER;
}
$allAttributes[$attributeCode]['fields']['sort_' . $attributeCode] = $textAttributeConfig;
}
if ($this->attributeUtil->isComplexAttribute($attribute)) {
$allAttributes[$attributeCode]['type'] = self::ATTRIBUTE_TYPE_KEYWORD;
$allAttributes[$attributeCode . '_value'] = [
'type' => self::ATTRIBUTE_TYPE_TEXT,
];
}
}
return $allAttributes;
}
/**
* @param AbstractAttribute $attribute
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function isSearchable(AbstractAttribute $attribute)
{
return $attribute->getIsSearchable()
&& ($attribute->getIsVisibleInAdvancedSearch()
|| $attribute->getIsFilterable()
|| $attribute->getIsFilterableInSearch()
)
|| (in_array($attribute->getAttributeCode(), self::DEFAULT_ATTRIBUTE_CODE_ARRAY, true));
}
/**
* @param AbstractAttribute $attribute
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getAttributeType(AbstractAttribute $attribute)
{
$backendType = $attribute->getBackendType();
$frontendInput = $attribute->getFrontendInput();
if ((in_array($backendType, ['int', 'smallint'], true)
|| (in_array($frontendInput, ['select', 'boolean'], true) && $backendType !== 'varchar'))
&& !$attribute->getIsUserDefined()
) {
$fieldType = self::ATTRIBUTE_TYPE_INT;
} elseif ($backendType === 'decimal') {
$fieldType = self::ATTRIBUTE_TYPE_FLOAT;
} else {
if (!$this->isSearchable($attribute)) {
if ($attribute->getIsFilterable() || $attribute->getIsFilterableInSearch()) {
$fieldType = self::ATTRIBUTE_TYPE_KEYWORD;
} else {
$fieldType = self::ATTRIBUTE_TYPE_TEXT;
}
} else {
$fieldType = self::ATTRIBUTE_TYPE_TEXT;
}
}
return $fieldType;
}
}
@@ -0,0 +1,44 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\Structure\EntityBuilder\Product;
use Amasty\ElasticSearch\Api\Data\Indexer\Structure\EntityBuilderInterface;
use Amasty\ElasticSearch\Model\Indexer\Structure\EntityBuilder\Product;
class CategoryData implements EntityBuilderInterface
{
/**
* @var \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
*/
private $categoryCollectionFactory;
public function __construct(
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory
) {
$this->categoryCollectionFactory = $categoryCollectionFactory;
}
/**
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function buildEntityFields()
{
$fields = [];
$categoryIds = $this->categoryCollectionFactory->create()->getAllIds();
foreach ($categoryIds as $categoryId) {
$fields['category_position_' . $categoryId] = [
'type' => Product::ATTRIBUTE_TYPE_INT,
'index' => false
];
}
return $fields;
}
}
@@ -0,0 +1,47 @@
<?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\Indexer\Structure\EntityBuilder\Product;
use Amasty\ElasticSearch\Api\Data\Indexer\Structure\EntityBuilderInterface;
use Amasty\ElasticSearch\Model\Di\Wrapper as DiWrapper;
use Amasty\ElasticSearch\Model\Indexer\Data\Product\OutofStockDataMapper;
use Amasty\ElasticSearch\Model\Indexer\Structure\EntityBuilder\Product;
use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
/**
* Provide is_out_of_stock field.
* Need because we have our data mapper, which different with magento data mapper.
*
* @see \Magento\InventoryElasticsearch\Plugin\Model\Adapter\FieldMapper\AdditionalFieldMapperPlugin
*/
class OutofStockData implements EntityBuilderInterface
{
/**
* @var DiWrapper|StockByWebsiteIdResolverInterface
*/
private $stockByWebsiteIdResolver;
public function __construct(DiWrapper $stockByWebsiteIdResolver)
{
$this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
}
/**
* @return array
*/
public function buildEntityFields()
{
if (!$this->stockByWebsiteIdResolver->canCreateObject()) {
return [];
}
return [OutofStockDataMapper::OUT_OF_STOCK_FIELD => ['type' => Product::ATTRIBUTE_TYPE_INT]];
}
}
@@ -0,0 +1,65 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\Structure;
use Amasty\ElasticSearch\Api\Data\Indexer\Structure\IndexBuilderInterface;
class IndexBuilder implements IndexBuilderInterface
{
/**
* @var \Amasty\ElasticSearch\Api\Data\Indexer\Structure\AnalyzerBuilderInterface
*/
private $analyserBuilder;
/**
* @var int
*/
private $storeId = 0;
/**
* @var \Amasty\ElasticSearch\Model\Debug
*/
private $debug;
public function __construct(
\Amasty\ElasticSearch\Api\Data\Indexer\Structure\AnalyzerBuilderInterface $analyserBuilder,
\Amasty\ElasticSearch\Model\Debug $debug
) {
$this->analyserBuilder = $analyserBuilder;
$this->debug = $debug;
}
/**
* {@inheritdoc}
*/
public function build()
{
$settings = [
'index.mapping.total_fields.limit' => IndexBuilderInterface::MAX_FIELDS_COUNT,
'max_result_window' => IndexBuilderInterface::MAX_RESULT_COUNT,
'analysis' => $this->analyserBuilder->build($this->storeId)
];
/*
* debug code for settings
\Amasty\Base\Debug\VarDump::setArrayDepthLevel(5);
$this->debug->debug($settings);
*/
return $settings;
}
/**
* @param int $storeId
* @return $this
*/
public function setStoreId($storeId)
{
$this->storeId = $storeId;
return $this;
}
}