Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to index child attributes or not #2443

Open
wants to merge 6 commits into
base: 2.10.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Smile\ElasticsuiteCatalog\Model\ResourceModel\Eav\Indexer\Fulltext\Datasource\AbstractAttributeData as ResourceModel;
use Smile\ElasticsuiteCore\Index\Mapping\FieldFactory;
use Smile\ElasticsuiteCatalog\Helper\AbstractAttribute as AttributeHelper;
use Smile\ElasticsuiteCatalog\Scope\Config;
use Smile\ElasticsuiteCore\Api\Index\Mapping\FieldInterface;

/**
Expand All @@ -44,6 +45,9 @@ abstract class AbstractAttributeData
*/
protected $attributeHelper;

/** @var Config */
protected $config;

/**
* @var \Smile\ElasticsuiteCatalog\Model\ResourceModel\Eav\Indexer\Fulltext\Datasource\AbstractAttributeData
*/
Expand Down Expand Up @@ -75,17 +79,20 @@ abstract class AbstractAttributeData
/**
* Constructor
*
* @param Config $config Configuration retriever
* @param ResourceModel $resourceModel Resource model.
* @param FieldFactory $fieldFactory Mapping field factory.
* @param AttributeHelper $attributeHelper Attribute helper.
* @param array $indexedBackendModels List of indexed backend models added to the default list.
*/
public function __construct(
Config $config,
ResourceModel $resourceModel,
FieldFactory $fieldFactory,
AttributeHelper $attributeHelper,
array $indexedBackendModels = []
) {
$this->config = $config;
$this->resourceModel = $resourceModel;
$this->attributeHelper = $attributeHelper;
$this->fieldFactory = $fieldFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Smile\ElasticsuiteCatalog\Helper\AbstractAttribute as AttributeHelper;
use Smile\ElasticsuiteCatalog\Model\Eav\Indexer\Fulltext\Datasource\AbstractAttributeData;
use Smile\ElasticsuiteCatalog\Model\ResourceModel\Eav\Indexer\Fulltext\Datasource\AbstractAttributeData as ResourceModel;
use Smile\ElasticsuiteCatalog\Scope\Config;
use Smile\ElasticsuiteCore\Api\Index\DatasourceInterface;
use Smile\ElasticsuiteCore\Api\Index\Mapping\DynamicFieldProviderInterface;
use Smile\ElasticsuiteCore\Index\Mapping\FieldFactory;
Expand Down Expand Up @@ -59,6 +60,7 @@ class AttributeData extends AbstractAttributeData implements DatasourceInterface
/**
* Constructor
*
* @param Config $config Configuration retriever
* @param ResourceModel $resourceModel Resource model.
* @param FieldFactory $fieldFactory Mapping field factory.
* @param AttributeHelper $attributeHelper Attribute helper.
Expand All @@ -67,14 +69,15 @@ class AttributeData extends AbstractAttributeData implements DatasourceInterface
* @param ScopeConfigInterface $scopeConfig Scope Config.
*/
public function __construct(
Config $config,
ResourceModel $resourceModel,
FieldFactory $fieldFactory,
AttributeHelper $attributeHelper,
array $indexedBackendModels = [],
array $forbiddenChildrenAttributes = [],
ScopeConfigInterface $scopeConfig = null
) {
parent::__construct($resourceModel, $fieldFactory, $attributeHelper, $indexedBackendModels);
parent::__construct($config, $resourceModel, $fieldFactory, $attributeHelper, $indexedBackendModels);

$this->scopeConfig = $scopeConfig;
$this->forbiddenChildrenAttributes = array_values($forbiddenChildrenAttributes);
Expand All @@ -88,6 +91,10 @@ public function addData($storeId, array $indexData)
$productIds = array_keys($indexData);
$indexData = $this->addAttributeData($storeId, $productIds, $indexData);

if (!$this->config->isIncludeChildAttributes()) {
return $indexData;
}

$relationsByChildId = $this->resourceModel->loadChildrens($productIds, $storeId);

if (!empty($relationsByChildId)) {
Expand Down
55 changes: 55 additions & 0 deletions src/module-elasticsuite-catalog/Scope/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCatalog
* @author Dan Wallis <[email protected]>
* @copyright 2022 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCatalog\Scope;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

/**
* Configuration retreival class
*
* @category Smile
* @package Smile\ElasticsuiteCatalog
* @author Dan Wallis <[email protected]>
*/
class Config
{
protected const INCLUDE_CHILD_ATTRIBUTES = 'smile_elasticsuite_catalogsearch_settings/catalogsearch/include_child_attributes';

/** @var ScopeConfigInterface */
protected $scopeConfig;

/**
* @param ScopeConfigInterface $scopeConfig Magento configuration class
*/
public function __construct(
ScopeConfigInterface $scopeConfig
) {
$this->scopeConfig = $scopeConfig;
}

/**
* Retrieve configuration option to include/exclude child attributes from indexing
*
* @return bool
*/
public function isIncludeChildAttributes(): bool
{
return $this->scopeConfig->isSetFlag(
self::INCLUDE_CHILD_ATTRIBUTES,
ScopeInterface::SCOPE_STORE
);
}
}
5 changes: 5 additions & 0 deletions src/module-elasticsuite-catalog/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[If enabled, child products SKUs of composite products will be indexed in a separate field and the "sku" field will only contain the parent product sku.]]></comment>
</field>
<field id="include_child_attributes" translate="label comment" type="select" sortOrder="55" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Include child attributes during indexing</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[If enabled, attributes of associated child products will be included with their parents when indexing. This will impact what filters are shown in category pages. For example, if a child of a bundle product has a particular colour then its parent will have this colour associated with it. <br><strong>Note when changing this setting</strong>, it is recommended to perform a full reindex of the <q>Catalog Search</q> indexer.]]></comment>
</field>
<field id="compute_child_product_discount" translate="label comment" type="select" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Enable indexing discount on child products</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
Expand Down
1 change: 1 addition & 0 deletions src/module-elasticsuite-catalog/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<expanded_facets>3</expanded_facets>
<adaptive_slider_enabled>0</adaptive_slider_enabled>
<index_child_product_sku>0</index_child_product_sku>
<include_child_attributes>1</include_child_attributes>
<compute_child_product_discount>0</compute_child_product_discount>
</catalogsearch>
</smile_elasticsuite_catalogsearch_settings>
Expand Down
Loading