Skip to content

Commit

Permalink
Merge branch 'MC-41647' of https://github.com/magento-l3/magento2ce i…
Browse files Browse the repository at this point in the history
…nto PR-20210423
  • Loading branch information
chittima committed May 6, 2021
2 parents 135d932 + 1bc28f4 commit 8e6a5b1
Showing 1 changed file with 89 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection implements
\Magento\Search\Model\SearchCollectionInterface
{
/**
* @var array
*/
private $indexUsageEnforcements;

/**
* Attribute collection
*
Expand Down Expand Up @@ -61,6 +66,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
* @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
* @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeCollectionFactory
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
* @param array $indexUsageEnforcements
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -84,7 +90,8 @@ public function __construct(
\Magento\Framework\Stdlib\DateTime $dateTime,
\Magento\Customer\Api\GroupManagementInterface $groupManagement,
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeCollectionFactory,
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
array $indexUsageEnforcements = []
) {
$this->_attributeCollectionFactory = $attributeCollectionFactory;
parent::__construct(
Expand All @@ -109,6 +116,7 @@ public function __construct(
$groupManagement,
$connection
);
$this->indexUsageEnforcements = $indexUsageEnforcements;
}

/**
Expand Down Expand Up @@ -197,6 +205,35 @@ protected function _hasAttributeOptionsAndSearchable($attribute)
return false;
}

/**
* Prepare table names for the index enforcements
*
* @return array
*/
private function prepareIndexEnforcements() : array
{
$result = [];
foreach ($this->indexUsageEnforcements as $table => $index) {
$table = $this->getTable($table);
if ($this->isIndexExists($table, $index)) {
$result[$table] = $index;
}
}
return $result;
}

/**
* Check if index exists in the table
*
* @param string $table
* @param string $index
* @return bool
*/
private function isIndexExists(string $table, string $index) : bool
{
return array_key_exists($index, $this->_conn->getIndexList($table));
}

/**
* Retrieve SQL for search entities
*
Expand All @@ -208,6 +245,7 @@ protected function _getSearchEntityIdsSql($query, $searchOnlyInCurrentStore = tr
{
$tables = [];
$selects = [];
$preparedIndexEnforcements = $this->prepareIndexEnforcements();

$likeOptions = ['position' => 'any'];

Expand Down Expand Up @@ -249,23 +287,56 @@ protected function _getSearchEntityIdsSql($query, $searchOnlyInCurrentStore = tr

$ifValueId = $this->getConnection()->getIfNullSql('t2.value', 't1.value');
foreach ($tables as $table => $attributeIds) {
$selects[] = $this->getConnection()->select()->from(
['t1' => $table],
$linkField
)->joinLeft(
['t2' => $table],
$joinCondition,
[]
)->where(
't1.attribute_id IN (?)',
$attributeIds,
\Zend_Db::INT_TYPE
)->where(
't1.store_id = ?',
0
)->where(
$this->_resourceHelper->getCILike($ifValueId, $this->_searchQuery, $likeOptions)
);
if (!empty($preparedIndexEnforcements[$table])) {
$condition1 = $this->_conn->quoteInto(
'`t1`.`attribute_id` IN (?)',
$attributeIds,
\Zend_Db::INT_TYPE
);
$condition2 = '`t1`.`store_id` = 0';
$quotedField = $this->_conn->quoteIdentifier($ifValueId);
$condition3 = $this->_conn->quoteInto(
$quotedField . ' LIKE ?',
$this->_resourceHelper->addLikeEscape($this->_searchQuery, $likeOptions)
);

//force index statement not implemented in framework
// phpcs:ignore Magento2.SQL.RawQuery
$select = sprintf(
'SELECT `t1`.`%s` FROM `%s` AS `t1` FORCE INDEX(%s)
LEFT JOIN `%s` AS `t2` FORCE INDEX(%s)
ON %s WHERE %s AND %s AND (%s)',
$linkField,
$table,
$preparedIndexEnforcements[$table],
$table,
$preparedIndexEnforcements[$table],
$joinCondition,
$condition1,
$condition2,
$condition3
);
} else {
$select = $this->getConnection()->select();
$select->from(
['t1' => $table],
$linkField
)->joinLeft(
['t2' => $table],
$joinCondition,
[]
)->where(
't1.attribute_id IN (?)',
$attributeIds,
\Zend_Db::INT_TYPE
)->where(
't1.store_id = ?',
0
)->where(
$this->_resourceHelper->getCILike($ifValueId, $this->_searchQuery, $likeOptions)
);
}
$selects[] = $select;
}

$sql = $this->_getSearchInOptionSql($query);
Expand Down

0 comments on commit 8e6a5b1

Please sign in to comment.