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,23 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Api\Data\Indexer\Data;
interface DataMapperInterface
{
public const ENTITY_ID_FIELD_NAME = 'entity_id';
/**
* @since 1.17.1 introduced mapped data in $context['documents']
*
* @param array $indexData
* @param int $storeId
* @param array{ 'indexer_id': string, 'document': array{int: array|string|int} } $context
* @return array
*/
public function map(array $indexData, $storeId, array $context = []);
}
@@ -0,0 +1,20 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Api\Data\Indexer\Data;
interface DataMapperResolverInterface
{
/**
* @param int $entityId
* @param array $indexData
* @param int $storeId
* @param array $context
* @return array
*/
public function mapEntityData(array $indexData, $storeId, $context = []);
}
@@ -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\Api\Data\Indexer\Structure;
interface AnalyzerBuilderInterface
{
/**
* @param int $storeId
* @return array
*/
public function build($storeId);
}
@@ -0,0 +1,16 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Api\Data\Indexer\Structure;
interface EntityBuilderInterface
{
/**
* @return array
*/
public function buildEntityFields();
}
@@ -0,0 +1,25 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Api\Data\Indexer\Structure;
interface IndexBuilderInterface
{
public const MAX_RESULT_COUNT = 1000000;
public const MAX_FIELDS_COUNT = 1000000;
/**
* @return array
*/
public function build();
/**
* @param int $storeId
* @return \Amasty\ElasticSearch\Api\Data\Indexer\Structure\IndexBuilderInterface;
*/
public function setStoreId($storeId);
}
@@ -0,0 +1,117 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Api\Data;
interface RelevanceRuleInterface
{
/**#@+
* Constants defined for keys of data array
*/
public const RULE_ID = 'rule_id';
public const WEBSITE_ID = 'website_id';
public const IS_ENABLED = 'is_enabled';
public const TITLE = 'title';
public const FROM_DATE = 'from_date';
public const TO_DATE = 'to_date';
public const MULTIPLIER = 'multiplier';
public const CONDITIONS = 'conditions_serialized';
public const TABLE_NAME = 'amasty_elastic_relevance_rule';
/**#@-*/
/**
* @return int|null
*/
public function getId();
/**
* @return int
*/
public function getWebsiteId();
/**
* @return bool
*/
public function isEnabled();
/**
* @return string
*/
public function getTitle();
/**
* @return string
*/
public function getFromDate();
/**
* @return string
*/
public function getToDate();
/**
* @return int
*/
public function getMultiplier();
/**
* @return string
*/
public function getConditions();
/**
* @param int $websiteId
* @return RelevanceRuleInterface
*/
public function setWebsiteId($websiteId);
/**
* @param $isEnabled
* @return RelevanceRuleInterface
*/
public function setIsEnabled($isEnabled);
/**
* @param $title
* @return RelevanceRuleInterface
*/
public function setTitle($title);
/**
* @param $fromDate
* @return RelevanceRuleInterface
*/
public function setFromDate($fromDate);
/**
* @param $toDate
* @return RelevanceRuleInterface
*/
public function setToDate($toDate);
/**
* @param $multiplier
* @return RelevanceRuleInterface
*/
public function setMultiplier($multiplier);
/**
* @param $condition
* @return RelevanceRuleInterface
*/
public function setConditions($condition);
/**
* @return \Magento\CatalogRule\Model\Rule
*/
public function getCatalogRule();
/**
* @return bool
*/
public function isConditionEmpty();
}
@@ -0,0 +1,56 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Api\Data;
interface StopWordInterface
{
/**#@+
* Constants defined for keys of data array
*/
public const STOP_WORD_ID = 'stop_word_id';
public const TABLE_NAME = 'amasty_elastic_stop_word';
public const TERM = 'term';
public const STORE_ID = 'store_id';
/**#@-*/
/**
* @return int
*/
public function getStopWordId();
/**
* @param int $stopWordId
*
* @return \Amasty\ElasticSearch\Api\Data\StopWordInterface
*/
public function setStopWordId($stopWordId);
/**
* @return string
*/
public function getTerm();
/**
* @param string $term
*
* @return \Amasty\ElasticSearch\Api\Data\StopWordInterface
*/
public function setTerm($term);
/**
* @return int
*/
public function getStoreId();
/**
* @param int $storeId
*
* @return \Amasty\ElasticSearch\Api\Data\StopWordInterface
*/
public function setStoreId($storeId);
}
@@ -0,0 +1,56 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Api\Data;
interface SynonymInterface
{
/**#@+
* Constants defined for keys of data array
*/
public const SYNONYM_ID = 'synonym_id';
public const TABLE_NAME = 'amasty_elastic_synonym';
public const STORE_ID = 'store_id';
public const TERM = 'term';
/**#@-*/
/**
* @return int
*/
public function getSynonymId();
/**
* @param int $synonymId
*
* @return \Amasty\ElasticSearch\Api\Data\SynonymInterface
*/
public function setSynonymId($synonymId);
/**
* @return int
*/
public function getStoreId();
/**
* @param int $storeId
*
* @return \Amasty\ElasticSearch\Api\Data\SynonymInterface
*/
public function setStoreId($storeId);
/**
* @return string
*/
public function getTerm();
/**
* @param string $term
*
* @return \Amasty\ElasticSearch\Api\Data\SynonymInterface
*/
public function setTerm($term);
}
@@ -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\Api;
/**
* @api
*/
interface RelevanceRuleRepositoryInterface
{
/**
* @param Data\RelevanceRuleInterface $rule
* @return Data\SynonymInterface
*/
public function save(Data\RelevanceRuleInterface $rule);
/**
* @param int $ruleId|null
* @return Data\RelevanceRuleInterface
*/
public function get($ruleId);
/**
* @param Data\RelevanceRuleInterface $rule
* @return bool true on success
*/
public function delete(Data\RelevanceRuleInterface $rule);
/**
* @param int $ruleId
* @return bool true on success
*/
public function deleteById($ruleId);
/**
* @return \Amasty\ElasticSearch\Model\ResourceModel\RelevanceRule\Collection
*/
public function getActiveRules();
/**
* @param int[]|null $productIds
* @param int|null $websiteId
* @return array
*/
public function getProductBoostMultipliers(?array $productIds = null, ?int $websiteId = null): array;
}
@@ -0,0 +1,74 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Api;
/**
* @api
*/
interface StopWordRepositoryInterface
{
/**
* Save
*
* @param \Amasty\ElasticSearch\Api\Data\StopWordInterface $stopWord
* @return \Amasty\ElasticSearch\Api\Data\StopWordInterface
*/
public function save(\Amasty\ElasticSearch\Api\Data\StopWordInterface $stopWord);
/**
* Get by id
*
* @param int $stopWordId
* @return \Amasty\ElasticSearch\Api\Data\StopWordInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getById($stopWordId);
/**
* Delete
*
* @param \Amasty\ElasticSearch\Api\Data\StopWordInterface $stopWord
* @return bool true on success
* @throws \Magento\Framework\Exception\CouldNotDeleteException
*/
public function delete(\Amasty\ElasticSearch\Api\Data\StopWordInterface $stopWord);
/**
* Delete by id
*
* @param int $stopWordId
* @return bool true on success
* @throws \Magento\Framework\Exception\CouldNotDeleteException
*/
public function deleteById($stopWordId);
/**
* Lists
*
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
* @return \Magento\Framework\Api\SearchResultsInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);
/**
* Stop Words array by storeid
*
* @param int $storeId
* @return array
*/
public function getArrayListByStoreId($storeId);
/**
* @param $file
* @param $storeId
* @return int
* @throws \Exception
*/
public function importStopWords($file, $storeId);
}
@@ -0,0 +1,58 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Api;
/**
* @api
*/
interface SynonymRepositoryInterface
{
/**
* Save
*
* @param \Amasty\ElasticSearch\Api\Data\SynonymInterface $synonym
* @return \Amasty\ElasticSearch\Api\Data\SynonymInterface
*/
public function save(\Amasty\ElasticSearch\Api\Data\SynonymInterface $synonym);
/**
* Get by id
*
* @param int $synonymId
* @return \Amasty\ElasticSearch\Api\Data\SynonymInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getById($synonymId);
/**
* Delete
*
* @param \Amasty\ElasticSearch\Api\Data\SynonymInterface $synonym
* @return bool true on success
* @throws \Magento\Framework\Exception\CouldNotDeleteException
*/
public function delete(\Amasty\ElasticSearch\Api\Data\SynonymInterface $synonym);
/**
* Delete by id
*
* @param int $synonymId
* @return bool true on success
* @throws \Magento\Framework\Exception\CouldNotDeleteException
*/
public function deleteById($synonymId);
/**
* Lists
*
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
* @return \Magento\Framework\Api\SearchResultsInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);
}
@@ -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\Block\Adminhtml\Form\Field;
/**
* @method array|null getFulltextAttributes()
* @method Attribute setFulltextAttributes(array $attributes)
*/
class Attribute extends \Magento\Framework\View\Element\Text
{
/**
* @inheritdoc
*/
protected function _toHtml()
{
$this->setText('');
foreach (array_keys($this->getFulltextAttributes()) as $attributeCode) {
$this->addText('<%= option_extra_attrs.option_' . self::calcOptionHash($attributeCode) . ' %>');
}
return parent::_toHtml();
}
/**
* @param string $optionValue
* @return string
*/
public function calcOptionHash($optionValue)
{
return sprintf('%u', crc32($this->getData('name') . $this->getData('id') . $optionValue));
}
/**
* @param string $value
* @return $this
*/
public function setInputName($value)
{
return $this->setData('name', $value);
}
}
@@ -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\Block\Adminhtml\Form\Field;
class Combining extends \Magento\Framework\View\Element\Html\Select
{
/**
* @var \Amasty\ElasticSearch\Model\Source\CombiningType
*/
private $source;
public function __construct(
\Magento\Framework\View\Element\Context $context,
\Amasty\ElasticSearch\Model\Source\CombiningType $source,
array $data = []
) {
parent::__construct($context, $data);
$this->source = $source;
}
/**
* @param $value
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function setInputName($value)
{
return $this->setName($value);
}
/**
* @return string
*/
protected function _toHtml()
{
if (!$this->getOptions()) {
$this->setOptions($this->source->toOptionArray());
}
return parent::_toHtml();
}
}
@@ -0,0 +1,168 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Block\Adminhtml\Form\Field;
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Amasty\ElasticSearch\Model\Config\QuerySettings as BackendModel;
class QuerySettings extends AbstractFieldArray
{
public const STYLE = 'style="width:90px"';
/**
* @var null|Attribute
*/
private $attributeRenderer;
/**
* @var null|YesNo
*/
private $wildcardRenderer;
/**
* @var null|YesNo
*/
private $spellCorrectionRenderer;
/**
* @var null|Combining
*/
private $combiningRenderer;
/**
* @var \Amasty\ElasticSearch\Model\Source\FulltextAttributes
*/
private $fulltextAttributesSource;
/**
* @inheritdoc
*/
protected function _construct()
{
parent::_construct();
$this->fulltextAttributesSource = $this->getData('fulltext_attributes');
}
/**
* @return Attribute|null
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function getAttributeRenderer()
{
if (!$this->attributeRenderer) {
$this->attributeRenderer = $this->getLayout()->createBlock(
Attribute::class,
'',
['data' => ['is_render_to_js_template' => true]]
);
$this->attributeRenderer
->setFulltextAttributes($this->fulltextAttributesSource->toArray())
->setExtraParams(self::STYLE);
}
return $this->attributeRenderer;
}
/**
* @return YesNo|null
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function getWildcardRenderer()
{
if (!$this->wildcardRenderer) {
$this->wildcardRenderer = $this->getLayout()->createBlock(
YesNo::class,
'',
['data' => ['is_render_to_js_template' => true]]
);
$this->wildcardRenderer->setExtraParams(self::STYLE);
}
return $this->wildcardRenderer;
}
/**
* @return YesNo|null
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function getSpellCorrectionRenderer()
{
if (!$this->spellCorrectionRenderer) {
$this->spellCorrectionRenderer = $this->getLayout()->createBlock(
YesNo::class,
'',
['data' => ['is_render_to_js_template' => true]]
);
$this->spellCorrectionRenderer->setExtraParams(self::STYLE);
}
return $this->spellCorrectionRenderer;
}
/**
* @return YesNo|null
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function getCombiningRenderer()
{
if (!$this->combiningRenderer) {
$this->combiningRenderer = $this->getLayout()->createBlock(
Combining::class,
'',
['data' => ['is_render_to_js_template' => true]]
);
$this->combiningRenderer->setExtraParams(self::STYLE);
}
return $this->combiningRenderer;
}
/**
* @inheritdoc
*/
protected function _prepareToRender()
{
$this->setTemplate('system/config/array.phtml');
$this->addColumn(
BackendModel::ATTRIBUTE,
['label' => __('Attribute'), 'renderer' => $this->getAttributeRenderer()]
);
$this->addColumn(
BackendModel::WILDCARD,
['label' => __('Wildcard'), 'renderer' => $this->getWildcardRenderer()]
);
$this->addColumn(
BackendModel::SPELLING,
['label' => __('Spell Correction'), 'renderer' => $this->getSpellCorrectionRenderer()]
);
$this->addColumn(
BackendModel::COMBINING,
['label' => __('Match Mode'), 'renderer' => $this->getCombiningRenderer()]
);
}
/**
* @inheritdoc
*/
protected function _prepareArrayRow(\Magento\Framework\DataObject $row)
{
$optionId = $this->getAttributeRenderer()->calcOptionHash($row->getData(BackendModel::ATTRIBUTE));
$value = $this->fulltextAttributesSource->toArray()[$row->getData(BackendModel::ATTRIBUTE)];
$optionExtraAttr['option_'. $optionId] = $value;
$optionId = $this->getWildcardRenderer()->calcOptionHash($row->getData(BackendModel::WILDCARD));
$optionExtraAttr['option_'. $optionId] = ' checked ';
$optionId = $this->getSpellCorrectionRenderer()->calcOptionHash($row->getData(BackendModel::SPELLING));
$optionExtraAttr['option_'. $optionId] = ' checked ';
$optionId = $this->getCombiningRenderer()->calcOptionHash($row->getData(BackendModel::COMBINING));
$optionExtraAttr['option_'. $optionId] = 'selected="selected"';
$row->setData('option_extra_attrs', $optionExtraAttr);
}
}
@@ -0,0 +1,64 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Block\Adminhtml\Form\Field;
use Magento\Framework\View\Element\Context;
use Magento\Framework\View\Element\Html\Select;
class YesNo extends Select
{
/**
* @var \Magento\Config\Model\Config\Source\Yesno
*/
private $source;
public function __construct(
Context $context,
\Magento\Config\Model\Config\Source\Yesno $source,
array $data = []
) {
parent::__construct($context, $data);
$this->source = $source;
}
/**
* @param $value
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function setInputName($value)
{
return $this->setName($value);
}
/**
* @return string
*/
protected function _toHtml()
{
if (!$this->getOptions()) {
$this->setOptions($this->source->toOptionArray());
}
if (!$this->_beforeToHtml()) {
return '';
}
$html = '<div name="' . $this->getName() . '" class="admin__actions-switch ' . $this->getClass() . '">';
$selectedHtml = ' <%= option_extra_attrs.option_' . self::calcOptionHash('1') . ' %>';
$html .= ' <input id="' . $this->getName() . '" type="checkbox" class="admin__actions-switch-checkbox input-'
. $this->getColumnName() . '" ' . $selectedHtml . '" name="' . $this->getName() . '"/>' .
'<label class="admin__actions-switch-label ' . $this->getColumnName() . '" for="' . $this->getName()
. '" style="width: 70px;">' .
'<span class="admin__actions-switch-text" data-text-on="'. __('Yes'). '" data-text-off="' . __('No')
. '"></span></label>';
$html .= '</div>';
return $html;
}
}
@@ -0,0 +1,176 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Block\Adminhtml\RelevanceRule;
use Amasty\ElasticSearch\Controller\Adminhtml\RelevanceRule\AbstractRelevance;
use Magento\Backend\Block\Widget\Form;
use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Ui\Component\Layout\Tabs\TabInterface;
class Conditions extends Generic implements TabInterface
{
/**
* @var Form\Renderer\Fieldset
*/
private $rendererFieldset;
/**
* @var \Magento\Rule\Block\Conditions
*/
private $conditions;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Data\FormFactory $formFactory,
\Magento\Rule\Block\Conditions $conditions,
\Magento\Backend\Block\Widget\Form\Renderer\Fieldset $rendererFieldset,
array $data = []
) {
parent::__construct($context, $registry, $formFactory, $data);
$this->conditions = $conditions;
$this->rendererFieldset = $rendererFieldset;
}
/**
* @inheritdoc
*/
public function getTabClass()
{
return null;
}
/**
* @inheritdoc
*/
public function getTabUrl()
{
return null;
}
/**
* @inheritdoc
*/
public function isAjaxLoaded()
{
return false;
}
/**
* @inheritdoc
*/
public function getTabLabel()
{
return __('Conditions');
}
/**
* @inheritdoc
*/
public function getTabTitle()
{
return __('Conditions');
}
/**
* @inheritdoc
*/
public function canShowTab()
{
return true;
}
/**
* @inheritdoc
*/
public function isHidden()
{
return false;
}
/**
* @inheritdoc
*/
protected function _prepareForm()
{
$model = $this->_coreRegistry->registry(AbstractRelevance::CURRENT_RULE)->getCatalogRule();
$form = $this->addTabToForm($model);
$this->setForm($form);
return parent::_prepareForm();
}
/**
* @param \Magento\CatalogRule\Model\Rule $model
* @param string $fieldsetId
* @param string $formName
* @return \Magento\Framework\Data\Form
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function addTabToForm(
\Magento\CatalogRule\Model\Rule $model,
$fieldsetId = 'conditions_fieldset',
$formName = 'amasty_elastic_relevancerule_form'
) {
/** @var \Magento\Framework\Data\Form $form */
$form = $this->_formFactory->create();
$form->setHtmlIdPrefix('rule_');
$conditionsFieldSetId = $model->getConditionsFieldSetId($formName);
$newChildUrl = $this->getUrl(
'amasty_elastic/relevancerule/newConditionHtml/form/' . $conditionsFieldSetId,
['form_namespace' => $formName]
);
$renderer = $this->rendererFieldset
->setNameInLayout($this->getNameInLayout() . 'fieldset_renderer')
->setTemplate('Magento_CatalogRule::promo/fieldset.phtml')
->setNewChildUrl($newChildUrl)
->setFieldSetId($conditionsFieldSetId);
$fieldset = $form->addFieldset(
$fieldsetId,
['legend' => __('Rule won\'t work if no conditions are applied')]
)
->setRenderer($renderer);
$fieldset->addField(
'conditions',
'text',
[
'name' => 'conditions',
'label' => __('Conditions'),
'title' => __('Conditions'),
'required' => true
]
)->setRule($model)->setRenderer($this->conditions);
$form->setValues($model->getData());
$this->setConditionFormName($model->getConditions(), $formName, $conditionsFieldSetId);
return $form;
}
/**
* @param \Magento\Rule\Model\Condition\AbstractCondition $conditions
* @param string $formName
* @param string $jsFormName
* @return void
*/
private function setConditionFormName(
\Magento\Rule\Model\Condition\AbstractCondition $conditions,
$formName,
$jsFormName
) {
$conditions->setFormName($formName);
$conditions->setJsFormObject($jsFormName);
if ($conditions->getConditions() && is_array($conditions->getConditions())) {
foreach ($conditions->getConditions() as $condition) {
$this->setConditionFormName($condition, $formName, $jsFormName);
}
}
}
}
@@ -0,0 +1,105 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Block\Adminhtml\System\Config;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\Json\EncoderInterface;
class ConnectionChecker extends \Magento\Config\Block\System\Config\Form\Field
{
/**
* @var EncoderInterface
*/
private $jsonEncoder;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
EncoderInterface $jsonEncoder,
array $data = []
) {
parent::__construct($context, $data);
$this->jsonEncoder = $jsonEncoder;
}
/**
* @return $this
*/
protected function _prepareLayout()
{
parent::_prepareLayout();
$this->setTemplate('Amasty_ElasticSearch::system/config/connection.phtml');
return $this;
}
/**
* @param AbstractElement $element
* @return string
*/
public function render(AbstractElement $element)
{
$element = clone $element;
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
return parent::render($element);
}
/**
* @param AbstractElement $element
* @return string
*/
protected function _getElementHtml(AbstractElement $element)
{
$originalData = $element->getOriginalData();
$this->addData(
[
'button_label' => __($originalData['button_label']),
'json_config' => $this->generateButtonConfig($element),
'html_id' => $element->getHtmlId()
]
);
return $this->_toHtml();
}
/**
* @return array
*/
private function getFieldMapping()
{
return [
'hostname' => 'server_hostname',
'port' => 'server_port',
'index' => 'index_prefix',
'enableAuth' => 'enable_auth',
'username' => 'username',
'password' => 'password',
'timeout' => 'server_timeout',
'engine' => 'engine',
'customAnalyzer' => 'custom_analyzer'
];
}
/**
* @param $element
* @return mixed
*/
private function generateButtonConfig(AbstractElement $element)
{
$result = [
'url' => $this->_urlBuilder->getUrl('amasty_elastic/config_checker/connection'),
'elementId' => $element->getHtmlId(),
'successText' => __('Successful! Test again?'),
'failedText' => __('Connection failed! Test again?'),
'fieldMapping' => $this->getFieldMapping(),
'validation' => [],
];
return $this->jsonEncoder->encode($result);
}
}
@@ -0,0 +1,160 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Config\Checker;
use Amasty\ElasticSearch\Exception\Missing404Exception;
use Amasty\ElasticSearch\Model\Client\ClientFactory;
use Amasty\ElasticSearch\Model\Client\ClientInterface;
use Amasty\ElasticSearch\Model\Config;
use Amasty\ElasticSearch\Model\GetAmastySearchEngines;
use Amasty\ElasticSearch\Model\Source\CustomAnalyzer;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filter\StripTags;
class Connection extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::config';
public const EXCEPTION_PATTERN = '/unknown analyzer/i';
public const IS_READ_ONLY_INDEX = 'amasty_is_read_only_flag';
public const CUSTOM_ANALYZER_TEST_INDEX_NAME = 'amasty_custom_analyzer_test_index';
/**
* @var Config
*/
private $config;
/**
* @var JsonFactory
*/
private $resultJsonFactory;
/**
* @var StripTags
*/
private $tagFilter;
/**
* @var GetAmastySearchEngines
*/
private $getAmastySearchEngines;
/**
* @var ClientFactory
*/
private $clientFactory;
public function __construct(
Context $context,
Config $config,
JsonFactory $resultJsonFactory,
StripTags $tagFilter,
GetAmastySearchEngines $getAmastySearchEngines,
ClientFactory $clientFactory
) {
parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
$this->tagFilter = $tagFilter;
$this->config = $config;
$this->getAmastySearchEngines = $getAmastySearchEngines;
$this->clientFactory = $clientFactory;
}
/**
* @return $this|ResponseInterface|ResultInterface
*/
public function execute()
{
$result = [
'success' => false,
'errorMessage' => '',
];
$options = $this->getRequest()->getParams();
try {
if (empty($options['engine'])
|| !in_array($options['engine'], $this->getAmastySearchEngines->execute(), true)
) {
throw new LocalizedException(
__('Test connection can be applied only for Amasty Elastic Search engine.')
);
}
$connectionData = $this->config->prepareConnectionData($options);
$client = $this->clientFactory->create($options['engine'], $connectionData);
$pingResult = $client->ping();
if ($pingResult) {
$indexPrefix = $connectionData['index'] ?? $this->config->getIndexPrefix();
$this->checkIsReadOnly($client, $indexPrefix);
if (isset($options['customAnalyzer'])
&& $options['customAnalyzer'] != CustomAnalyzer::DISABLED
) {
$customAnalyzerIndexName = $indexPrefix . '_' . self::CUSTOM_ANALYZER_TEST_INDEX_NAME;
$client->createIndex(
$customAnalyzerIndexName,
[
'analysis' => [
'analyzer' => [
'default' => ['type' => $options['customAnalyzer']]
]
]
]
);
try {
$client->deleteIndex($customAnalyzerIndexName);
} catch (Missing404Exception $e) {
;// do nothing
}
}
$result['success'] = $pingResult;
} else {
$result['errorMessage'] = __('It is impossible to perform the search.
Please make sure Elasticsearch Engine is installed on the server and configured properly.');
}
// @codingStandardsIgnoreLine
} catch (LocalizedException $e) {
$result['errorMessage'] = $e->getMessage();
} catch (\Exception $e) {
if (preg_match(self::EXCEPTION_PATTERN, $e->getMessage())) {
$result['errorMessage'] = __('To use custom analyzer you have to install matching plugin');
} else {
$result['errorMessage'] = $this->tagFilter->filter(__($e->getMessage()));
}
}
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->resultJsonFactory->create();
return $resultJson->setData($result);
}
private function checkIsReadOnly(ClientInterface $client, string $indexPrefix)
{
$testIndexName = $indexPrefix . '_' . self::IS_READ_ONLY_INDEX;
try {
$client->createIndex($testIndexName, []);
$client->deleteIndex($testIndexName);
} catch (\Exception $e) {
$this->messageManager->addErrorMessage(
__('Please check the state of read_only_allow_delete setting in Elasticsearch server configuration.')
);
}
}
}
@@ -0,0 +1,21 @@
<?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\Controller\Adminhtml\Product\Mui;
use Amasty\ElasticSearch\Controller\Adminhtml\RelevanceRule\AbstractRelevance;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Ui\Controller\Adminhtml\Index\Render as MagentoUiRenderController;
class Render extends MagentoUiRenderController implements HttpGetActionInterface, HttpPostActionInterface
{
public const ADMIN_RESOURCE = AbstractRelevance::ADMIN_RESOURCE;
}
@@ -0,0 +1,82 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\RelevanceRule;
use Amasty\ElasticSearch\Model\RelevanceRuleFactory;
use Amasty\ElasticSearch\Model\RelevanceRuleRepository;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\ForwardFactory;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Store\Model\StoreManagerInterface;
abstract class AbstractRelevance extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::relevance_rules';
public const CURRENT_RULE = 'amasty_elastic_relevance_rule';
/**
* @var ForwardFactory
*/
protected $resultForwardFactory;
/**
* @var RelevanceRuleRepository
*/
protected $ruleRepository;
/**
* @var RelevanceRuleFactory
*/
protected $ruleFactory;
/**
* @var Registry
*/
protected $registry;
/**
* @var TimezoneInterface
*/
protected $timezone;
/**
* @var StoreManagerInterface
*/
protected $storeManager;
public function __construct(
Context $context,
ForwardFactory $resultForwardFactory,
RelevanceRuleRepository $ruleRepository,
RelevanceRuleFactory $ruleFactory,
Registry $registry,
TimezoneInterface $timezone,
StoreManagerInterface $storeManager
) {
parent::__construct($context);
$this->resultForwardFactory = $resultForwardFactory;
$this->ruleRepository = $ruleRepository;
$this->ruleFactory = $ruleFactory;
$this->registry = $registry;
$this->timezone = $timezone;
$this->storeManager = $storeManager;
}
/**
* @param \Magento\Backend\Model\View\Result\Page $resultPage
* @return \Magento\Backend\Model\View\Result\Page
*/
protected function initPage($resultPage)
{
$resultPage->setActiveMenu('Amasty_ElasticSearch::Amasty_ElasticSearch');
$resultPage->getConfig()->getTitle()->prepend(__('Manage Relevance Rules'));
return $resultPage;
}
}
@@ -0,0 +1,77 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\RelevanceRule;
use Amasty\ElasticSearch\Api\Data\RelevanceRuleInterface;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Backend\App\Action;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::relevance_rules';
/**
* @var \Amasty\ElasticSearch\Model\RelevanceRuleRepository
*/
private $relevanceRuleRepository;
/**
* @var \Amasty\ElasticSearch\Model\ResourceModel\RelevanceRule\CollectionFactory
*/
private $collectionFactory;
/**
* @var Filter
*/
private $filter;
public function __construct(
Action\Context $context,
\Amasty\ElasticSearch\Model\RelevanceRuleRepository $relevanceRuleRepository,
\Amasty\ElasticSearch\Model\ResourceModel\RelevanceRule\CollectionFactory $collectionFactory,
Filter $filter
) {
parent::__construct($context);
$this->relevanceRuleRepository = $relevanceRuleRepository;
$this->collectionFactory = $collectionFactory;
$this->filter = $filter;
}
/**
* @inheritdoc
*/
public function execute()
{
if ($this->getRequest()->getParam(Filter::SELECTED_PARAM)
|| $this->getRequest()->getParam(Filter::EXCLUDED_PARAM)
) {
$ids = $this->filter->getCollection($this->collectionFactory->create())->getAllIds();
} else {
$ids = (array)$this->getRequest()->getParam(RelevanceRuleInterface::RULE_ID);
}
if ($ids) {
foreach ($ids as $id) {
try {
$this->relevanceRuleRepository->deleteById($id);
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
}
$this->messageManager->addSuccessMessage(
__('%1 rule(s) was successfully removed', count($ids))
);
} else {
$this->messageManager->addErrorMessage(__('Please select Relevance Rule(s)'));
}
return $this->resultRedirectFactory->create()->setPath('*/*/');
}
}
@@ -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\Controller\Adminhtml\RelevanceRule;
use Amasty\ElasticSearch\Api\Data\RelevanceRuleInterface;
use Magento\Framework\Controller\ResultFactory;
class Edit extends AbstractRelevance
{
/**
* @inheritdoc
*/
public function execute()
{
$ruleId = (int)$this->getRequest()->getParam(RelevanceRuleInterface::RULE_ID);
try {
if ($ruleId) {
/** @var \Amasty\ElasticSearch\Model\RelevanceRule $model */
$model = $this->ruleRepository->get($ruleId);
} else {
$model = $this->ruleFactory->create();
}
$this->registry->register(self::CURRENT_RULE, $model);
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
$this->messageManager->addErrorMessage(__('This Relevance Rule is no longer exists.'));
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/');
}
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$text = $model->getId() ? $model->getTitle() : __('New Relevance Rule');
$this->initPage($resultPage)->getConfig()->getTitle()->prepend($text);
return $resultPage;
}
}
@@ -0,0 +1,22 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\RelevanceRule;
use Magento\Framework\Controller\ResultFactory;
class Index extends AbstractRelevance
{
/**
* @inheritdoc
*/
public function execute()
{
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
return $this->initPage($resultPage);
}
}
@@ -0,0 +1,20 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\RelevanceRule;
class NewAction extends AbstractRelevance
{
/**
* @inheritdoc
*/
public function execute()
{
$resultForward = $this->resultForwardFactory->create();
return $resultForward->forward('edit');
}
}
@@ -0,0 +1,42 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\RelevanceRule;
class NewConditionHtml extends AbstractRelevance
{
/**
* @inheritdoc
*/
public function execute()
{
$ruleId = $this->getRequest()->getParam('id');
$formName = $this->getRequest()->getParam('form_namespace');
$typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
$type = $typeArr[0];
$model = $this->_objectManager->create($type)
->setId($ruleId)
->setType($type)
->setRule($this->_objectManager->create(\Magento\CatalogRule\Model\Rule::class))
->setPrefix('conditions');
if (!empty($typeArr[1])) {
$model->setAttribute($typeArr[1]);
}
if ($model instanceof \Magento\Rule\Model\Condition\AbstractCondition) {
$model->setJsFormObject($this->getRequest()->getParam('form'));
$model->setFormName($formName);
$html = $model->asHtmlRecursive();
} else {
$html = '';
}
$this->getResponse()->setBody($html);
}
}
@@ -0,0 +1,115 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\RelevanceRule;
use Amasty\ElasticSearch\Api\Data\RelevanceRuleInterface;
use Amasty\ElasticSearch\Model\Source\RelevanceRuleModificationType;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Stdlib\DateTime;
class Save extends AbstractRelevance
{
/**
* @inheritdoc
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$rule = (int)$this->getRequest()->getParam(RelevanceRuleInterface::RULE_ID);
$data = $this->getRequest()->getPostValue();
try {
if ($rule) {
/** @var \Amasty\ElasticSearch\Model\RelevanceRule $model */
$model = $this->ruleRepository->get($rule);
} else {
$model = $this->ruleFactory->create();
}
$data = $this->prepareData($data, $model);
$model->setData($data);
$this->ruleRepository->save($model);
$this->messageManager->addSuccessMessage(__('You have saved the Rule.'));
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
$this->messageManager->addErrorMessage(
__('Relevance Rule with the same term already exists in an associated store.')
);
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
$this->messageManager->addErrorMessage(__('This Relevance Rule no longer exists.'));
$resultRedirect = $this->resultRedirectFactory->create();
}
return $resultRedirect->setPath('*/*/');
}
/**
* @param array $data
* @param \Amasty\ElasticSearch\Model\RelevanceRule $rule
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function prepareData(array $data, \Amasty\ElasticSearch\Model\RelevanceRule $rule)
{
if (empty($data[RelevanceRuleInterface::RULE_ID])) {
$data[RelevanceRuleInterface::RULE_ID] = null;
}
$data[RelevanceRuleInterface::MULTIPLIER] = max(
min($data[RelevanceRuleInterface::MULTIPLIER], 100),
1
);
if ($data['modification_type'] === RelevanceRuleModificationType::DECREASE) {
$data[RelevanceRuleInterface::MULTIPLIER] *= -1;
}
if (isset($data['rule']) && isset($data['rule']['conditions'])) {
$conditions = $data['rule']['conditions'];
unset($data['rule']);
$data[RelevanceRuleInterface::CONDITIONS] = $rule->getConditionsSerialized($conditions);
}
$data[RelevanceRuleInterface::FROM_DATE] = $this->prepareDates($data[RelevanceRuleInterface::FROM_DATE]);
$data[RelevanceRuleInterface::TO_DATE] = $this->prepareDates($data[RelevanceRuleInterface::TO_DATE]);
$data = $this->prepareRuleStatus($data);
return $data;
}
private function prepareDates(string $date): ?string
{
return $date ? $this->timezone->date($date)->format(DateTime::DATE_PHP_FORMAT) : '';
}
private function prepareRuleStatus(array $data): array
{
$websiteId = $data[RelevanceRuleInterface::WEBSITE_ID] ?? false;
if ($websiteId) {
try {
$website = $this->storeManager->getWebsite($websiteId);
if (!empty($data[RelevanceRuleInterface::FROM_DATE])
&& !empty($data[RelevanceRuleInterface::TO_DATE])
&& !$this->timezone->isScopeDateInInterval(
$website,
$data[RelevanceRuleInterface::FROM_DATE],
$data[RelevanceRuleInterface::TO_DATE]
)
) {
$data[RelevanceRuleInterface::IS_ENABLED] = false;
}
} catch (LocalizedException $e) {
;// do nothing
}
}
return $data;
}
}
@@ -0,0 +1,72 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Stopword;
use Magento\Backend\App\Action\Context;
use Magento\Backend\App\Action;
/**
* Class AbstractStopWord
*/
abstract class AbstractStopWord extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::stop_words';
/**
* @var \Magento\Backend\Model\View\Result\ForwardFactory
*/
protected $resultForwardFactory;
/**
* @var \Amasty\ElasticSearch\Model\StopWordRepository
*/
protected $stopWordRepository;
/**
* @var \Amasty\ElasticSearch\Model\StopWordFactory
*/
protected $stopWordFactory;
/**
* @var \Magento\Framework\Registry
*/
protected $registry;
/**
* @var \Magento\Framework\Indexer\IndexerRegistry
*/
protected $indexerRegistry;
public function __construct(
Context $context,
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
\Amasty\ElasticSearch\Model\StopWordRepository $ruleRepository,
\Amasty\ElasticSearch\Model\StopWordFactory $stopWordFactory,
\Magento\Framework\Registry $registry,
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
) {
parent::__construct($context);
$this->resultForwardFactory = $resultForwardFactory;
$this->stopWordRepository = $ruleRepository;
$this->stopWordFactory = $stopWordFactory;
$this->registry = $registry;
$this->indexerRegistry = $indexerRegistry;
}
/**
* @param \Magento\Backend\Model\View\Result\Page $resultPage
* @return \Magento\Backend\Model\View\Result\Page
*/
protected function initPage($resultPage)
{
$resultPage->setActiveMenu('Amasty_ElasticSearch::Amasty_ElasticSearch');
$resultPage->getConfig()->getTitle()->prepend(__('Manage Stop Words'));
return $resultPage;
}
}
@@ -0,0 +1,89 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Stopword;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Backend\App\Action;
use Amasty\ElasticSearch\Api\Data\StopWordInterface;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::stop_words';
/**
* @var \Amasty\ElasticSearch\Model\StopWordRepository
*/
private $stopWordRepository;
/**
* @var \Amasty\ElasticSearch\Model\ResourceModel\StopWord\CollectionFactory
*/
private $collectionFactory;
/**
* @var Filter
*/
private $filter;
/**
* @var \Magento\Framework\Indexer\IndexerRegistry
*/
private $indexerRegistry;
public function __construct(
Action\Context $context,
\Amasty\ElasticSearch\Model\StopWordRepository $stopWordRepository,
\Amasty\ElasticSearch\Model\ResourceModel\StopWord\CollectionFactory $collectionFactory,
Filter $filter,
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
) {
parent::__construct($context);
$this->stopWordRepository = $stopWordRepository;
$this->collectionFactory = $collectionFactory;
$this->filter = $filter;
$this->indexerRegistry = $indexerRegistry;
}
/**
* {@inheritdoc}
*/
public function execute()
{
$idsToRemove = [];
$ids = $this->getRequest()->getParam(StopWordInterface::STOP_WORD_ID);
if ($ids) {
$idsToRemove = [$ids];
}
if ($this->getRequest()->getParam(Filter::SELECTED_PARAM)
|| $this->getRequest()->getParam(Filter::EXCLUDED_PARAM)
) {
$idsToRemove = $this->filter->getCollection($this->collectionFactory->create())->getAllIds();
}
if ($idsToRemove) {
foreach ($idsToRemove as $id) {
try {
$this->stopWordRepository->deleteById($id);
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
}
$this->messageManager->addSuccessMessage(
__('%1 stop word(s) was successfully removed', count($idsToRemove))
);
$this->indexerRegistry->get(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID)->invalidate();
} else {
$this->messageManager->addErrorMessage(__('Please select Stop Word(s)'));
}
return $this->resultRedirectFactory->create()->setPath('*/*/');
}
}
@@ -0,0 +1,129 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Stopword;
use Magento\Backend\App\Action;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Amasty\ElasticSearch\Api\Data\StopWordInterface;
class DoImport extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::stop_words';
public const MEDIA_PATH = 'amasty/elastic/import';
public const FILE_WAS_NOT_UPLOADED_CODE_ERROR = '666';
/**
* @var \Magento\Framework\Filesystem
*/
private $filesystem;
/**
* @var \Magento\Framework\Filesystem\Io\File
*/
private $ioFile;
/**
* @var \Magento\MediaStorage\Model\File\UploaderFactory
*/
private $fileUploaderFactory;
/**
* @var \Amasty\ElasticSearch\Model\StopWordRepository
*/
private $stopWordRepository;
/**
* @var \Amasty\ElasticSearch\Model\StopWordFactory
*/
private $stopWordFactory;
/**
* @var \Magento\Framework\Indexer\IndexerRegistry
*/
private $indexerRegistry;
public function __construct(
Action\Context $context,
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\Filesystem\Io\File $ioFile,
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
\Amasty\ElasticSearch\Model\StopWordRepository $stopWordRepository,
\Amasty\ElasticSearch\Model\StopWordFactory $stopWordFactory,
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
) {
parent::__construct($context);
$this->filesystem = $filesystem;
$this->ioFile = $ioFile;
$this->fileUploaderFactory = $fileUploaderFactory;
$this->stopWordRepository = $stopWordRepository;
$this->stopWordFactory = $stopWordFactory;
$this->indexerRegistry = $indexerRegistry;
}
/**
* {@inheritdoc}
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$data = $this->getRequest()->getPostValue();
$storeId = (int) $this->getRequest()->getParam(StopWordInterface::STORE_ID);
if ($data) {
try {
//upload images
$path = $this->filesystem->getDirectoryRead(
DirectoryList::MEDIA
)->getAbsolutePath(
self::MEDIA_PATH
);
$this->ioFile->checkAndCreateFolder($path);
try {
/** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
$uploader = $this->fileUploaderFactory->create(['fileId' => 'amasty_elastic_file_upload']);
$uploader->setAllowedExtensions(['csv']);
$uploader->setAllowRenameFiles(true);
$result = $uploader->save($path);
} catch (\Exception $ex) {
$this->messageManager->addErrorMessage(__($ex->getMessage()));
return $resultRedirect->setPath('*/*/import');
}
if (isset($result['file']) && $this->ioFile->fileExists($path . '/' . $result['file'])) {
$file = $path . '/' . $result['file'];
} else {
$this->messageManager->addErrorMessage(__('Something wend wrong during saving file'));
return $resultRedirect->setPath('*/*/import');
}
$count = $this->stopWordRepository->importStopWords($file, $storeId);
$this->ioFile->rm($file);
} catch (LocalizedException $ex) {
$this->messageManager->addErrorMessage($ex->getMessage());
return $resultRedirect->setPath('*/*/import');
} catch (\Exception $ex) {
$this->messageManager->addErrorMessage(__('Something went wrong. Please try again'));
return $resultRedirect->setPath('*/*/import');
}
if (isset($count)) {
$this->messageManager->addSuccessMessage(__('%1 Stop Word(s) has been imported', $count));
$this->indexerRegistry->get(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID)->invalidate();
} else {
$this->messageManager->addWarningMessage('Imported file was empty or all terms are already exist');
}
} else {
$this->messageManager->addErrorMessage('There are no data to import');
return $resultRedirect->setPath('*/*/import');
}
return $resultRedirect->setPath('*/*/');
}
}
@@ -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\Controller\Adminhtml\Stopword;
use Magento\Framework\Controller\ResultFactory;
use Amasty\ElasticSearch\Api\Data\StopWordInterface;
class Edit extends AbstractStopWord
{
/**
* {@inheritdoc}
*/
public function execute()
{
$stopWordId = (int)$this->getRequest()->getParam(StopWordInterface::STOP_WORD_ID);
try {
if ($stopWordId) {
/** @var \Amasty\ElasticSearch\Model\StopWord $model */
$model = $this->stopWordRepository->getById($stopWordId);
} else {
$model = $this->stopWordFactory->create();
}
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
$this->messageManager->addErrorMessage(__('This Stop Word no longer exists.'));
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/');
}
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$text = $model->getId() ? __('Edit Stop Word "%1"', $model->getTerm()) : __('New Stop Word');
$this->initPage($resultPage)->getConfig()->getTitle()->prepend($text);
return $resultPage;
}
}
@@ -0,0 +1,26 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Stopword;
use Magento\Framework\Controller\ResultFactory;
class Import extends AbstractStopWord
{
/**
* {@inheritdoc}
*/
public function execute()
{
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$this->initPage($resultPage)
->getConfig()->getTitle()->prepend(__('Import Stop Words'));
return $resultPage;
}
}
@@ -0,0 +1,26 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Stopword;
use Magento\Framework\Controller\ResultFactory;
class Index extends AbstractStopWord
{
/**
* {@inheritdoc}
*/
public function execute()
{
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$this->initPage($resultPage)
->getConfig()->getTitle()->prepend(__('Manage Stop Words'));
return $resultPage;
}
}
@@ -0,0 +1,18 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Stopword;
class NewAction extends AbstractStopWord
{
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Forward $resultForward */
$resultForward = $this->resultForwardFactory->create();
return $resultForward->forward('edit');
}
}
@@ -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\Controller\Adminhtml\Stopword;
use Amasty\ElasticSearch\Api\Data\StopWordInterface;
class Save extends AbstractStopWord
{
/**
* {@inheritdoc}
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$stopWordId = (int)$this->getRequest()->getParam(StopWordInterface::STOP_WORD_ID);
try {
if ($stopWordId) {
/** @var \Amasty\ElasticSearch\Model\StopWord $model */
$model = $this->stopWordRepository->getById($stopWordId);
} else {
$model = $this->stopWordFactory->create();
}
$model->setTerm($this->getRequest()->getParam(StopWordInterface::TERM))
->setStoreId($this->getRequest()->getParam(StopWordInterface::STORE_ID));
$this->stopWordRepository->save($model);
$this->messageManager->addSuccessMessage(__('You have saved the Stop Word.'));
$this->indexerRegistry->get(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID)->invalidate();
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
$this->messageManager->addErrorMessage(
__('A Stop Word with the same term already exists in an associated store.')
);
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
$this->messageManager->addErrorMessage(__('This Stop Word no longer exists.'));
$resultRedirect = $this->resultRedirectFactory->create();
}
return $resultRedirect->setPath('*/*/');
}
}
@@ -0,0 +1,72 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Synonym;
use Magento\Backend\App\Action\Context;
use Magento\Backend\App\Action;
/**
* Class AbstractSynonym
*/
abstract class AbstractSynonym extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::synonym';
/**
* @var \Magento\Backend\Model\View\Result\ForwardFactory
*/
protected $resultForwardFactory;
/**
* @var \Amasty\ElasticSearch\Model\SynonymRepository
*/
protected $synonymRepository;
/**
* @var \Amasty\ElasticSearch\Model\SynonymFactory
*/
protected $synonymFactory;
/**
* @var \Magento\Framework\Registry
*/
protected $registry;
/**
* @var \Magento\Framework\Indexer\IndexerRegistry
*/
protected $indexerRegistry;
public function __construct(
Context $context,
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
\Amasty\ElasticSearch\Model\SynonymRepository $synonymRepository,
\Amasty\ElasticSearch\Model\SynonymFactory $synonymFactory,
\Magento\Framework\Registry $registry,
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
) {
parent::__construct($context);
$this->resultForwardFactory = $resultForwardFactory;
$this->synonymRepository = $synonymRepository;
$this->synonymFactory = $synonymFactory;
$this->registry = $registry;
$this->indexerRegistry = $indexerRegistry;
}
/**
* @param \Magento\Backend\Model\View\Result\Page $resultPage
* @return \Magento\Backend\Model\View\Result\Page
*/
protected function initPage($resultPage)
{
$resultPage->setActiveMenu('Amasty_ElasticSearch::Amasty_ElasticSearch');
$resultPage->getConfig()->getTitle()->prepend(__('Manage Synonyms'));
return $resultPage;
}
}
@@ -0,0 +1,89 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Synonym;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Backend\App\Action;
use Amasty\ElasticSearch\Api\Data\SynonymInterface;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::synonym';
/**
* @var \Amasty\ElasticSearch\Model\SynonymRepository
*/
private $synonymRepository;
/**
* @var \Amasty\ElasticSearch\Model\ResourceModel\Synonym\CollectionFactory
*/
private $collectionFactory;
/**
* @var Filter
*/
private $filter;
/**
* @var \Magento\Framework\Indexer\IndexerRegistry
*/
private $indexerRegistry;
public function __construct(
Action\Context $context,
\Amasty\ElasticSearch\Model\SynonymRepository $synonymRepository,
\Amasty\ElasticSearch\Model\ResourceModel\Synonym\CollectionFactory $collectionFactory,
Filter $filter,
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
) {
parent::__construct($context);
$this->synonymRepository = $synonymRepository;
$this->collectionFactory = $collectionFactory;
$this->filter = $filter;
$this->indexerRegistry = $indexerRegistry;
}
/**
* {@inheritdoc}
*/
public function execute()
{
$idsToRemove = [];
$ids = $this->getRequest()->getParam(SynonymInterface::SYNONYM_ID);
if ($ids) {
$idsToRemove = [$ids];
}
if ($this->getRequest()->getParam(Filter::SELECTED_PARAM)
|| $this->getRequest()->getParam(Filter::EXCLUDED_PARAM)
) {
$idsToRemove = $this->filter->getCollection($this->collectionFactory->create())->getAllIds();
}
if ($idsToRemove) {
foreach ($idsToRemove as $id) {
try {
$this->synonymRepository->deleteById($id);
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
}
$this->messageManager->addSuccessMessage(
__('%1 synonym(s) was successfully removed', count($idsToRemove))
);
$this->indexerRegistry->get(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID)->invalidate();
} else {
$this->messageManager->addErrorMessage(__('Please select Synonym(s)'));
}
return $this->resultRedirectFactory->create()->setPath('*/*/');
}
}
@@ -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\Controller\Adminhtml\Synonym;
use Magento\Backend\App\Action;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\LocalizedException;
use Amasty\ElasticSearch\Api\Data\SynonymInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Amasty\ElasticSearch\Model\Indexer\Data\DataMapperResolver;
class DoImport extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::synonym';
public const MEDIA_PATH = 'amasty/elastic/import';
public const FILE_WAS_NOT_UPLOADED_CODE_ERROR = '666';
/**
* @var \Magento\Framework\Filesystem
*/
private $filesystem;
/**
* @var \Magento\Framework\Filesystem\Io\File
*/
private $ioFile;
/**
* @var \Magento\MediaStorage\Model\File\UploaderFactory
*/
private $fileUploaderFactory;
/**
* @var \Amasty\ElasticSearch\Model\SynonymRepository
*/
private $synonymRepository;
/**
* @var \Amasty\ElasticSearch\Model\SynonymFactory
*/
private $synonymFactory;
/**
* @var \Magento\Framework\File\Csv
*/
private $csv;
/**
* @var \Magento\Indexer\Model\IndexerFactory
*/
protected $indexerFactory;
public function __construct(
Action\Context $context,
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\Filesystem\Io\File $ioFile,
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
\Amasty\ElasticSearch\Model\SynonymRepository $synonymRepository,
\Amasty\ElasticSearch\Model\SynonymFactory $synonymFactory,
\Magento\Framework\File\Csv $csv,
\Magento\Indexer\Model\IndexerFactory $indexerFactory
) {
parent::__construct($context);
$this->filesystem = $filesystem;
$this->ioFile = $ioFile;
$this->fileUploaderFactory = $fileUploaderFactory;
$this->synonymRepository = $synonymRepository;
$this->synonymFactory = $synonymFactory;
$this->csv = $csv;
$this->indexerFactory = $indexerFactory;
}
/**
* {@inheritdoc}
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$data = $this->getRequest()->getPostValue();
$storeId = (int) $this->getRequest()->getParam(SynonymInterface::STORE_ID);
$count = 0;
if ($data) {
try {
$file = $this->uploadFile();
$csvData = $this->csv->getData($file);
foreach ($csvData as $data) {
$term = ($data && is_array($data)) ? implode(',', $data) : null;
if ($term) {
try {
$model = $this->synonymFactory->create()
->setStoreId($storeId)
->setTerm($term);
$this->synonymRepository->save($model);
} catch (AlreadyExistsException $ex) {
continue;
} catch (CouldNotSaveException $ex) {
continue;
}
$count++;
}
}
$this->invalidateIndex();
$this->ioFile->rm($file);
} catch (LocalizedException $ex) {
$this->messageManager->addErrorMessage($ex->getMessage());
return $resultRedirect->setPath('*/*/import');
} catch (\Exception $ex) {
$this->messageManager->addErrorMessage(__('Something went wrong. Please try again'));
return $resultRedirect->setPath('*/*/import');
}
if ($count > 0) {
$this->messageManager->addSuccessMessage(__('%1 Synonym(s) has been imported', $count));
} else {
$this->messageManager->addWarningMessage('Imported file was empty or all terms are already exist');
}
} else {
$this->messageManager->addErrorMessage('There are no data to import');
return $resultRedirect->setPath('*/*/import');
}
return $resultRedirect->setPath('*/*/');
}
/**
* @return string
* @throws LocalizedException
* @throws \Exception
*/
private function uploadFile()
{
$path = $this->filesystem->getDirectoryRead(
DirectoryList::MEDIA
)->getAbsolutePath(
self::MEDIA_PATH
);
$this->ioFile->checkAndCreateFolder($path);
try {
/** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
$uploader = $this->fileUploaderFactory->create(['fileId' => 'amasty_elastic_file_upload']);
$uploader->setAllowedExtensions(['csv']);
$uploader->setAllowRenameFiles(true);
$result = $uploader->save($path);
} catch (\Exception $ex) {
throw new LocalizedException(__($ex->getMessage()));
}
if (isset($result['file']) && $this->ioFile->fileExists($path . '/' . $result['file'])) {
$file = $path . '/' . $result['file'];
} else {
throw new LocalizedException(__('Something wend wrong during saving file'));
}
return $file;
}
private function invalidateIndex()
{
$indexer = $this->indexerFactory->create()->load(DataMapperResolver::DEFAULT_DATA_INDEXER);
$indexer->invalidate();
}
}
@@ -0,0 +1,43 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Synonym;
use Magento\Framework\Controller\ResultFactory;
use Amasty\ElasticSearch\Api\Data\SynonymInterface;
class Edit extends AbstractSynonym
{
/**
* {@inheritdoc}
*/
public function execute()
{
$stopWordId = (int)$this->getRequest()->getParam(SynonymInterface::SYNONYM_ID);
try {
if ($stopWordId) {
/** @var \Amasty\ElasticSearch\Model\Synonym $model */
$model = $this->synonymRepository->getById($stopWordId);
} else {
$model = $this->synonymFactory->create();
}
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
$this->messageManager->addErrorMessage(__('This Synonym no longer exists.'));
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/');
}
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$text = $model->getId() ? __('Edit Synonym "%1"', $model->getTerm()) : __('New Synonym');
$this->initPage($resultPage)->getConfig()->getTitle()->prepend($text);
return $resultPage;
}
}
@@ -0,0 +1,26 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Synonym;
use Magento\Framework\Controller\ResultFactory;
class Import extends AbstractSynonym
{
/**
* {@inheritdoc}
*/
public function execute()
{
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$this->initPage($resultPage)
->getConfig()->getTitle()->prepend(__('Import Synonyms'));
return $resultPage;
}
}
@@ -0,0 +1,26 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Synonym;
use Magento\Framework\Controller\ResultFactory;
class Index extends AbstractSynonym
{
/**
* {@inheritdoc}
*/
public function execute()
{
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$this->initPage($resultPage)
->getConfig()->getTitle()->prepend(__('Manage Synonyms'));
return $resultPage;
}
}
@@ -0,0 +1,18 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Synonym;
class NewAction extends AbstractSynonym
{
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Forward $resultForward */
$resultForward = $this->resultForwardFactory->create();
return $resultForward->forward('edit');
}
}
@@ -0,0 +1,55 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Synonym;
use Amasty\ElasticSearch\Api\Data\SynonymInterface;
use Magento\Framework\Exception\NoSuchEntityException;
class Save extends AbstractSynonym
{
/**
* {@inheritdoc}
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$stopWordId = (int)$this->getRequest()->getParam(SynonymInterface::SYNONYM_ID);
try {
if ($stopWordId) {
/** @var \Amasty\ElasticSearch\Model\Synonym $model */
$model = $this->synonymRepository->getById($stopWordId);
} else {
$model = $this->synonymFactory->create();
}
$model->setTerm($this->getRequest()->getParam(SynonymInterface::TERM))
->setStoreId($this->getRequest()->getParam(SynonymInterface::STORE_ID));
$this->synonymRepository->save($model);
$this->messageManager->addSuccessMessage(__('You have saved the Synonym.'));
$this->indexerRegistry->get(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID)->invalidate();
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
$this->messageManager->addErrorMessage(
__('A Synonym with the same term already exists in an associated store.')
);
} catch (NoSuchEntityException $exception) {
$this->messageManager->addErrorMessage(__('This Synonym no longer exists.'));
$resultRedirect = $this->resultRedirectFactory->create();
}
$matches = null;
preg_match('/(\+|-|\/|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|\*|\?|:|\\\)/', $model->getTerm(), $matches);
if (count($matches)) {
$this->messageManager->addNoticeMessage(__(
'Synonyms with special symbols only works on elasticsearch engine v6.0 or higher.'
));
}
return $resultRedirect->setPath('*/*/');
}
}
@@ -0,0 +1,83 @@
<?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\Cron;
use Amasty\ElasticSearch\Api\Data\RelevanceRuleInterface;
use Amasty\ElasticSearch\Api\RelevanceRuleRepositoryInterface;
use Amasty\ElasticSearch\Model\ResourceModel\RelevanceRule\CollectionFactory;
class RelevanceRuleDailyUpdate
{
/**
* @var CollectionFactory
*/
private $ruleCollectionFactory;
/**
* @var RelevanceRuleRepositoryInterface
*/
private $relevanceRuleRepository;
public function __construct(
CollectionFactory $ruleCollectionFactory,
RelevanceRuleRepositoryInterface $relevanceRuleRepository
) {
$this->ruleCollectionFactory = $ruleCollectionFactory;
$this->relevanceRuleRepository = $relevanceRuleRepository;
}
public function execute(): void
{
foreach ($this->getRulesForEnable() as $relevanceRule) {
$relevanceRule->setIsEnabled(true);
$this->relevanceRuleRepository->save($relevanceRule);
}
foreach ($this->getRulesForDisable() as $relevanceRule) {
$relevanceRule->setIsEnabled(false);
$this->relevanceRuleRepository->save($relevanceRule);
}
}
/**
* @return RelevanceRuleInterface[]
*/
private function getRulesForEnable(): iterable
{
$collection = $this->ruleCollectionFactory->create();
$collection->addStatusFilter(false);
$collection->addFieldToFilter(
RelevanceRuleInterface::FROM_DATE,
['lt' => new \Zend_Db_Expr('NOW()')]
);
$collection->addFieldToFilter(
RelevanceRuleInterface::TO_DATE,
['gt' => new \Zend_Db_Expr('NOW()')]
);
return $collection;
}
/**
* @return RelevanceRuleInterface[]
*/
private function getRulesForDisable(): iterable
{
$collection = $this->ruleCollectionFactory->create();
$collection->addStatusFilter(true);
$collection->addFieldToFilter(
RelevanceRuleInterface::TO_DATE,
['lt' => new \Zend_Db_Expr('NOW()')]
);
return $collection;
}
}
@@ -0,0 +1,17 @@
<?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\Exception;
use Exception;
class Missing404Exception extends Exception
{
}
@@ -0,0 +1,17 @@
<?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\Exception;
use Exception;
class NoNodesAvailableException extends Exception
{
}
@@ -0,0 +1,17 @@
<?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\Exception;
use Magento\Framework\Exception\StateException;
class NotAmastySearchEngine extends StateException
{
}
@@ -0,0 +1,17 @@
<?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\Exception;
use Magento\Framework\Exception\LocalizedException;
class SystemPackageNotInstalled extends LocalizedException
{
}
@@ -0,0 +1,63 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Adapter;
interface AdapterInterface
{
/**
* @param array $documentData
* @param int $storeId
* @param string $indexerId
* @return array
*/
public function prepareDocuments(array $documentData, int $storeId, string $indexerId): array;
/**
* @param array $documents
* @param int $storeId
* @param string $indexerId
* @return void
* @throws \Exception
*/
public function saveDocuments(array $documents, int $storeId, string $indexerId): void;
/**
* @param array $documentIds
* @param int $storeId
* @param string $indexerId
* @return void
* @throws \Exception
*/
public function deleteDocuments(array $documentIds, int $storeId, string $indexerId): void;
/**
* @param int $storeId
* @param string $indexerId
* @return void
*/
public function cleanIndex(int $storeId, string $indexerId): void;
/**
* @param int $storeId
* @param string $indexerId
* @return void
*/
public function checkIndex(int $storeId, string $indexerId): void;
/**
* @param int $storeId
* @param string $indexerId
* @return void
*/
public function updateAlias(int $storeId, string $indexerId): void;
/**
* @return bool
*/
public function ping(): bool;
}
@@ -0,0 +1,230 @@
<?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\Adapter\Elasticsearch7;
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperResolverInterface;
use Amasty\ElasticSearch\Api\Data\Indexer\Structure\IndexBuilderInterface;
use Amasty\ElasticSearch\Model\Adapter\AdapterInterface;
use Amasty\ElasticSearch\Model\Adapter\FieldMappingBuilderInterface;
use Amasty\ElasticSearch\Model\Adapter\Index\IndexNameResolver;
use Amasty\ElasticSearch\Model\Adapter\SaveQueryBuilderInterface;
use Amasty\ElasticSearch\Model\Client\ClientInterface;
use Amasty\ElasticSearch\Model\Client\ClientRepositoryInterface;
use Amasty\ElasticSearch\Model\Debug;
use Amasty\ElasticSearch\Model\Indexer\Structure\EntityBuilder as EntityStructureBuilder;
use Exception;
class Elasticsearch implements AdapterInterface
{
/**
* @var FieldMappingBuilderInterface
*/
private $fieldMappingBuilder;
/**
* @var IndexNameResolver
*/
private $indexNameResolver;
/**
* @var ClientRepositoryInterface
*/
private $clientRepository;
/**
* @var IndexBuilderInterface
*/
private $indexBuilder;
/**
* @var EntityStructureBuilder
*/
private $entityStructureBuilder;
/**
* @var DataMapperResolverInterface
*/
private $dataMapperResolver;
/**
* @var SaveQueryBuilderInterface
*/
private $saveQueryBuilder;
/**
* @var Debug
*/
private $debug;
/**
* @var ClientInterface|null
*/
private $client;
/**
* @var string[] ['store_id' => 'index_name']
*/
private $indexNameByStore = [];
public function __construct(
IndexNameResolver $indexNameResolver,
ClientRepositoryInterface $clientRepository,
IndexBuilderInterface $indexBuilder,
EntityStructureBuilder $entityStructureBuilder,
FieldMappingBuilderInterface $fieldMappingBuilder,
DataMapperResolverInterface $dataMapperResolver,
SaveQueryBuilderInterface $saveQueryBuilder,
Debug $debug,
?ClientInterface $client = null
) {
$this->indexNameResolver = $indexNameResolver;
$this->clientRepository = $clientRepository;
$this->indexBuilder = $indexBuilder;
$this->entityStructureBuilder = $entityStructureBuilder;
$this->fieldMappingBuilder = $fieldMappingBuilder;
$this->dataMapperResolver = $dataMapperResolver;
$this->saveQueryBuilder = $saveQueryBuilder;
$this->debug = $debug;
$this->client = $client;
}
public function prepareDocuments(array $documentData, int $storeId, string $indexerId): array
{
$documents = [];
if (!empty($documentData)) {
$documents = $this->dataMapperResolver->mapEntityData(
$documentData,
$storeId,
['indexer_id' => $indexerId]
);
}
return $documents;
}
public function saveDocuments(array $documents, int $storeId, string $indexerId): void
{
if (!empty($documents)) {
try {
$indexName = $this->indexNameResolver->getIndexName($indexerId, $storeId, $this->indexNameByStore);
if ($this->getClient()->indexExists($indexName)) {
$query = $this->saveQueryBuilder->execute($documents, $indexName);
$this->getClient()->bulkQuery($query);
}
} catch (Exception $e) {
$this->debug->debug($e);
throw $e;
}
}
}
public function deleteDocuments(array $documentIds, int $storeId, string $indexerId): void
{
try {
$indexName = $this->indexNameResolver->getIndexName($indexerId, $storeId, $this->indexNameByStore);
if (!$this->getClient()->indexExists($indexName)) {
return;
}
$query = $this->saveQueryBuilder->execute(
$documentIds,
$indexName,
SaveQueryBuilderInterface::BULK_ACTION_DELETE
);
$this->getClient()->bulkQuery($query);
} catch (Exception $e) {
$this->debug->debug($e);
throw $e;
}
}
public function cleanIndex(int $storeId, string $indexerId): void
{
// needed to fix bug with double indices in alias because of second reindex in same process
unset($this->indexNameByStore[$storeId]);
$this->checkIndex($storeId, $indexerId);
$indexName = $this->indexNameResolver->getIndexName($indexerId, $storeId, $this->indexNameByStore);
if ($this->getClient()->isEmptyIndex($indexName)) {
return;
}
$indexPattern = $this->indexNameResolver->getIndexAlias($indexerId, $storeId) . '_v';
$version = (int)(str_replace($indexPattern, '', $indexName));
$newIndexName = $indexPattern . (++$version);
if ($this->getClient()->indexExists($newIndexName)) {
$this->getClient()->deleteIndex($newIndexName);
}
$this->prepareIndex($storeId, $newIndexName, $indexerId);
}
public function checkIndex(int $storeId, string $indexerId): void
{
$indexName = $this->indexNameResolver->getIndexName($indexerId, $storeId, $this->indexNameByStore);
if (!$this->getClient()->indexExists($indexName)) {
$this->prepareIndex($storeId, $indexName, $indexerId);
}
$alias = $this->indexNameResolver->getIndexAlias($indexerId, $storeId);
if (!$this->getClient()->existsAlias($alias, $indexName)) {
$this->getClient()->updateAlias($alias, $indexName);
}
}
public function updateAlias(int $storeId, string $indexerId): void
{
if (!isset($this->indexNameByStore[$storeId])) {
return;
}
$alias = $this->indexNameResolver->getIndexAlias($indexerId, $storeId);
$oldIndex = $this->indexNameResolver->getIndexNameByAlias($alias, $storeId, $this->indexNameByStore);
if (!empty($oldIndex)) {
$params['body']['actions'][] = ['remove' => ['alias' => $alias, 'index' => $oldIndex]];
}
$params['body']['actions'][] = ['add' => ['alias' => $alias, 'index' => $this->indexNameByStore[$storeId]]];
$this->getClient()->updateAliases($params);
if ($oldIndex) {
$this->getClient()->deleteIndex($oldIndex);
}
}
public function ping(): bool
{
return $this->getClient()->ping();
}
private function prepareIndex(int $storeId, string $indexName, string $indexerId): void
{
$this->indexBuilder->setStoreId($storeId);
$this->getClient()->createIndex($indexName, $this->indexBuilder->build());
$mappingData = $this->fieldMappingBuilder->execute(
$indexName,
$storeId,
$this->entityStructureBuilder->build($indexerId)
);
$this->getClient()->putMapping($mappingData);
$this->indexNameByStore[$storeId] = $indexName;
}
private function getClient(): ClientInterface
{
if ($this->client === null) {
$this->client = $this->clientRepository->get();
}
return $this->client;
}
}
@@ -0,0 +1,80 @@
<?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\Adapter\Elasticsearch7;
use Amasty\ElasticSearch\Model\Adapter\FieldMappingBuilderInterface;
use Amasty\ElasticSearch\Model\Config;
use Amasty\ElasticSearch\Model\Indexer\Structure\DynamicTemplateMapper;
use Amasty\ElasticSearch\Model\Indexer\Structure\EntityBuilder\Product;
class FieldMappingBuilder implements FieldMappingBuilderInterface
{
/**
* @var Config
*/
private $config;
/**
* @var DynamicTemplateMapper
*/
private $dynamicTemplateMapper;
public function __construct(Config $config, DynamicTemplateMapper $dynamicTemplateMapper)
{
$this->config = $config;
$this->dynamicTemplateMapper = $dynamicTemplateMapper;
}
public function execute(string $index, int $storeId, array $fields): array
{
$entityType = $this->config->getEntityType();
$params = [
'index' => $index,
'type' => $entityType,
'body' => [
$entityType => [
'properties' => [],
'dynamic_templates' => [
$this->dynamicTemplateMapper->map($storeId)
],
'numeric_detection' => false,
]
]
];
$isAllowSpecialChars = $this->config->allowSpecialChars($storeId);
$hasStemming = $this->config->hasStemming($storeId);
foreach ($fields as $field => $fieldInfo) {
if ($fieldInfo['type'] === Product::ATTRIBUTE_TYPE_TEXT) {
$fieldInfo['fielddata'] = true;
if ($isAllowSpecialChars) {
$fieldInfo['analyzer'] = 'allow_spec_chars';
}
if ($hasStemming) {
$fieldInfo['fields']['stemming'] = [
'type' => Product::ATTRIBUTE_TYPE_TEXT,
'fielddata' => true,
'analyzer' => 'stem',
'index' => true
];
}
}
$params['body'][$entityType]['properties'][$field] = $fieldInfo;
}
$params['include_type_name'] = true;
return $params;
}
}
@@ -0,0 +1,54 @@
<?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\Adapter\Elasticsearch7;
use Amasty\ElasticSearch\Model\Adapter\SaveQueryBuilderInterface;
use Amasty\ElasticSearch\Model\Config;
class SaveQueryBuilder implements SaveQueryBuilderInterface
{
/**
* @var Config
*/
private $config;
public function __construct(Config $config)
{
$this->config = $config;
}
public function execute(array $documents, string $indexName, string $action = self::BULK_ACTION_INDEX): array
{
$entityType = $this->config->getEntityType();
$bulkArray = [
'index' => $indexName,
'type' => $entityType,
'body' => [],
'refresh' => true,
];
foreach ($documents as $id => $document) {
$bulkArray['body'][] = [
$action => [
'_id' => $id,
'_type' => $entityType,
'_index' => $indexName
]
];
if ($action === self::BULK_ACTION_INDEX) {
$bulkArray['body'][] = $document;
}
}
return $bulkArray;
}
}
@@ -0,0 +1,75 @@
<?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\Adapter\Elasticsearch8;
use Amasty\ElasticSearch\Model\Adapter\FieldMappingBuilderInterface;
use Amasty\ElasticSearch\Model\Config;
use Amasty\ElasticSearch\Model\Indexer\Structure\DynamicTemplateMapper;
use Amasty\ElasticSearch\Model\Indexer\Structure\EntityBuilder\Product;
class FieldMappingBuilder implements FieldMappingBuilderInterface
{
/**
* @var Config
*/
private $config;
/**
* @var DynamicTemplateMapper
*/
private $dynamicTemplateMapper;
public function __construct(Config $config, DynamicTemplateMapper $dynamicTemplateMapper)
{
$this->config = $config;
$this->dynamicTemplateMapper = $dynamicTemplateMapper;
}
public function execute(string $index, int $storeId, array $fields): array
{
$params = [
'index' => $index,
'body' => [
'properties' => [],
'dynamic_templates' => [
$this->dynamicTemplateMapper->map($storeId)
],
'numeric_detection' => false,
],
];
$isAllowSpecialChars = $this->config->allowSpecialChars($storeId);
$hasStemming = $this->config->hasStemming($storeId);
foreach ($fields as $field => $fieldInfo) {
if ($fieldInfo['type'] === Product::ATTRIBUTE_TYPE_TEXT) {
$fieldInfo['fielddata'] = true;
if ($isAllowSpecialChars) {
$fieldInfo['analyzer'] = 'allow_spec_chars';
}
if ($hasStemming) {
$fieldInfo['fields']['stemming'] = [
'type' => Product::ATTRIBUTE_TYPE_TEXT,
'fielddata' => true,
'analyzer' => 'stem',
'index' => true
];
}
}
$params['body']['properties'][$field] = $fieldInfo;
}
$params['include_type_name'] = true;
return $params;
}
}
@@ -0,0 +1,39 @@
<?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\Adapter\Elasticsearch8;
use Amasty\ElasticSearch\Model\Adapter\SaveQueryBuilderInterface;
class SaveQueryBuilder implements SaveQueryBuilderInterface
{
public function execute(array $documents, string $indexName, string $action = self::BULK_ACTION_INDEX): array
{
$bulkArray = [
'index' => $indexName,
'body' => [],
'refresh' => true,
];
foreach ($documents as $id => $document) {
$bulkArray['body'][] = [
$action => [
'_id' => $id,
'_index' => $indexName
]
];
if ($action === self::BULK_ACTION_INDEX) {
$bulkArray['body'][] = $document;
}
}
return $bulkArray;
}
}
@@ -0,0 +1,19 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Adapter;
interface FieldMappingBuilderInterface
{
/**
* @param string $index
* @param array $fields
* @param int $storeId
* @return array
*/
public function execute(string $index, int $storeId, array $fields): array;
}
@@ -0,0 +1,97 @@
<?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\Adapter\Index;
use Amasty\ElasticSearch\Model\Client\ClientInterface;
use Amasty\ElasticSearch\Model\Client\ClientRepositoryInterface;
use Amasty\ElasticSearch\Model\Config;
class IndexNameResolver
{
/**
* @var Config
*/
private $config;
/**
* @var ClientRepositoryInterface
*/
private $clientRepository;
/**
* @var ClientInterface|null
*/
private $client;
public function __construct(
Config $config,
ClientRepositoryInterface $clientRepository,
?ClientInterface $client = null
) {
$this->config = $config;
$this->clientRepository = $clientRepository;
$this->client = $client;
}
public function getIndexAlias(string $indexerId, int $storeId): string
{
return $this->config->getIndexName($indexerId, $storeId);
}
public function getIndexName(string $indexerId, int $storeId, array $preparedIndexName = []): string
{
// TODO: check original method for Xsearch
if (!isset($preparedIndexName[$storeId])) {
$alias = $this->getIndexAlias($indexerId, $storeId);
$indexName = $this->getIndexNameByAlias($alias, $storeId);
if (empty($indexName)) {
$indexName = $alias . '_v1';
}
return $indexName;
}
return $preparedIndexName[$storeId];
}
public function getIndexNameByAlias(string $alias, int $storeId, array $preparedIndexName = []): string
{
$storeIndex = '';
if ($this->getClient()->existsAlias($alias)) {
$indexPattern = $alias . '_v';
$alias = $this->getClient()->getAlias($alias);
$indices = array_keys($alias);
natsort($indices);
foreach ($indices as $index) {
if (strpos($index, $indexPattern) === 0) {
if (isset($preparedIndexName[$storeId]) && $preparedIndexName[$storeId] == $index) {
continue;
}
$storeIndex = $index;
break;
}
}
}
return $storeIndex;
}
private function getClient(): ?ClientInterface
{
if ($this->client === null) {
$this->client = $this->clientRepository->get();
}
return $this->client;
}
}
@@ -0,0 +1,35 @@
<?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\Adapter\Opensearch;
use Amasty\ElasticSearch\Model\Adapter\Elasticsearch8\FieldMappingBuilder as Elasticsearch8FieldMappingBuilder;
use Amasty\ElasticSearch\Model\Adapter\FieldMappingBuilderInterface;
class FieldMappingBuilder implements FieldMappingBuilderInterface
{
/**
* @var Elasticsearch8FieldMappingBuilder
*/
private $fieldMappingBuilder;
public function __construct(Elasticsearch8FieldMappingBuilder $fieldMappingBuilder)
{
$this->fieldMappingBuilder = $fieldMappingBuilder;
}
public function execute(string $index, int $storeId, array $fields): array
{
$result = $this->fieldMappingBuilder->execute($index, $storeId, $fields);
unset($result['include_type_name']);
return $result;
}
}
@@ -0,0 +1,22 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Adapter;
interface SaveQueryBuilderInterface
{
public const BULK_ACTION_INDEX = 'index';
public const BULK_ACTION_DELETE = 'delete';
/**
* @param array $documents
* @param string $indexName
* @param string $action
* @return array
*/
public function execute(array $documents, string $indexName, string $action = self::BULK_ACTION_INDEX): array;
}
@@ -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\Adminhtml\System\Config\Backend;
class LongTail extends \Magento\Framework\App\Config\Value
{
/**
* @var \Magento\Framework\Indexer\IndexerRegistry
*/
protected $indexerRegistry;
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
$this->indexerRegistry = $indexerRegistry;
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
}
/**
* After save call
* Invalidate catalog search index if longtail was changed
*
* @return $this
*/
public function afterSave()
{
parent::afterSave();
if ($this->isValueChanged()) {
$this->indexerRegistry->get(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID)->invalidate();
}
return $this;
}
}
@@ -0,0 +1,59 @@
<?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\Client;
use Amasty\ElasticSearch\Exception\NotAmastySearchEngine;
use Magento\Framework\ObjectManagerInterface;
class ClientFactory
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var array
*/
private $engines;
/**
* @var ConfigBuilder
*/
private $configBuilder;
public function __construct(
ConfigBuilder $configBuilder,
ObjectManagerInterface $objectManager,
array $engines = []
) {
$this->configBuilder = $configBuilder;
$this->objectManager = $objectManager;
$this->engines = $engines;
}
/**
* @throws NotAmastySearchEngine
*/
public function create(string $searchEngine, array $options): ClientInterface
{
if (!isset($this->engines[$searchEngine])) {
throw new NotAmastySearchEngine(__('Selected engine not supported by Amasty_Elasticsearch module.'));
}
/** @var ClientInterface $client */
$client = $this->objectManager->create($this->engines[$searchEngine], [
'options' => $this->configBuilder->build($searchEngine, $options)
]);
return $client;
}
}
@@ -0,0 +1,112 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Client;
interface ClientInterface
{
/**
* @param array $query
* @return array
*/
public function query(array $query): array;
/**
* @param array $query
* @return array
* @throws \Amasty\ElasticSearch\Exception\Missing404Exception
*/
public function search(array $query): array;
/**
* @param array $query
* @return array
*/
public function bulkQuery(array $query): array;
/**
* @param string $index
* @return bool
*/
public function isEmptyIndex(string $index): bool;
/**
* @param string $index
* @param array $settings
* @return void
*/
public function createIndex(string $index, array $settings): void;
/**
* @param string $index
* @return void
*/
public function deleteIndex(string $index): void;
/**
* @param string $index
* @return bool
*/
public function indexExists(string $index): bool;
/**
* @param string $alias
* @param string $index
* @return bool
* @throws \Amasty\ElasticSearch\Exception\NoNodesAvailableException
*/
public function existsAlias(string $alias, string $index = ''): bool;
/**
* @param string $alias
* @return array
*/
public function getAlias(string $alias): array;
/**
* @param string $alias
* @param string $newIndex
* @return void
*/
public function updateAlias(string $alias, string $newIndex): void;
/**
* @param array $params
* @return void
*/
public function updateAliases(array $params): void;
/**
* @param array $params
* @return void
*/
public function putMapping(array $params): void;
/**
* @return bool
*/
public function ping(): bool;
/**
* Check if correct system package installed.
* @return bool
*/
public function isSystemPackageAvailable(): bool;
/**
* Get version of system package needed to install.
* @return string
*/
public function getNeededSystemPackageVersion(): string;
/**
* @return string
*/
public function getSystemPackageName(): string;
public function getCurrentEngineVersion(): string;
}
@@ -0,0 +1,67 @@
<?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\Client;
use Amasty\ElasticSearch\Model\Config;
use Magento\Framework\Serialize\Serializer\Json as Serializer;
class ClientRepository implements ClientRepositoryInterface
{
/**
* @var Config
*/
private $config;
/**
* @var Serializer
*/
private $serializer;
/**
* @var ClientFactory
*/
private $clientFactory;
/**
* @var ClientInterface[] ['key' => client, ...]
*/
private $clients = [];
public function __construct(
Config $config,
Serializer $serializer,
ClientFactory $clientFactory
) {
$this->config = $config;
$this->serializer = $serializer;
$this->clientFactory = $clientFactory;
}
public function get(?string $searchEngine = null, ?array $options = null): ClientInterface
{
$searchEngine = $searchEngine ?? $this->config->getEngine();
$options = $options ?? $this->config->prepareConnectionData($options ?? []);
$cacheKey = $this->getCacheKey($searchEngine, $options);
if (!isset($this->clients[$cacheKey])) {
$this->clients[$cacheKey] = $this->clientFactory->create($searchEngine, $options);
}
return $this->clients[$cacheKey];
}
private function getCacheKey(string $searchEngine, array $options): string
{
$options['engine'] = $searchEngine;
$serializeData = $this->serializer->serialize(ksort($options));
return sha1($serializeData);
}
}
@@ -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\Client;
/**
* Interface ClientRepositoryInterface
*/
interface ClientRepositoryInterface
{
/**
* @param string|null $searchEngine
* @param array|null $options
* @return \Amasty\ElasticSearch\Model\Client\ClientInterface
*/
public function get(?string $searchEngine = null, ?array $options = null): ClientInterface;
}
@@ -0,0 +1,40 @@
<?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\Client;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\RuntimeException;
class ConfigBuilder
{
/**
* @var ConfigBuilderInterface[] ['engine' => builder, ...]
*/
private $configBuilders;
public function __construct(array $configBuilders = [])
{
$this->configBuilders = $configBuilders;
}
/**
* @throws LocalizedException
*/
public function build(string $engine, array $clientOptions): array
{
$configBuilder = $this->configBuilders[$engine] ?? $this->configBuilders['default'] ?? null;
if ($configBuilder === null) {
throw new RuntimeException(__('Config builder not set.'));
}
return $configBuilder->build($clientOptions);
}
}
@@ -0,0 +1,18 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Client;
interface ConfigBuilderInterface
{
/**
* @param array $clientOptions
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function build(array $clientOptions): array;
}
@@ -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\Client\Defaults;
use Amasty\ElasticSearch\Model\Client\ConfigBuilderInterface;
use Magento\Framework\Exception\LocalizedException;
class ConfigBuilder implements ConfigBuilderInterface
{
public function build(array $clientOptions): array
{
if (empty($clientOptions['hostname'])
|| (array_key_exists('enableAuth', $clientOptions) && 1 == $clientOptions['enableAuth']
&& (empty($clientOptions['username']) || empty($clientOptions['password'])))
) {
throw new LocalizedException(__(
'It is impossible to perform the search. Please make sure Elasticsearch Engine'
. ' is installed on the server and configured properly.'
));
}
$host = preg_replace('/http[s]?:\/\//i', '', $clientOptions['hostname']);
//@codingStandardsIgnoreLine
$protocol = parse_url($clientOptions['hostname'], PHP_URL_SCHEME) ?: 'http';
if (!empty($clientOptions['port'])) {
$host .= ':' . $clientOptions['port'];
}
if (!empty($clientOptions['enableAuth']) && ($clientOptions['enableAuth'] == 1)) {
$host = sprintf('%s:%s@%s', $clientOptions['username'], $clientOptions['password'], $host);
}
$host = sprintf('%s://%s', $protocol, $host);
$clientOptions['hosts'] = [$host];
return $clientOptions;
}
}
@@ -0,0 +1,192 @@
<?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\Client\Elasticsearch7;
use Amasty\ElasticSearch\Exception\Missing404Exception as AmastyMissing404Exception;
use Amasty\ElasticSearch\Exception\NoNodesAvailableException as AmastyNoNodesAvailableException;
use Amasty\ElasticSearch\Exception\SystemPackageNotInstalled;
use Amasty\ElasticSearch\Model\Client\ClientInterface;
use Elasticsearch\Client as ElasticsearchClient;
use Elasticsearch\ClientBuilder;
use Elasticsearch\Common\Exceptions\Missing404Exception;
use Elasticsearch\Common\Exceptions\NoNodesAvailableException;
use Magento\Framework\Exception\LocalizedException;
class Client implements ClientInterface
{
/**
* @var array
*/
private $options;
/**
* @var ElasticsearchClient[] ['pid' => client, ...]
*/
private $client = [];
/**
* @var string|null
*/
private $elasticEngineVersion;
public function __construct(array $options)
{
$this->options = $options;
}
public function query(array $query): array
{
return $this->getElasticsearchClient()->search($query);
}
public function search(array $query): array
{
try {
return $this->getElasticsearchClient()->search($query);
} catch (Missing404Exception $e) {
$message = __('Elasticsearch index not found. Run "bin/magento indexer:reindex catalogsearch_fulltext".');
throw new AmastyMissing404Exception($message, 0, $e);
}
}
public function bulkQuery(array $query): array
{
return $this->getElasticsearchClient()->bulk($query);
}
public function isEmptyIndex(string $index): bool
{
$stats = $this->getElasticsearchClient()->indices()->stats(['index' => $index, 'metric' => 'docs']);
return !isset($stats['indices'][$index]) || $stats['indices'][$index]['primaries']['docs']['count'] === 0;
}
public function createIndex(string $index, array $settings): void
{
$params = [
'index' => $index,
'body' => []
];
if (!empty($settings)) {
$params['body']['settings'] = $settings;
}
$this->getElasticsearchClient()->indices()->create($params);
}
public function deleteIndex(string $index): void
{
$this->getElasticsearchClient()->indices()->delete(['index' => $index]);
}
public function indexExists(string $index): bool
{
return $this->getElasticsearchClient()->indices()->exists(['index' => $index]);
}
public function existsAlias(string $alias, string $index = ''): bool
{
$params = ['name' => $alias];
if ($index) {
$params['index'] = $index;
}
try {
return $this->getElasticsearchClient()->indices()->existsAlias($params);
} catch (NoNodesAvailableException $e) {
$message = __('Elasticsearch server not found. Check "Stores > Configuration'
. ' > Amasty Elastic Search > Connection" and make sure that "Test Connection"'
.' is successfull with appropriate host name and port.');
throw new AmastyNoNodesAvailableException($message, 0, $e);
}
}
public function getAlias(string $alias): array
{
return $this->getElasticsearchClient()->indices()->getAlias(['name' => $alias]);
}
public function updateAlias(string $alias, string $newIndex): void
{
$params['body']['actions'][] = ['add' => ['alias' => $alias, 'index' => $newIndex]];
$this->updateAliases($params);
}
public function updateAliases(array $params): void
{
$this->getElasticsearchClient()->indices()->updateAliases($params);
}
public function putMapping(array $params): void
{
$this->getElasticsearchClient()->indices()->putMapping($params);
}
public function ping(): bool
{
return $this->getElasticsearchClient()->ping(
['client' => ['timeout' => $this->options['timeout']]]
);
}
public function isSystemPackageAvailable(): bool
{
return class_exists(ClientBuilder::class);
}
public function getNeededSystemPackageVersion(): string
{
return '7.17';
}
public function getSystemPackageName(): string
{
return 'elasticsearch/elasticsearch';
}
public function getCurrentEngineVersion(): string
{
if ($this->elasticEngineVersion === null) {
$elasticInfo = $this->getElasticsearchClient()->info();
$this->elasticEngineVersion = $elasticInfo['version']['number'] ?? null;
}
return $this->elasticEngineVersion;
}
/**
* @throws SystemPackageNotInstalled
*/
private function getElasticsearchClient(): ElasticsearchClient
{
$pid = getmypid();
if (!isset($this->client[$pid])) {
$this->validateClient();
$this->client[$pid] = ClientBuilder::fromConfig($this->options, true);
}
return $this->client[$pid];
}
/**
* @throws SystemPackageNotInstalled
*/
private function validateClient(): void
{
if (!$this->isSystemPackageAvailable()) {
throw new SystemPackageNotInstalled(__(
'Elasticsearch package is not installed. Please, run the following command
in the SSH: composer require %1 ^%2',
$this->getSystemPackageName(),
$this->getNeededSystemPackageVersion()
));
}
}
}
@@ -0,0 +1,195 @@
<?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\Client\Elasticsearch8;
use Amasty\ElasticSearch\Exception\Missing404Exception as AmastyMissing404Exception;
use Amasty\ElasticSearch\Exception\NoNodesAvailableException as AmastyNoNodesAvailableException;
use Amasty\ElasticSearch\Exception\SystemPackageNotInstalled;
use Amasty\ElasticSearch\Model\Client\ClientInterface;
use Elastic\Elasticsearch\Client as ElasticsearchClient;
use Elastic\Elasticsearch\ClientBuilder;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Transport\Exception\NoNodeAvailableException;
use Magento\Framework\Exception\LocalizedException;
class Client implements ClientInterface
{
/**
* @var array
*/
private $options;
/**
* @var ElasticsearchClient[] ['pid' => client, ...]
*/
private $client = [];
/**
* @var string|null
*/
private $elasticEngineVersion;
public function __construct(array $options)
{
$this->options = $options;
}
public function query(array $query): array
{
return $this->getElasticsearchClient()->search($query)->asArray();
}
public function search(array $query): array
{
try {
return $this->getElasticsearchClient()->search($query)->asArray();
} catch (ClientResponseException $e) {
$message = __('Elasticsearch index not found. Run "bin/magento indexer:reindex catalogsearch_fulltext".');
throw new AmastyMissing404Exception($message->render(), 0, $e);
}
}
/**
* TODO: check
*/
public function bulkQuery(array $query): array
{
return $this->getElasticsearchClient()->bulk($query)->asArray();
}
public function isEmptyIndex(string $index): bool
{
$stats = $this->getElasticsearchClient()->indices()->stats(['index' => $index, 'metric' => 'docs'])->asArray();
return !isset($stats['indices'][$index]) || $stats['indices'][$index]['primaries']['docs']['count'] === 0;
}
public function createIndex(string $index, array $settings): void
{
$params = [
'index' => $index,
'body' => []
];
if (!empty($settings)) {
$params['body']['settings'] = $settings;
}
$this->getElasticsearchClient()->indices()->create($params);
}
public function deleteIndex(string $index): void
{
$this->getElasticsearchClient()->indices()->delete(['index' => $index]);
}
public function indexExists(string $index): bool
{
return $this->getElasticsearchClient()->indices()->exists(['index' => $index])->asBool();
}
/**
* TODO: check
*/
public function existsAlias(string $alias, string $index = ''): bool
{
$params = ['name' => $alias];
if ($index) {
$params['index'] = $index;
}
try {
return $this->getElasticsearchClient()->indices()->existsAlias($params)->asBool();
} catch (NoNodeAvailableException $e) {
$message = __('Elasticsearch server not found. Check "Stores > Configuration'
. ' > Amasty Elastic Search > Connection" and make sure that "Test Connection"'
.' is successfull with appropriate host name and port.');
throw new AmastyNoNodesAvailableException($message->render(), 0, $e);
}
}
public function getAlias(string $alias): array
{
return $this->getElasticsearchClient()->indices()->getAlias(['name' => $alias])->asArray();
}
public function updateAlias(string $alias, string $newIndex): void
{
$params['body']['actions'][] = ['add' => ['alias' => $alias, 'index' => $newIndex]];
$this->updateAliases($params);
}
public function updateAliases(array $params): void
{
$this->getElasticsearchClient()->indices()->updateAliases($params);
}
public function putMapping(array $params): void
{
$this->getElasticsearchClient()->indices()->putMapping($params);
}
public function ping(): bool
{
return $this->getElasticsearchClient()->ping(
['client' => ['timeout' => $this->options['timeout']]]
)->asBool();
}
public function isSystemPackageAvailable(): bool
{
return class_exists(ClientBuilder::class);
}
public function getNeededSystemPackageVersion(): string
{
return '8.6';
}
public function getSystemPackageName(): string
{
return 'elasticsearch/elasticsearch';
}
public function getCurrentEngineVersion(): string
{
if ($this->elasticEngineVersion === null) {
$elasticInfo = $this->getElasticsearchClient()->info()->asArray();
$this->elasticEngineVersion = $elasticInfo['version']['number'] ?? null;
}
return $this->elasticEngineVersion;
}
private function getElasticsearchClient(): ElasticsearchClient
{
$pid = getmypid();
if (!isset($this->client[$pid])) {
$this->validateClient();
$this->client[$pid] = ClientBuilder::fromConfig($this->options, true);
}
return $this->client[$pid];
}
/**
* @throws SystemPackageNotInstalled
*/
private function validateClient(): void
{
if (!$this->isSystemPackageAvailable()) {
throw new SystemPackageNotInstalled(__(
'Elasticsearch package is not installed. Please, run the following command
in the SSH: composer require %1 ^%2',
$this->getSystemPackageName(),
$this->getNeededSystemPackageVersion()
));
}
}
}
@@ -0,0 +1,193 @@
<?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\Client\Opensearch;
use Amasty\ElasticSearch\Exception\Missing404Exception as AmastyMissing404Exception;
use Amasty\ElasticSearch\Exception\NoNodesAvailableException as AmastyNoNodesAvailableException;
use Amasty\ElasticSearch\Exception\SystemPackageNotInstalled;
use Amasty\ElasticSearch\Model\Client\ClientInterface;
use OpenSearch\Client as OpenSearchClient;
use OpenSearch\ClientBuilder;
use OpenSearch\Common\Exceptions\Missing404Exception;
use OpenSearch\Common\Exceptions\NoNodesAvailableException;
class Client implements ClientInterface
{
/**
* @var array
*/
private $options;
/**
* @var OpenSearchClient[] ['pid' => client, ...]
*/
private $client = [];
/**
* @var string|null
*/
private $elasticEngineVersion;
public function __construct(array $options)
{
$this->options = $options;
}
public function query(array $query): array
{
unset($query['type']);
return $this->getOpenSearchClient()->search($query);
}
public function search(array $query): array
{
try {
unset($query['type']);
return $this->getOpenSearchClient()->search($query);
} catch (Missing404Exception $e) {
$message = __('Opensearch index not found. Run "bin/magento indexer:reindex catalogsearch_fulltext".');
throw new AmastyMissing404Exception($message, 0, $e);
}
}
public function bulkQuery(array $query): array
{
return $this->getOpenSearchClient()->bulk($query);
}
public function isEmptyIndex(string $index): bool
{
$stats = $this->getOpenSearchClient()->indices()->stats(['index' => $index, 'metric' => 'docs']);
return !isset($stats['indices'][$index]) || $stats['indices'][$index]['primaries']['docs']['count'] === 0;
}
public function createIndex(string $index, array $settings): void
{
$params = [
'index' => $index,
'body' => []
];
if (!empty($settings)) {
$params['body']['settings'] = $settings;
}
$this->getOpenSearchClient()->indices()->create($params);
}
public function deleteIndex(string $index): void
{
$this->getOpenSearchClient()->indices()->delete(['index' => $index]);
}
public function indexExists(string $index): bool
{
return $this->getOpenSearchClient()->indices()->exists(['index' => $index]);
}
public function existsAlias(string $alias, string $index = ''): bool
{
$params = ['name' => $alias];
if ($index) {
$params['index'] = $index;
}
try {
return $this->getOpenSearchClient()->indices()->existsAlias($params);
} catch (NoNodesAvailableException $e) {
$message = __('Opensearch server not found. Check "Stores > Configuration'
. ' > Amasty Elastic Search > Connection" and make sure that "Test Connection"'
.' is successfull with appropriate host name and port.');
throw new AmastyNoNodesAvailableException($message, 0, $e);
}
}
public function getAlias(string $alias): array
{
return $this->getOpenSearchClient()->indices()->getAlias(['name' => $alias]);
}
public function updateAlias(string $alias, string $newIndex): void
{
$params['body']['actions'][] = ['add' => ['alias' => $alias, 'index' => $newIndex]];
$this->updateAliases($params);
}
public function updateAliases(array $params): void
{
$this->getOpenSearchClient()->indices()->updateAliases($params);
}
public function putMapping(array $params): void
{
$this->getOpenSearchClient()->indices()->putMapping($params);
}
public function ping(): bool
{
return $this->getOpenSearchClient()->ping(
['client' => ['timeout' => $this->options['timeout']]]
);
}
public function isSystemPackageAvailable(): bool
{
return class_exists(ClientBuilder::class);
}
public function getNeededSystemPackageVersion(): string
{
return '2.0.0';
}
public function getSystemPackageName(): string
{
return 'opensearch-project/opensearch-php';
}
public function getCurrentEngineVersion(): string
{
if ($this->elasticEngineVersion === null) {
$elasticInfo = $this->getOpenSearchClient()->info();
$this->elasticEngineVersion = $elasticInfo['version']['number'] ?? null;
}
return $this->elasticEngineVersion;
}
/**
* @throws SystemPackageNotInstalled
*/
private function getOpenSearchClient(): OpenSearchClient
{
$pid = getmypid();
if (!isset($this->client[$pid])) {
$this->validateClient();
$this->client[$pid] = ClientBuilder::fromConfig($this->options, true);
}
return $this->client[$pid];
}
/**
* @throws SystemPackageNotInstalled
*/
private function validateClient(): void
{
if (!$this->isSystemPackageAvailable()) {
throw new SystemPackageNotInstalled(__(
'Elasticsearch package is not installed. Please, run the following command
in the SSH: composer require %1 ^%2',
$this->getSystemPackageName(),
$this->getNeededSystemPackageVersion()
));
}
}
}
+299
View File
@@ -0,0 +1,299 @@
<?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;
use Amasty\ElasticSearch\Model\Config\QuerySettingsProcessor;
use Amasty\ElasticSearch\Model\Source\CustomAnalyzer;
use Magento\Framework\App\ObjectManager;
use Magento\Store\Model\ScopeInterface;
class Config
{
public const ELASTIC_SEARCH_SECTION_NAME = 'amasty_elastic/';
public const DEBUG_SECTION_NAME = 'debug/';
public const ELASTIC_SEARCH_ENGINE = 'amasty_elastic';
public const CONFIG_ENGINE_PATH = 'amasty_elastic/connection/engine';
public const ELASTIC_SEARCH_TYPE_DOCUMENT = 'document';
public const ELASTIC_SEARCH_DEFAULT_ENTITY_TYPE = 'product';
public const ELASTIC_SEARCH_DEFAULT_TIMEOUT = 15;
public const ALLOW_FURTHER_QUERY_PATH = 'catalog/allow_further_query';
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var Config\QuerySettings
*/
private $querySettings;
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
?Config\QuerySettings $querySettingsOld = null, // @deprecated TODO delete
QuerySettingsProcessor $querySettings = null // TODO move to not optional
) {
$this->scopeConfig = $scopeConfig;
// OM for backward compatibility
$this->querySettings =
$querySettings ?? ObjectManager::getInstance()->get(QuerySettingsProcessor::class);
}
/**
* @param string $attributeCode
* @return array|null
*/
public function getQuerySettingByAttributeCode($attributeCode)
{
return $this->querySettings->getConfigValue($attributeCode);
}
/**
* @param string $path
* @param int $storeId
* @return mixed
*/
public function getModuleConfig($path, $storeId = null)
{
return $this->scopeConfig->getValue(
self::ELASTIC_SEARCH_SECTION_NAME . $path,
ScopeInterface::SCOPE_STORE,
$storeId
);
}
/**
* @param string $path
* @param int $storeId
* @return mixed
*/
public function getGeneralConfig($path, $storeId = null)
{
return $this->scopeConfig->getValue(
$path,
ScopeInterface::SCOPE_STORE,
$storeId
);
}
/**
* @param $path
* @return bool
*/
public function getDebugConfig($path)
{
return $this->scopeConfig->getValue(
self::ELASTIC_SEARCH_SECTION_NAME . self::DEBUG_SECTION_NAME . $path
);
}
/**
* @return string
*/
public function getEngine()
{
return (string)$this->getGeneralConfig(self::CONFIG_ENGINE_PATH);
}
/**
* @return bool
*/
public function isElasticEngine()
{
return $this->getEngine() == self::ELASTIC_SEARCH_ENGINE;
}
/**
* @param array $testData
* @return array
*/
public function prepareConnectionData($testData = [])
{
$defaultData = [
'hostname' => $this->getModuleConfig('connection/server_hostname') ?: 'localhost',
'port' => $this->getModuleConfig('connection/server_port') ?: '9200',
'index' => $this->getModuleConfig('connection/index_prefix') ?: 'magento2',
'enableAuth' => $this->getModuleConfig('connection/enable_auth') ?: 0,
'username' => $this->getModuleConfig('connection/username'),
'password' => $this->getModuleConfig('connection/password'),
'timeout' => $this->getModuleConfig('connection/server_timeout') ? : self::ELASTIC_SEARCH_DEFAULT_TIMEOUT,
];
$data = array_merge($defaultData, $testData);
return $data;
}
/**
* @param string $indexType
* @param string|int $storeId
* @return string
*/
public function getIndexName($indexType, $storeId)
{
if ($indexType == 'catalogsearch_fulltext') {
$indexType = 'product';
}
return $this->getModuleConfig('connection/index_prefix') . '_' . $indexType . '_' . $storeId;
}
/**
* @return mixed
*/
public function getIndexPrefix()
{
return $this->getModuleConfig('connection/index_prefix');
}
/**
* get ElasticSearch entity type
*
* @return string
*/
public function getEntityType()
{
return self::ELASTIC_SEARCH_TYPE_DOCUMENT;
}
/**
* @param int $storeId
* @return bool
*/
public function allowSpecialChars($storeId = null)
{
return (bool)$this->getModuleConfig('catalog/long_tail')
&& !$this->useCustomAnalyzer($storeId);
}
/**
* @param null $storeId
* @return bool
*/
public function hasStemming($storeId = null)
{
return $this->getUsePredefinedStemming($storeId);
}
/**
* @param int $storeId
* @return bool
*/
public function getUsePredefinedStopwords($storeId = null)
{
return (bool)$this->getModuleConfig('index/use_language_stopwords', $storeId);
}
/**
* @param int $storeId
* @return string
*/
public function getStopWordsLanguage($storeId = null)
{
return $this->getModuleConfig('index/stopwords_language', $storeId);
}
/**
* @param int $storeId
* @return bool
*/
public function useCustomAnalyzer($storeId = null)
{
return $this->getModuleConfig('connection/custom_analyzer', $storeId) != CustomAnalyzer::DISABLED;
}
/**
* @param int $storeId
* @return bool
*/
public function getUsePredefinedStemming($storeId = null)
{
$analyzerType = $this->getAnalyzerType($storeId);
return ($analyzerType == CustomAnalyzer::DISABLED || $analyzerType == CustomAnalyzer::JAPANESE)
&& (bool)$this->getModuleConfig('index/use_language_stemmed_words', $storeId);
}
/**
* @param int $storeId
* @return string
*/
public function getStemmedLanguage($storeId = null)
{
return $this->getModuleConfig('index/stemmed_words_language', $storeId);
}
/**
* @param int $storeId
* @return bool
*/
public function getAnalyzerType($storeId = null)
{
return $this->getModuleConfig('connection/custom_analyzer', $storeId);
}
/**
* @param int $storeId
* @return bool
*/
public function getUseNoriReadingForm($storeId)
{
return $this->getModuleConfig('index/use_nori_reading_form', $storeId);
}
/**
* @param int $storeId
* @return string
*/
public function getNoriTokenMode($storeId)
{
return $this->getModuleConfig('index/nori_token_mode', $storeId);
}
/**
* @param int $storeId
* @return string
*/
public function getKuromojiReadingForm($storeId)
{
return $this->getModuleConfig('index/kuromoji_reading_form', $storeId);
}
/**
* @param int $storeId
* @return string
*/
public function getKoromojiTokenMode($storeId)
{
return $this->getModuleConfig('index/kuromoji_token_mode', $storeId);
}
/**
* @param int $storeId
* @return array
*/
public function getCharMappings(int $storeId): array
{
$mappings = $this->getModuleConfig('index/char_mapping', $storeId);
if ($mappings) {
$mappings = str_replace("\r\n", "\n", $mappings);
$mappings = str_replace("\r", "\n", $mappings);
$mappings = preg_replace("/\n{2,}/", "\n", $mappings);
return explode("\n", trim($mappings));
}
return [];
}
public function isFurtherQueryAllowed(): bool
{
return (bool) $this->getModuleConfig(self::ALLOW_FURTHER_QUERY_PATH);
}
}
@@ -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\Config\Backend;
class Debug extends \Magento\Framework\App\Config\Value
{
public const LIFE_TIME = 300;
/**
* @return $this|\Magento\Framework\App\Config\Value
*/
public function afterLoad()
{
if ($this->getValue()) {
$this->setValue($this->isActive($this->getValue()));
}
return $this;
}
/**
* @param float $value
* @return bool
*/
public function isActive($value)
{
return (microtime(true) - $value) < self::LIFE_TIME;
}
/**
* @return $this|\Magento\Framework\App\Config\Value
*/
public function beforeSave()
{
if ($this->getValue()) {
$this->setValue(microtime(true));
}
return $this;
}
}
@@ -0,0 +1,220 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Config;
class QuerySettings extends \Magento\Framework\App\Config\Value
{
/**
* Keys for array configuration usage
*/
public const ATTRIBUTE = 'attribute';
public const WILDCARD = 'wildcard';
public const SPELLING = 'spelling';
public const COMBINING = 'combining';
/**
* Keys for DB storage
*/
public const KEY_WILDCARD = 'w';
public const KEY_SPELLING = 's';
public const KEY_COMBINING = 'c';
/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $serializer;
/**
* @var \Amasty\ElasticSearch\Model\Config\QuerySettingsProcessor
*/
private $processor;
/**
* @inheritdoc
*/
protected function _construct()
{
parent::_construct();
$this->serializer = $this->getData('serializer');
$this->processor = $this->getData('processor');
}
/**
* @inheritdoc
*/
protected function _afterLoad()
{
$value = $this->getValue();
$value = $this->processor->parseLoad($value);
$value = $this->encodeArrayFieldValue($value);
$this->setValue($value);
}
/**
* @inheritdoc
*/
public function beforeSave()
{
$value = $this->getValue();
$value = $this->makeStorableArrayFieldValue($value);
if ($value) {
$this->setValue($value);
} else {
$this->_dataSaveAllowed = false;
}
return $this;
}
/**
* Make value ready for store
*
* @param string|array $value
* @return string
*/
private function makeStorableArrayFieldValue($value)
{
if (!is_array($value)) {
$value = $this->unserializeValue($value);
}
$value = $this->decodeArrayFieldValue($value);
$value = $this->serializeValue($value);
return $value;
}
/**
* Prepare configuration array of all searchable attributes for frontend
*
* @param array $value
* @return array
*/
private function encodeArrayFieldValue(array $value)
{
$result = [];
$attributeCodes = $this->processor->getSearchableAttributeCodes();
foreach ($value as $attributeCode => $data) {
if (in_array($attributeCode, $attributeCodes, true)) {
$result[$attributeCode] = [
self::ATTRIBUTE => $attributeCode,
self::WILDCARD => $data[self::WILDCARD],
self::SPELLING => $data[self::SPELLING],
self::COMBINING => $data[self::COMBINING]
];
}
}
foreach ($attributeCodes as $code) {
if (!isset($result[$code])) {
$result[$code] = [
self::ATTRIBUTE => $code,
self::WILDCARD => 0,
self::SPELLING => 0,
self::COMBINING => 0
];
}
}
return $result;
}
/**
* Create a value from a storable representation
*
* @param int|float|string $value
* @return array
*/
private function unserializeValue($value)
{
$result = [];
if (is_string($value) && !empty($value)) {
$result = $this->serializer->unserialize($value);
}
return $result;
}
/**
* Decode value from used in \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
*
* @param array $value
* @return array
*/
private function decodeArrayFieldValue(array $value)
{
unset($value['__empty']);
foreach ($value as $attribute => $data) {
if (!is_array($data) || !array_key_exists(self::COMBINING, $data)) {
unset($value[$attribute]);
}
}
return $value;
}
/**
* Generate a storable representation of a value
*
* @param int|float|string|array $value
* @return string
*/
private function serializeValue($value): string
{
if (is_array($value) && !empty($value)) {
$result = [];
foreach ($value as $attributeCode => $data) {
$wildcard = 0;
if (isset($data[self::WILDCARD])) {
$wildcard = $this->prepareVariable($data[self::WILDCARD]);
}
$spelling = 0;
if (isset($data[self::SPELLING])) {
$spelling = $this->prepareVariable($data[self::SPELLING]);
}
$combing = (int)($data[self::COMBINING] ?? 0);
if ($wildcard || $spelling || $combing) {
$result[$attributeCode] = [
self::KEY_WILDCARD => $wildcard,
self::KEY_SPELLING => $spelling,
self::KEY_COMBINING => $combing
];
}
}
return $this->serializer->serialize($result);
}
return '';
}
/**
* @param string $data
*/
private function prepareVariable($data): int
{
if (strcasecmp($data, 'on') === 0) {
return 1;
}
return (int) $data;
}
/**
* @deprecated moved to separate class
* @see \Amasty\ElasticSearch\Model\Config\QuerySettingsProcessor::getConfigValue
*
* @param string $attributeCode
* @return array|null
*/
public function getConfigValue($attributeCode)
{
return $this->processor->getConfigValue($attributeCode);
}
}
@@ -0,0 +1,138 @@
<?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\Config;
use Amasty\ElasticSearch\Model\Config as ElasticConfig;
use Amasty\ElasticSearch\Model\Source\FulltextAttributes;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Store\Model\ScopeInterface;
class QuerySettingsProcessor
{
/**
* @var int[]|string[]
*/
private $searchableAttributes;
/**
* @var array
*/
private $value;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var FulltextAttributes
*/
private $fulltextAttributes;
/**
* @var Json
*/
private $serializer;
public function __construct(
ScopeConfigInterface $scopeConfig,
FulltextAttributes $fulltextAttributes,
Json $serializer
) {
$this->scopeConfig = $scopeConfig;
$this->fulltextAttributes = $fulltextAttributes;
$this->serializer = $serializer;
}
/**
* @return array|null array(
* QuerySettings::WILDCARD => 1|0,
* QuerySettings::SPELLING => 1|0,
* QuerySettings::COMBINING => 1|0
* )
*/
public function getConfigValue(string $attributeCode): ?array
{
if (!in_array($attributeCode, $this->getSearchableAttributeCodes(), true)) {
return null;
}
return $this->getValue()[$attributeCode] ?? [
QuerySettings::WILDCARD => 0,
QuerySettings::SPELLING => 0,
QuerySettings::COMBINING => 0
];
}
public function getSearchableAttributeCodes(): array
{
if ($this->searchableAttributes === null) {
$this->searchableAttributes = array_keys($this->fulltextAttributes->toArray());
}
return $this->searchableAttributes;
}
private function getValue(): array
{
if ($this->value === null) {
$value = $this->scopeConfig->getValue(
ElasticConfig::ELASTIC_SEARCH_ENGINE . '/catalog/query_settings',
ScopeInterface::SCOPE_STORE
);
$this->value = $this->parseLoad($value);
}
return $this->value;
}
public function parseLoad($value)
{
$value = $this->unserializeValue($value);
return $this->convertConfigurations($value);
}
private function unserializeValue($value)
{
$result = [];
if (is_string($value) && !empty($value)) {
$result = $this->serializer->unserialize($value);
}
return $result;
}
/**
* Convert configuration from storable format to usable
*
* Older version of the module didn't have uniq storable format.
* Method have resolver for that
*/
private function convertConfigurations(array $configurations): array
{
$attributeCodes = $this->getSearchableAttributeCodes();
$result = [];
foreach ($configurations as $code => $row) {
if (in_array($code, $attributeCodes, true)) {
$result[$code] = [
QuerySettings::WILDCARD => $row[QuerySettings::KEY_WILDCARD] ?? $row[QuerySettings::WILDCARD] ?? 0,
QuerySettings::SPELLING => $row[QuerySettings::KEY_SPELLING] ?? $row[QuerySettings::SPELLING] ?? 0,
QuerySettings::COMBINING => $row[QuerySettings::KEY_COMBINING] ??
$row[QuerySettings::COMBINING] ?? 0,
];
}
}
return $result;
}
}
+34
View File
@@ -0,0 +1,34 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model;
use Amasty\Base\Debug\Log;
class Debug
{
public const LOG_FILE_NAME = 'amasty_elastic_search.log';
/**
* @param $variable
* @param bool $showBacktrace
* @return $this
*/
public function debug($variable, $showBacktrace = false)
{
//for local debugging set return true for Amasty/Base/Debug/VarDump.php : isAllowed()
if (class_exists(Log::class)) {
Log::setLogFile(self::LOG_FILE_NAME);
Log::execute($variable);
if ($showBacktrace) {
Log::backtrace();
}
}
return $this;
}
}
+157
View File
@@ -0,0 +1,157 @@
<?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\Di;
use Magento\Framework\Module\Manager;
use Magento\Framework\ObjectManager\ConfigInterface as ObjectManagerMetaProvider;
use Magento\Framework\ObjectManagerInterface;
class Wrapper
{
/**
* @var ObjectManagerInterface
*/
private $objectManagerInterface;
/**
* @var string
*/
private $name;
/**
* @var bool
*/
private $isShared;
/**
* @var bool
*/
private $isProxy;
/**
* @var object
*/
private $subject;
/**
* @var Manager
*/
private $moduleManager;
/**
* @var ObjectManagerMetaProvider
*/
private $diMetaProvider;
/**
* @var string[]
*/
private $enabledModules = [
'Magento_Framework'
];
public function __construct(
ObjectManagerInterface $objectManagerInterface,
Manager $moduleManager,
ObjectManagerMetaProvider $diMetaProvider,
$name = '',
$isShared = false,
$isProxy = false
) {
$this->objectManagerInterface = $objectManagerInterface;
$this->moduleManager = $moduleManager;
$this->diMetaProvider = $diMetaProvider;
$this->name = $name;
$this->isShared = $isShared;
$this->isProxy = $isProxy;
}
/**
* @param string $name
* @param array arguments
* @return bool|mixed
*/
public function __call($name, $arguments)
{
$result = false;
if ($this->canCreateObject()) {
$object = $this->getSubject();
//phpcs:ignore
$result = call_user_func_array([$object, $name], $arguments);
}
return $result;
}
/**
* @return object
*/
public function getSubject()
{
if ($this->isProxy && $this->subject) {
return $this->subject;
}
if ($this->isShared) {
$object = $this->objectManagerInterface->get($this->name);
} else {
$object = $this->objectManagerInterface->create($this->name);
}
if ($this->isProxy) {
$this->subject = $object;
}
return $object;
}
private function isVirtualType(string $class): bool
{
$type = $this->diMetaProvider->getInstanceType($class);
return $type !== $class;
}
public function canCreateObject(): bool
{
$canAutoload = (class_exists($this->name) || interface_exists($this->name))
&& $this->isModuleEnabled($this->getModuleName($this->name));
$canGetObjectByDI = $this->isVirtualType($this->name);
return $this->name && ($canAutoload || $canGetObjectByDI);
}
private function isModuleEnabled(string $moduleName): bool
{
return in_array($moduleName, $this->enabledModules, true)
|| $this->moduleManager->isEnabled($moduleName);
}
private function getModuleName(string $class): string
{
$class = ltrim($class, '\\');
$parts = preg_split('@[\\\_]@', $class);
$parts = array_filter($parts);
if (count($parts) < 2) {
throw new \InvalidArgumentException(
(string)__('Provided argument is not in PSR-0 or underscore notation.')
);
}
return sprintf(
'%1s_%2s',
ucfirst($parts[0]),
ucfirst($parts[1])
);
}
}
@@ -0,0 +1,32 @@
<?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;
class GetAmastySearchEngines
{
/**
* @var string[]
*/
private $amastySearchEngines;
public function __construct(array $amastySearchEngines = [])
{
$this->amastySearchEngines = array_values($amastySearchEngines);
}
/**
* @return string[]
*/
public function execute(): array
{
return $this->amastySearchEngines;
}
}
@@ -0,0 +1,36 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model;
class GetNonTextAttributes
{
/**
* @var array
*/
private $nonTextAttributes = [
'*',
'price',
'visibility',
'tax_class_id',
'category_ids'
];
public function __construct(
array $additionalAttributes = []
) {
$this->nonTextAttributes = array_merge($this->nonTextAttributes, $additionalAttributes);
}
/**
* @return array
*/
public function execute()
{
return $this->nonTextAttributes;
}
}
@@ -0,0 +1,72 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\Data;
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperInterface;
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperResolverInterface;
class DataMapperResolver implements DataMapperResolverInterface
{
public const DEFAULT_DATA_INDEXER = 'catalogsearch_fulltext';
public const INDEXER_ID = 'indexer_id';
/**
* @since 1.17.1 added mapped data to context
*/
public const MAPPED_DATA_KEY = 'document';
/**
* @var DataMapperInterface[][]
*/
private $mappers = [];
public function __construct(array $dataMappers = [])
{
foreach ($dataMappers as $type => $mappers) {
$this->mappers[$type] = [];
foreach ($mappers as $mapper) {
$this->mappers[$type][] = $mapper;
}
}
}
/**
* @param array $indexData [attribute_id(int) => attribute_value(string|int|string[]|int[])]
* @param int $storeId
* @param array $context
* @return array
*/
public function mapEntityData(array $indexData, $storeId, $context = [])
{
$data = [];
$indexerId = $context[self::INDEXER_ID] ?? self::DEFAULT_DATA_INDEXER;
if (isset($this->mappers[$indexerId]) && is_array($this->mappers[$indexerId])) {
/** @var DataMapperInterface $mapper */
foreach ($this->mappers[$indexerId] as $mapper) {
$context[self::MAPPED_DATA_KEY] = $data;
$mappedData = $mapper->map($indexData, $storeId, $context);
$data = $this->mergeData($mappedData, $data);
}
}
return $data;
}
private function mergeData(array $mappedData, array $data): array
{
/** @var array $entityData */
foreach ($mappedData as $entityId => $entityData) {
if (isset($data[$entityId])) {
$data[$entityId] = $entityData + $data[$entityId];
} else {
$data[$entityId] = $entityData;
}
}
return $data;
}
}
@@ -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\Indexer\Data\External;
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperInterface;
use Amasty\ElasticSearch\Model\Indexer\Structure\EntityBuilder\External\Finder;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
class FinderDataMapper implements DataMapperInterface
{
/**
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute
*/
private $eavAttribute;
/**
* @var Finder
*/
private $externalBuilder;
public function __construct(
\Magento\Eav\Model\ResourceModel\Entity\Attribute $eavAttribute,
Finder $externalBuilder
) {
$this->eavAttribute = $eavAttribute;
$this->externalBuilder = $externalBuilder;
}
/**
* @param array $documentData
* @param int $storeId
* @param array $context
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function map(array $documentData, $storeId, array $context = [])
{
$documents = [];
$externalAttributeIds = [];
foreach (array_keys($this->externalBuilder->getExtraValueFields()) as $attributeCode) {
$externalAttributeIds[$attributeCode . '_value']
= $this->eavAttribute->getIdByCode(ProductAttributeInterface::ENTITY_TYPE_CODE, $attributeCode);
}
foreach ($documentData as $productId => $indexData) {
foreach ($externalAttributeIds as $filterName => $attributeId) {
if (isset($indexData[$attributeId])) {
if (is_array($indexData[$attributeId])) {
$value = isset($indexData[$attributeId][$productId])
? $indexData[$attributeId][$productId] : '';
} else {
$value = $indexData[$attributeId];
}
$documents[$productId][$filterName] = $value;
}
}
}
return $documents;
}
}
@@ -0,0 +1,55 @@
<?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\Data\External;
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperInterface;
use Amasty\ElasticSearch\Api\RelevanceRuleRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
class RelevanceBoostDataMapper implements DataMapperInterface
{
public const DEFAULT_RELEVANCE = 1.;
public const ATTRIBUTE_NAME = 'amasty_product_relevance';
/**
* @var RelevanceRuleRepositoryInterface
*/
private $relevanceRuleRepository;
/**
* @var StoreManagerInterface
*/
private $storeManager;
public function __construct(
RelevanceRuleRepositoryInterface $relevanceRuleRepository,
StoreManagerInterface $storeManager
) {
$this->relevanceRuleRepository = $relevanceRuleRepository;
$this->storeManager = $storeManager;
}
public function map(array $indexData, $storeId, array $context = []): array
{
$websiteId = $this->storeManager->getStore($storeId)->getWebsiteId();
$productIds = array_keys($indexData);
$productBoostData = $this->relevanceRuleRepository->getProductBoostMultipliers($productIds, (int) $websiteId);
$indexBoostData = [];
foreach ($indexData as $productId => $productData) {
$indexBoostData[$productId] = [
self::ATTRIBUTE_NAME => $productBoostData[$productId] ?? self::DEFAULT_RELEVANCE
];
}
return $indexBoostData;
}
}
@@ -0,0 +1,81 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\Data\Product;
class AttributeDataProvider
{
/**
* @var \Magento\Eav\Model\Entity\Attribute[]
*/
private $attributesById;
/**
* @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory
*/
private $productAttributeCollectionFactory;
/**
* @var \Magento\Eav\Model\Config
*/
private $eavConfig;
public function __construct(
\Magento\Eav\Model\Config $eavConfig,
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $prodAttributeCollectionFactory
) {
$this->eavConfig = $eavConfig;
$this->productAttributeCollectionFactory = $prodAttributeCollectionFactory;
}
/**
* @param string $backendType
* @return $this
*/
private function initSearchableAttributes()
{
if ($this->attributesById === null) {
/** @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection $productAttributes */
$productAttributes = $this->productAttributeCollectionFactory->create();
$productAttributes->addToIndexFilter(true);
/** @var \Magento\Eav\Model\Entity\Attribute[] $attributes */
$attributes = $productAttributes->getItems();
$entity = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getEntity();
foreach ($attributes as $attribute) {
$options = [];
if ($attribute->getFrontendInput() === 'select' || $attribute->getFrontendInput() === 'multiselect') {
foreach ($attribute->getOptions() as $option) {
$options[$option->getValue()] = $option->getLabel();
}
}
$attribute->setAttributeOptionsArray($options);
$attribute->setEntity($entity);
$this->attributesById[$attribute->getAttributeId()] = $attribute;
}
}
return $this;
}
/**
* @param int|string $attribute
* @return \Magento\Eav\Model\Entity\Attribute|null
*/
public function getAttribute($attributeId)
{
$this->initSearchableAttributes();
if (isset($this->attributesById[$attributeId])) {
return $this->attributesById[$attributeId];
}
return null;
}
}
@@ -0,0 +1,138 @@
<?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\Data\Product;
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperInterface;
use Amasty\ElasticSearch\Model\Di\Wrapper as DiWrapper;
use Amasty\ElasticSearch\Model\ResourceModel\Product\ConvertProductIdToSku;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
use Magento\Store\Api\StoreRepositoryInterface;
/**
* Provide is_out_of_stock value.
* Need because we have our data mapper, which different with magento data mapper.
*
* @see \Magento\InventoryElasticsearch\Plugin\Model\Adapter\BatchDataMapper\ProductDataMapperPlugin
*/
class OutofStockDataMapper implements DataMapperInterface
{
public const OUT_OF_STOCK_FIELD = 'is_out_of_stock';
/**
* @var StoreRepositoryInterface
*/
private $storeRepository;
/**
* @var DiWrapper|StockByWebsiteIdResolverInterface
*/
private $stockByWebsiteIdResolver;
/**
* @var DiWrapper|GetStockItemDataInterface
*/
private $getStockItemData;
/**
* @var AttributeDataProvider
*/
private $attributeDataProvider;
/**
* @var int|null
*/
private $skuAttributeId;
/**
* @var ConvertProductIdToSku
*/
private $convertProductIdToSku;
public function __construct(
StoreRepositoryInterface $storeRepository,
DiWrapper $stockByWebsiteIdResolver,
DiWrapper $getStockItemData,
AttributeDataProvider $attributeDataProvider,
ConvertProductIdToSku $convertProductIdToSku
) {
$this->storeRepository = $storeRepository;
$this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
$this->getStockItemData = $getStockItemData;
$this->attributeDataProvider = $attributeDataProvider;
$this->convertProductIdToSku = $convertProductIdToSku;
}
/**
* @param array $indexData
* @param int $storeId
* @param array $context
* @return array
*/
public function map(array $indexData, $storeId, array $context = [])
{
$store = $this->storeRepository->getById($storeId);
$stock = $this->stockByWebsiteIdResolver->execute((int) $store->getWebsiteId());
if ($stock === false) {
return [];
}
$outOfStockValues = [];
foreach ($indexData as $productId => $document) {
$sku = $this->getSku($productId, $document);
if (!$sku) {
$isOutOfStock = 1;
} else {
try {
$stockItemData = $this->getStockItemData->execute($sku, $stock->getStockId());
} catch (NoSuchEntityException $e) {
$stockItemData = null;
}
$isOutOfStock = null !== $stockItemData
? (int) $stockItemData[GetStockItemDataInterface::IS_SALABLE] : 1;
}
$outOfStockValues[$productId] = [self::OUT_OF_STOCK_FIELD => $isOutOfStock];
}
return $outOfStockValues;
}
private function getSku($productId, array $document): string
{
if ($this->skuAttributeId === null) {
$this->resolveSkuAttributeId($document);
}
if ($this->skuAttributeId) {
$sku = $document[$this->skuAttributeId] ?? '';
if (is_array($sku)) {
$sku = $sku[$productId] ?? reset($sku);
}
} else {
$sku = $this->convertProductIdToSku->execute((int)$productId);
}
return $sku;
}
private function resolveSkuAttributeId(array $document): void
{
$this->skuAttributeId = 0; // skip next sku attribute id search
foreach ($document as $attributeId => $value) {
$attribute = $this->attributeDataProvider->getAttribute($attributeId);
if ($attribute && $attribute->getAttributeCode() === 'sku') {
$this->skuAttributeId = $attributeId;
break;
}
}
}
}
@@ -0,0 +1,140 @@
<?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\Data\Product\PriceDataMapper;
use Amasty\ElasticSearch\Model\Indexer\Data\Product\ProductPriceDataMapper;
use Magento\Catalog\Model\Indexer\Product\Price\DimensionModeConfiguration;
use Magento\Customer\Model\Indexer\CustomerGroupDimensionProvider;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Indexer\Dimension;
use Magento\Framework\Indexer\DimensionFactory;
use Magento\Framework\Search\Request\IndexScopeResolverInterface;
use Magento\Store\Model\Indexer\WebsiteDimensionProvider;
class PriceResolver
{
/**
* @var IndexScopeResolverInterface
*/
private $priceTableResolver;
/**
* @var DimensionFactory
*/
private $dimensionFactory;
/**
* @var DimensionModeConfiguration
*/
private $dimensionModeConfiguration;
/**
* @var ResourceConnection
*/
private $resource;
public function __construct(
IndexScopeResolverInterface $priceTableResolver,
DimensionFactory $dimensionFactory,
DimensionModeConfiguration $dimensionModeConfiguration,
ResourceConnection $resourceConnection
) {
$this->priceTableResolver = $priceTableResolver;
$this->dimensionFactory = $dimensionFactory;
$this->dimensionModeConfiguration = $dimensionModeConfiguration;
$this->resource = $resourceConnection;
}
public function getProductPriceData(array $productIds, array $customerGroupIds, int $websiteId): array
{
$priceTableNames = $this->resolvePriceTableNames($websiteId, $customerGroupIds);
$result = [];
$connection = $this->resource->getConnection();
foreach ($priceTableNames as $tableName) {
$select = $connection->select()->from(
$this->resource->getTableName($tableName),
['entity_id', 'customer_group_id', 'min_price']
);
$select->where('website_id = ?', $websiteId);
$select->where('entity_id IN (?)', $productIds);
foreach ($connection->fetchAll($select) as $row) {
$result[$row['entity_id']][$row['customer_group_id']] = round(
(float)$row['min_price'],
ProductPriceDataMapper::DEFAULT_PRECISION
);
}
}
return $result;
}
/**
* Collect price index table names
*
* Price index can have dimensions for customer group and website
*
* @param int $websiteId
* @param int[]|string[] $customerGroupIds
* @return string[] DB table names
*/
private function resolvePriceTableNames(int $websiteId, array $customerGroupIds): array
{
$dimensionConfiguration = $this->dimensionModeConfiguration->getDimensionConfiguration();
$isCustomerGroup = false;
$websiteDimension = null;
foreach ($dimensionConfiguration as $dimensionName) {
if ($dimensionName === WebsiteDimensionProvider::DIMENSION_NAME) {
$websiteDimension = $this->createDimensionFromWebsite($websiteId);
}
if ($dimensionName === CustomerGroupDimensionProvider::DIMENSION_NAME) {
$isCustomerGroup = true;
}
}
$priceTableNames = [];
if ($isCustomerGroup) {
foreach ($customerGroupIds as $groupId) {
$dimensions = [];
if ($websiteDimension) {
$dimensions[] = $websiteDimension;
}
$dimensions[] = $this->createDimensionFromCustomerGroup((int)$groupId);
$priceTableNames[] = $this->priceTableResolver->resolve('catalog_product_index_price', $dimensions);
}
} elseif ($websiteDimension) {
$priceTableNames[] = $this->priceTableResolver->resolve('catalog_product_index_price', [$websiteDimension]);
} else {
$priceTableNames[] = 'catalog_product_index_price';
}
return $priceTableNames;
}
private function createDimensionFromWebsite(int $websiteId): Dimension
{
return $this->dimensionFactory->create(
WebsiteDimensionProvider::DIMENSION_NAME,
(string)$websiteId
);
}
private function createDimensionFromCustomerGroup(int $customerGroupId): Dimension
{
return $this->dimensionFactory->create(
CustomerGroupDimensionProvider::DIMENSION_NAME,
(string)$customerGroupId
);
}
}
@@ -0,0 +1,73 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\Data\Product;
use Magento\Framework\App\ResourceConnection;
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperInterface;
class ProductCategoryDataMapper implements DataMapperInterface
{
/**
* @var ResourceConnection
*/
private $resource;
public function __construct(ResourceConnection $resourceConnection)
{
$this->resource = $resourceConnection;
}
/**
* @param array $documentData
* @param int $storeId
* @param array $context
* @return array
*/
public function map(array $documentData, $storeId, array $context = [])
{
$categoryData = $this->getProductCategoryData($storeId, array_keys($documentData));
$categoryDocumentData = [];
foreach ($documentData as $productId => $document) {
$categoryDocumentData[$productId]['category_ids'] = $categoryData[$productId]['category_ids'] ?? [];
if (isset($categoryData[$productId]['position'])) {
foreach ($categoryData[$productId]['position'] as $field => $value) {
$categoryDocumentData[$productId][$field] = $value;
}
}
}
return $categoryDocumentData;
}
/**
* @param int $storeId
* @param array $productIds
* @return array
*/
private function getProductCategoryData($storeId, array $productIds = [])
{
$result = [];
if (!empty($productIds)) {
$connection = $this->resource->getConnection();
$tableName = $this->resource->getTableName('catalog_category_product_index_store' . $storeId);
$tableName = $connection->isTableExists($tableName)
? $tableName : $this->resource->getTableName('catalog_category_product_index');
$select = $connection->select()->from(
[$tableName],
['category_id', 'product_id', 'position']
)->where('product_id IN (?)', $productIds);
foreach ($connection->fetchAll($select) as $row) {
$result[$row['product_id']]['category_ids'][] = $row['category_id'];
$result[$row['product_id']]['position']['category_position_' . $row['category_id']] = $row['position'];
}
}
return $result;
}
}
@@ -0,0 +1,292 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\Data\Product;
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperInterface;
use Amasty\ElasticSearch\Model\ResourceModel\ConfigurableResolver;
class ProductDataMapper implements DataMapperInterface
{
/**
* @var array
*/
private $excludedAttributes = [
'price',
'media_gallery',
'tier_price',
'quantity_and_stock_status',
'media_gallery',
'giftcard_amounts'
];
/**
* @var AttributeDataProvider
*/
private $attributeDataProvider;
/**
* @var string[]
*/
private $attributesExcludedFromMerge = [
'status',
'visibility',
'tax_class_id'
];
/**
* @var ConfigurableResolver
*/
private $configurableResolver;
public function __construct(
AttributeDataProvider $attributeDataProvider,
ConfigurableResolver $configurableResolver,
array $excludedAttributes = []
) {
$this->attributeDataProvider = $attributeDataProvider;
$this->excludedAttributes = array_merge($this->excludedAttributes, $excludedAttributes);
$this->configurableResolver = $configurableResolver;
}
/**
* @param array $documentData
* @param int $storeId
* @param array $context
* @return array
*/
public function map(array $documentData, $storeId, array $context = [])
{
// reset attribute data for new store
$documents = [];
$skuValues = $this->configurableResolver->getRelationSkuValues(array_keys($documentData));
foreach ($documentData as $productId => $indexData) {
$document = ['store_id' => $storeId];
$productIndexData = $this->getProductData($productId, $indexData, $storeId);
$productIndexData = $this->updateSkuValues($productIndexData, $skuValues);
foreach ($productIndexData as $attributeCode => $value) {
if (in_array($attributeCode, $this->excludedAttributes, true)) {
continue;
}
$document[$attributeCode] = $value;
}
$documents[$productId] = $document;
}
return $documents;
}
/**
* @param array $productIndexData
* @param array $skuValues
* @return array
*/
private function updateSkuValues(array $productIndexData, array $skuValues)
{
$id = $productIndexData['entity_id'];
if (isset($skuValues[$id]) && isset($productIndexData['sku'])) {
$productIndexData['sku'] = array_merge([$productIndexData['sku']], explode(',', $skuValues[$id]));
$productIndexData['sku'] = implode(' ', $productIndexData['sku']);
}
return $productIndexData;
}
/**
* @param int $productId
* @param array $indexData
* @param int $storeId
* @return array
*/
private function getProductData($productId, array $indexData, $storeId)
{
$productAttributes = [self::ENTITY_ID_FIELD_NAME => $productId];
foreach ($indexData as $attributeId => $attributeValue) {
$attribute = $this->attributeDataProvider->getAttribute($attributeId);
if (!$attribute) {
continue;
}
// phpcs:ignore
$productAttributes = array_merge(
$productAttributes,
$this->prepareProductData(
$productId,
$attributeValue,
$attribute
)
);
}
return $productAttributes;
}
/**
* @param int $productId
* @param mixed $value
* @param \Magento\Eav\Model\Entity\Attribute $attribute
*
* @return array
*/
public function prepareProductData($productId, $value, \Magento\Eav\Model\Entity\Attribute $attribute)
{
$productAttributes = [];
$attributeCode = $attribute->getAttributeCode();
$attributeFrontendInput = $attribute->getFrontendInput();
$preparedValue = $this->getProductAttributeValue($productId, $attribute, $value);
if (is_array($value) && in_array($attributeFrontendInput, ['select', 'multiselect'], true)) {
$productAttributes = $this->prepareOptionalAttributeValues($attribute, $value, $preparedValue);
} elseif ($preparedValue) {
$productAttributes[$attributeCode] = $preparedValue;
if (!is_array($preparedValue)) {
$attributeOptions = $this->getAttributeOptions($attribute);
if (isset($attributeOptions[$preparedValue])) {
$productAttributes[$attributeCode . '_value'] = $attributeOptions[$preparedValue];
}
if ($attributeFrontendInput === 'date'
|| in_array($attribute->getBackendType(), ['datetime', 'timestamp'], true)
) {
if (preg_replace('#[ 0:-]#', '', $preparedValue) !== '') {
$dateObj = new \DateTime($preparedValue, new \DateTimeZone('UTC'));
$productAttributes[$attributeCode] = $dateObj->format('c');
}
}
}
}
return $productAttributes;
}
/**
* @param int $productId
* @param \Magento\Eav\Model\Entity\Attribute $attribute
* @param mixed $attributeValue
* @return array|string|int
*/
private function getProductAttributeValue($productId, $attribute, $attributeValue)
{
if (is_array($attributeValue)) {
if (isset($attributeValue[$productId])
&& (!$attribute->getIsSearchable() || $this->isAttributeExcludedFromMerge($attribute))
) {
if (in_array($attribute->getFrontendInput(), ['text', 'textarea'], true)) {
return $attributeValue[$productId];
}
$explodedValue = explode(',', (string) $attributeValue[$productId]);
return count($explodedValue) > 1 ? $explodedValue : $attributeValue[$productId];
}
return $this->prepareAttributeValues($attributeValue);
}
return $attributeValue;
}
/**
* Fix for compatibility with our Grouped Options module, due to which an attribute
* option can have several values that come through ',' and if they are also written to the
* elastic in this form, then the search for them will not work
*/
private function prepareAttributeValues(array $values): array
{
$values = array_values(array_unique($values));
foreach ($values as $key => $value) {
$attrCodeValues = explode(',', (string) $value);
unset($values[$key]);
foreach ($attrCodeValues as $attrCodeValue) {
$values[] = $attrCodeValue;
}
}
return array_values(array_unique($values));
}
public function isAttributeExcludedFromMerge(\Magento\Eav\Model\Entity\Attribute $attribute)
{
$result = false;
if (in_array($attribute->getAttributeCode(), $this->attributesExcludedFromMerge, true)
|| in_array($attribute->getFrontendInput(), ['text', 'textarea'], true)) {
$result = true;
}
return $result;
}
/**
* @param \Magento\Eav\Model\Entity\Attribute $attribute
* @param mixed $value
* @param mixed $preparedValue
* @return array
*/
private function prepareOptionalAttributeValues(
\Magento\Eav\Model\Entity\Attribute $attribute,
$value,
$preparedValue = null
) {
/**
* @TODO add prices mapper
*/
$productAttributes = [];
$attributeCode = $attribute->getAttributeCode();
$attributeFrontendInput = $attribute->getFrontendInput();
$attributeOptions = $this->getAttributeOptions($attribute);
$textAttributeValue = [];
if ($attributeFrontendInput == 'select') {
$productAttributes[$attributeCode] = $preparedValue;
if ($attribute->getIsSearchable()) {
foreach ($value as $optionIds) {
$optionIds = explode(',', $optionIds);
foreach ($optionIds as $optionId) {
if (isset($attributeOptions[$optionId])) {
$textAttributeValue[] = $attributeOptions[$optionId];
}
}
}
}
} elseif ($attributeFrontendInput == 'multiselect') {
$preparedValue = [];
foreach ($value as $valueByAttribute) {
$valueByAttribute = explode(',', $valueByAttribute);
foreach ($valueByAttribute as $optionIds) {
$optionIds = explode(',', $optionIds);
foreach ($optionIds as $optionId) {
if (isset($attributeOptions[$optionId]) && $attribute->getIsSearchable()) {
$preparedValue[] = $optionId;
$textAttributeValue[] = $attributeOptions[$optionId];
} elseif (isset($attributeOptions[$optionId])) {
$preparedValue[] = $optionId;
}
}
}
}
$productAttributes[$attributeCode] = array_values(array_unique($preparedValue));
}
if (!empty($textAttributeValue)) {
$productAttributes[$attributeCode . '_value'] = array_values(array_unique($textAttributeValue));
}
return $productAttributes;
}
/**
* @param \Magento\Eav\Model\Entity\Attribute $attribute
* @return array
*/
public function getAttributeOptions(\Magento\Eav\Model\Entity\Attribute $attribute)
{
return $attribute->getAttributeOptionsArray();
}
}
@@ -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\Data\Product;
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperInterface;
use Amasty\ElasticSearch\Model\Indexer\Data\Product\PriceDataMapper\PriceResolver;
use DateTime;
use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Stdlib\DateTime as StdlibDateTime;
use Magento\Store\Model\StoreManager;
/**
* Map minimum product price in store with considering customer group.
*
* Indexed price data can be used in product sorting by price.
*/
class ProductPriceDataMapper implements DataMapperInterface
{
public const DEFAULT_PRECISION = 4;
/**
* @var null|int[]|string[]
*/
private $customerGroupIds = null;
/**
* @var StdlibDateTime
*/
protected $date;
/**
* @var ResourceConnection
*/
private $resource;
/**
* @var StoreManager
*/
private $storeManager;
/**
* @var CollectionFactory
*/
private $groupCollectionFactory;
/**
* @var PriceDataMapper\PriceResolver
*/
private $priceResolver;
public function __construct(
ResourceConnection $resourceConnection,
CollectionFactory $customerGroupCollectionFactory,
StoreManager $storeManager,
StdlibDateTime $date,
PriceResolver $priceResolver
) {
$this->resource = $resourceConnection;
$this->groupCollectionFactory = $customerGroupCollectionFactory;
$this->storeManager = $storeManager;
$this->date = $date;
$this->priceResolver = $priceResolver;
}
/**
* @param array $documentData
* @param int $storeId
* @param array $context
* @return array
*/
public function map(array $documentData, $storeId, array $context = [])
{
$productIds = array_keys($documentData);
$prices = $this->getProductPriceData($productIds, (int)$storeId);
$priceDocumentData = [];
foreach ($productIds as $productId) {
foreach ($this->getCustomerGroupIds() as $customerGroupId) {
$priceValue = $prices[$productId][$customerGroupId] ?? 0;
$priceDocumentData[$productId]['price_' . $customerGroupId] = $priceValue;
}
}
return $priceDocumentData;
}
private function getCustomerGroupIds(): array
{
if ($this->customerGroupIds === null) {
$this->customerGroupIds = $this->groupCollectionFactory->create()->getAllIds();
}
return $this->customerGroupIds;
}
private function getProductPriceData(array $productIds = [], int $storeId = null): array
{
$result = [];
if (!empty($productIds)) {
$websiteId = $this->storeManager->getStore($storeId)->getWebsiteId();
$result = $this->priceResolver->getProductPriceData($productIds, $this->getCustomerGroupIds(), $websiteId);
$connection = $this->resource->getConnection();
$select = $connection->select()->from(
$this->resource->getTableName('catalogrule_product_price'),
['product_id', 'customer_group_id', 'rule_price']
);
$now = new DateTime();
$select->where('website_id = ?', $websiteId)
->where('rule_date = ?', $this->date->formatDate($now, false));
$select->where('product_id IN (?)', $productIds);
foreach ($connection->fetchAll($select) as $row) {
$rulePrice = round($row['rule_price'], self::DEFAULT_PRECISION);
$productId = $row['product_id'];
$customerGroupId = $row['customer_group_id'];
if (isset($result[$productId][$customerGroupId])
&& $result[$productId][$customerGroupId] > $rulePrice
) {
$result[$productId][$customerGroupId] = $rulePrice;
}
}
}
return $result;
}
}
+149
View File
@@ -0,0 +1,149 @@
<?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;
use Amasty\ElasticSearch\Model\Adapter\AdapterInterface;
use Magento\Framework\App\ScopeResolverInterface;
use Magento\Framework\Indexer\IndexStructureInterface;
use Magento\Framework\Indexer\SaveHandler\Batch;
use Magento\Framework\Indexer\SaveHandler\IndexerInterface;
use Magento\Framework\Search\Request\Dimension;
class DataHandler implements IndexerInterface
{
/**
* Default batch size
*/
public const BATCH_SIZE = 1000;
/**
* @var IndexStructureInterface
*/
private $indexStructure;
/**
* @var ScopeResolverInterface
*/
private $scopeResolver;
/**
* @var AdapterInterface
*/
private $adapter;
/**
* @var Batch
*/
private $batch;
/**
* @var array
*/
private $data;
/**
* @var int
*/
private $batchSize;
public function __construct(
IndexStructureInterface $indexStructure,
ScopeResolverInterface $scopeResolver,
AdapterInterface $adapter,
Batch $batch,
array $data = [],
$batchSize = self::BATCH_SIZE
) {
$this->indexStructure = $indexStructure;
$this->scopeResolver = $scopeResolver;
$this->adapter = $adapter;
$this->batch = $batch;
$this->data = $data;
$this->batchSize = $batchSize;
}
/**
* Add entities data to index
*
* @param Dimension[] $dimensions
* @param \Traversable $documents
* @return IndexerInterface
*/
public function saveIndex($dimensions, \Traversable $documents)
{
$scopeId = $this->resolveScopeId($dimensions);
foreach ($this->batch->getItems($documents, $this->batchSize) as $documents) {
$documents = $this->adapter->prepareDocuments($documents, $scopeId, $this->getIndexerId());
$this->adapter->saveDocuments($documents, $scopeId, $this->getIndexerId());
}
$this->adapter->updateAlias($scopeId, $this->getIndexerId());
return $this;
}
/**
* Remove entities data from index
*
* @param Dimension[] $dimensions
* @param \Traversable $documents
* @return IndexerInterface
*/
public function deleteIndex($dimensions, \Traversable $documents)
{
$scopeId = $this->resolveScopeId($dimensions);
$documentIds = [];
foreach ($documents as $document) {
$documentIds[$document] = $document;
}
$this->adapter->deleteDocuments($documentIds, $scopeId, $this->getIndexerId());
return $this;
}
/**
* Remove all data from index
*
* @param Dimension[] $dimensions
* @return IndexerInterface
*/
public function cleanIndex($dimensions)
{
$this->indexStructure->delete($this->getIndexerId(), $dimensions);
$this->indexStructure->create($this->getIndexerId(), [], $dimensions);
return $this;
}
/**
* Define if engine is available
*
* @param Dimension[] $dimensions
* @return bool
*/
public function isAvailable($dimensions = [])
{
return $this->adapter->ping();
}
private function getIndexerId(): string
{
return (string)$this->data['indexer_id'];
}
/**
* @param Dimension[] $dimensions
*/
private function resolveScopeId(array $dimensions): int
{
$dimension = current($dimensions);
return (int)$this->scopeResolver->getScope($dimension->getValue())->getId();
}
}
@@ -0,0 +1,135 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\RelevanceRule;
use Magento\Framework\Mview\ActionInterface as MviewActionInterface;
use Magento\Framework\Indexer\ActionInterface as IndexerActionInterface;
use Magento\Framework\DataObject\IdentityInterface;
abstract class AbstractIndexer implements IndexerActionInterface, MviewActionInterface, IdentityInterface
{
/**
* @var IndexBuilder
*/
private $indexBuilder;
/**
* @var \Magento\Framework\Event\ManagerInterface
*/
private $eventManager;
/**
* @var \Magento\Framework\App\CacheInterface
*/
private $cacheManager;
/**
* @var \Magento\Framework\Indexer\CacheContext
*/
private $cacheContext;
public function __construct(
IndexBuilder $indexBuilder,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Framework\App\CacheInterface $cacheManager,
\Magento\Framework\Indexer\CacheContext $cacheContext
) {
$this->indexBuilder = $indexBuilder;
$this->eventManager = $eventManager;
$this->cacheManager = $cacheManager;
$this->cacheContext = $cacheContext;
}
/**
* @param int[] $ids
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute($ids)
{
$this->executeList($ids);
}
/**
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function executeFull()
{
$this->indexBuilder->reindexFull();
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this]);
//was needed before MAGETWO-50668 was fixed
$this->cacheManager->clean($this->getIdentities());
}
/**
* @param int $productId
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function executeRow($id)
{
if (!$id) {
throw new \Magento\Framework\Exception\LocalizedException(
__('We can\'t rebuild the index for an undefined entity.')
);
}
$this->doExecuteRow($id);
}
/**
* @param int[] $ids
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function executeList(array $ids)
{
if (!$ids) {
throw new \Magento\Framework\Exception\LocalizedException(
__('Could not rebuild index for empty products array')
);
}
$this->doExecuteList($ids);
}
/**
* @return array
*/
public function getIdentities()
{
return [
\Magento\Catalog\Model\Category::CACHE_TAG,
\Magento\Catalog\Model\Product::CACHE_TAG,
\Magento\Framework\App\Cache\Type\Block::CACHE_TAG
];
}
/**
* @param int $id
*/
abstract protected function doExecuteRow($id);
/**
* @param int[] $ids
*/
abstract protected function doExecuteList($ids);
/**
* @return IndexBuilder
*/
protected function getIndexBuilder()
{
return $this->indexBuilder;
}
/**
* @return \Magento\Framework\Indexer\CacheContext
*/
protected function getCacheContext()
{
return $this->cacheContext;
}
}
@@ -0,0 +1,265 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\RelevanceRule;
use Amasty\ElasticSearch\Api\Data\RelevanceRuleInterface;
use Amasty\ElasticSearch\Api\RelevanceRuleRepositoryInterface;
use Amasty\ElasticSearch\Model\ResourceModel\RelevanceRule\CollectionFactory as RelevanceRuleCollectionFactory;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class IndexBuilder
{
public const SECONDS_IN_DAY = 86400;
public const PRODUCT_ID = 'product_id';
public const TABLE_NAME = 'amasty_elastic_relevance_rule_index_tmp';
public const MAX_INT_MYSQL = 4294967294;
/**
* @var ResourceConnection
*/
private $resource;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var RelevanceRuleRepositoryInterface
*/
private $ruleRepository;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var int
*/
private $batchCount;
/**
* @var RelevanceRuleCollectionFactory
*/
private $relevanceRuleCollectionFactory;
public function __construct(
ResourceConnection $resource,
StoreManagerInterface $storeManager,
RelevanceRuleRepositoryInterface $ruleRepository,
ProductRepositoryInterface $productRepository,
SearchCriteriaBuilder $searchCriteriaBuilder,
LoggerInterface $logger,
RelevanceRuleCollectionFactory $relevanceRuleCollectionFactory,
$batchCount = 1000
) {
$this->resource = $resource;
$this->storeManager = $storeManager;
$this->ruleRepository = $ruleRepository;
$this->productRepository = $productRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->logger = $logger;
$this->batchCount = $batchCount;
$this->relevanceRuleCollectionFactory = $relevanceRuleCollectionFactory;
}
/**
* Reindex by product Ids
*
* @param array $ids
* @throws LocalizedException
*/
public function reindexByIds(array $ids): void
{
try {
$this->cleanByIds($ids);
$products = $this->getProducts($ids);
foreach ($this->getActiveRules() as $rule) {
foreach ($products as $product) {
$this->applyRule($rule, $product);
}
}
} catch (\Exception $e) {
$this->critical($e);
throw new LocalizedException(
__("Amasty ElasticSearch Relevance rule indexing failed. See details in exception log.")
);
}
}
public function getProducts(array $productIds): iterable
{
$this->searchCriteriaBuilder->addFilter('entity_id', $productIds, 'in');
$searchCriteria = $this->searchCriteriaBuilder->create();
return $this->productRepository->getList($searchCriteria)->getItems();
}
public function reindexFull(): void
{
try {
$this->resource->getConnection()->truncateTable($this->getIndexTable());
foreach ($this->getActiveRules() as $rule) {
$this->doReindex($rule);
}
} catch (\Exception $e) {
$this->critical($e);
throw new LocalizedException(
__("Relevance rule indexing failed. See details in exception log.")
);
}
}
public function reindexByRuleIds(array $ids): void
{
$table = $this->getIndexTable();
$relevanceRuleCollection = $this->relevanceRuleCollectionFactory->create();
$relevanceRuleCollection->addFieldToFilter(RelevanceRuleInterface::RULE_ID, $ids);
$connection = $this->resource->getConnection();
$connection->delete(
$table,
[
$connection->prepareSqlCondition(RelevanceRuleInterface::RULE_ID, ['in' => $ids])
]
);
try {
foreach ($relevanceRuleCollection as $relevanceRule) {
try {
$this->doReindex($relevanceRule);
} catch (NoSuchEntityException $e) {
null;// do nothing
}
}
} catch (\Exception $e) {
$this->critical($e);
throw new LocalizedException(
__("Relevance rule indexing failed. See details in exception log.")
);
}
}
private function doReindex(RelevanceRuleInterface $rule): void
{
if ($rule->isConditionEmpty()) {
return;
}
$rows = [];
$size = 0;
$productIds = $rule->getCatalogRule()->getMatchingProductIds();
foreach ($productIds as $productId => $validationByWebsite) {
if (empty($validationByWebsite[$rule->getWebsiteId()])) {
continue;
}
$rows[] = $this->generateIndexData($rule, $productId);
$size++;
if ($size == $this->batchCount) {
$this->resource->getConnection()->insertMultiple($this->getIndexTable(), $rows);
$rows = [];
$size = 0;
}
}
if (!empty($rows)) {
$this->resource->getConnection()->insertMultiple($this->getIndexTable(), $rows);
}
}
private function applyRule(RelevanceRuleInterface $rule, ProductInterface $product): IndexBuilder
{
$table = $this->getIndexTable();
$connection = $this->resource->getConnection();
$connection->delete(
$table,
[
$connection->prepareSqlCondition(RelevanceRuleInterface::RULE_ID, ['eq' => $rule->getId()]),
$connection->prepareSqlCondition(self::PRODUCT_ID, ['eq' => $product->getId()])
]
);
if ($rule->isConditionEmpty() || !$rule->getCatalogRule()->validate($product)) {
return $this;
}
try {
$rows = [$this->generateIndexData($rule, $product->getId())];
$this->resource->getConnection()->insertMultiple($table, $rows);
} catch (\Exception $e) {
throw $e;
}
return $this;
}
private function generateIndexData(RelevanceRuleInterface $rule, int $productId): array
{
return [
RelevanceRuleInterface::RULE_ID => $rule->getId(),
RelevanceRuleInterface::WEBSITE_ID => $rule->getWebsiteId(),
RelevanceRuleInterface::MULTIPLIER => $rule->getMultiplier(),
self::PRODUCT_ID => $productId
];
}
private function cleanByIds(array $productIds): void
{
$select = $this->resource->getConnection()
->select()
->from($this->getIndexTable(), self::PRODUCT_ID)
->distinct()
->where(self::PRODUCT_ID . ' IN (?)', $productIds);
$query = $this->resource->getConnection()->deleteFromSelect($select, $this->getIndexTable());
$this->resource->getConnection()->query($query);
}
private function getIndexTable(): string
{
return $this->resource->getTableName(self::TABLE_NAME);
}
private function getActiveRules(): iterable
{
return $this->ruleRepository->getActiveRules();
}
private function critical(\Exception $exception): void
{
$this->logger->critical($exception);
}
}
@@ -0,0 +1,28 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\RelevanceRule;
class ProductRuleIndexer extends AbstractIndexer
{
/**
* @inheritdoc
*/
protected function doExecuteRow($id)
{
$this->getIndexBuilder()->reindexByIds([$id]);
}
/**
* @inheritdoc
*/
protected function doExecuteList($ids)
{
$this->getIndexBuilder()->reindexByIds(array_unique($ids));
$this->getCacheContext()->registerEntities(\Magento\Catalog\Model\Product::CACHE_TAG, $ids);
}
}
@@ -0,0 +1,13 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\RelevanceRule;
class ProductRuleProcessor extends \Magento\Framework\Indexer\AbstractProcessor
{
public const INDEXER_ID = 'amasty_elastic_relevance_rule_product';
}
@@ -0,0 +1,32 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\RelevanceRule;
class RuleProductIndexer extends AbstractIndexer
{
/**
* @inheritdoc
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function doExecuteRow($id)
{
$this->getIndexBuilder()->reindexByRuleIds([$id]);
}
/**
* @inheritdoc
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function doExecuteList($ids)
{
$this->getIndexBuilder()->reindexByRuleIds($ids);
$this->getCacheContext()->registerTags($this->getIdentities());
}
}
@@ -0,0 +1,13 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Indexer\RelevanceRule;
class RuleProductProcessor extends \Magento\Framework\Indexer\AbstractProcessor
{
public const INDEXER_ID = 'amasty_elastic_relevance_rule_rule';
}
@@ -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'];
}
}

Some files were not shown because too many files have changed in this diff Show More