Skip to content

Commit 05d2b3a

Browse files
committed
MAGETWO-51447: CatalogSearch Fulltext Collection not allows to use filters with multiple values
1 parent d0495a9 commit 05d2b3a

File tree

9 files changed

+205
-65
lines changed

9 files changed

+205
-65
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/AbstractEav.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ protected function _getIndexableAttributesCondition()
263263
'ca.is_filterable_in_search > 0',
264264
'ca.is_visible_in_advanced_search > 0',
265265
'ca.is_filterable > 0',
266+
// Visibility is attribute that isn't used by search, but required to determine is product should be shown
267+
"ea.attribute_code = 'visibility'"
266268
];
267269

268270
return implode(' OR ', $conditions);

app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public function addFieldToFilter($field, $condition = null)
265265

266266
$this->getSearchCriteriaBuilder();
267267
$this->getFilterBuilder();
268-
if (!is_array($condition) || !in_array(key($condition), ['from', 'to'])) {
268+
if (!is_array($condition) || !in_array(key($condition), ['from', 'to'], true)) {
269269
$this->filterBuilder->setField($field);
270270
$this->filterBuilder->setValue($condition);
271271
$this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
@@ -371,7 +371,7 @@ protected function _renderFilters()
371371
public function setOrder($attribute, $dir = Select::SQL_DESC)
372372
{
373373
$this->order = ['field' => $attribute, 'dir' => $dir];
374-
if ($attribute != 'relevance') {
374+
if ($attribute !== 'relevance') {
375375
parent::setOrder($attribute, $dir);
376376
}
377377
return $this;

app/code/Magento/CatalogSearch/Model/Search/IndexBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function build(RequestInterface $request)
123123
ScopeInterface::SCOPE_STORE
124124
);
125125
if ($isShowOutOfStock === false) {
126-
$select->joinLeft(
126+
$select->joinInner(
127127
['stock_index' => $this->resource->getTableName('cataloginventory_stock_status')],
128128
'search_index.entity_id = stock_index.product_id'
129129
. $this->resource->getConnection()->quoteInto(

app/code/Magento/CatalogSearch/Model/Search/TableMapper.php

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
namespace Magento\CatalogSearch\Model\Search;
88

9+
use Magento\Catalog\Model\Product;
910
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
11+
use Magento\Eav\Model\Config as EavConfig;
1012
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
13+
use Magento\Framework\App\ObjectManager;
1114
use Magento\Framework\App\ResourceConnection as AppResource;
1215
use Magento\Framework\DB\Select;
1316
use Magento\Framework\Search\Request\FilterInterface;
@@ -34,41 +37,64 @@ class TableMapper
3437

3538
/**
3639
* @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection
40+
* @deprecated
3741
*/
3842
private $attributeCollection;
3943

44+
/**
45+
* @var EavConfig
46+
*/
47+
private $eavConfig;
48+
4049
/**
4150
* @param AppResource $resource
4251
* @param StoreManagerInterface $storeManager
4352
* @param CollectionFactory $attributeCollectionFactory
53+
* @param EavConfig $eavConfig
4454
*/
4555
public function __construct(
4656
AppResource $resource,
4757
StoreManagerInterface $storeManager,
48-
CollectionFactory $attributeCollectionFactory
58+
CollectionFactory $attributeCollectionFactory,
59+
EavConfig $eavConfig = null
4960
) {
5061
$this->resource = $resource;
5162
$this->storeManager = $storeManager;
5263
$this->attributeCollection = $attributeCollectionFactory->create();
64+
$this->eavConfig = $eavConfig !== null ? $eavConfig : ObjectManager::getInstance()->get(EavConfig::class);
5365
}
5466

5567
/**
5668
* @param Select $select
5769
* @param RequestInterface $request
5870
* @return Select
71+
* @throws \LogicException
5972
*/
6073
public function addTables(Select $select, RequestInterface $request)
6174
{
6275
$mappedTables = [];
6376
$filters = $this->getFilters($request->getQuery());
6477
foreach ($filters as $filter) {
65-
list($alias, $table, $mapOn, $mappedFields) = $this->getMappingData($filter);
78+
list($alias, $table, $mapOn, $mappedFields, $joinType) = $this->getMappingData($filter);
6679
if (!array_key_exists($alias, $mappedTables)) {
67-
$select->joinLeft(
68-
[$alias => $table],
69-
$mapOn,
70-
$mappedFields
71-
);
80+
switch ($joinType) {
81+
case \Magento\Framework\DB\Select::INNER_JOIN:
82+
$select->joinInner(
83+
[$alias => $table],
84+
$mapOn,
85+
$mappedFields
86+
);
87+
break;
88+
case \Magento\Framework\DB\Select::LEFT_JOIN:
89+
$select->joinLeft(
90+
[$alias => $table],
91+
$mapOn,
92+
$mappedFields
93+
);
94+
break;
95+
default:
96+
throw new \LogicException(__('Unsupported join type: %1', $joinType));
97+
}
7298
$mappedTables[$alias] = $table;
7399
}
74100
}
@@ -102,6 +128,7 @@ private function getMappingData(FilterInterface $filter)
102128
$mapOn = null;
103129
$mappedFields = null;
104130
$field = $filter->getField();
131+
$joinType = \Magento\Framework\DB\Select::INNER_JOIN;
105132
$fieldToTableMap = $this->getFieldToTableMap($field);
106133
if ($fieldToTableMap) {
107134
list($alias, $table, $mapOn, $mappedFields) = $fieldToTableMap;
@@ -110,6 +137,7 @@ private function getMappingData(FilterInterface $filter)
110137
if ($filter->getType() === FilterInterface::TYPE_TERM
111138
&& in_array($attribute->getFrontendInput(), ['select', 'multiselect'], true)
112139
) {
140+
$joinType = \Magento\Framework\DB\Select::LEFT_JOIN;
113141
$table = $this->resource->getTableName('catalog_product_index_eav');
114142
$alias = $field . RequestGenerator::FILTER_SUFFIX;
115143
$mapOn = sprintf(
@@ -127,7 +155,7 @@ private function getMappingData(FilterInterface $filter)
127155
}
128156
}
129157

130-
return [$alias, $table, $mapOn, $mappedFields];
158+
return [$alias, $table, $mapOn, $mappedFields, $joinType];
131159
}
132160

133161
/**
@@ -242,10 +270,11 @@ private function getFieldToTableMap($field)
242270
/**
243271
* @param string $field
244272
* @return \Magento\Catalog\Model\ResourceModel\Eav\Attribute
273+
* @throws \Magento\Framework\Exception\LocalizedException
245274
*/
246275
private function getAttributeByCode($field)
247276
{
248-
$attribute = $this->attributeCollection->getItemByColumnValue('attribute_code', $field);
277+
$attribute = $this->eavConfig->getAttribute(Product::ENTITY, $field);
249278
return $attribute;
250279
}
251280
}

0 commit comments

Comments
 (0)