Skip to content

Commit 2382298

Browse files
author
Kasian,Andrii(akasian)
committed
Merge pull request #448 from magento-dragons/bugs
[DRAGONS] Bugs
2 parents aa2088f + ea028e9 commit 2382298

File tree

19 files changed

+489
-335
lines changed

19 files changed

+489
-335
lines changed

app/code/Magento/CatalogInventory/Model/Resource/Stock/Item.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ protected function _afterSave(AbstractModel $object)
123123
parent::_afterSave($object);
124124
/** @var StockItemInterface $object */
125125
if ($this->processIndexEvents) {
126-
$this->stockIndexerProcessor->markIndexerAsInvalid();
127126
$this->stockIndexerProcessor->reindexRow($object->getProductId());
128127
}
129128
return $this;

app/code/Magento/CatalogSearch/Model/Adapter/Mysql/Filter/Preprocessor.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,10 @@ public function __construct(
6464

6565
/**
6666
* {@inheritdoc}
67-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
6867
*/
6968
public function process(FilterInterface $filter, $isNegation, $query, QueryContainer $queryContainer)
7069
{
71-
return $resultQuery = $this->processQueryWithField($filter, $isNegation, $query, $queryContainer);
70+
return $this->processQueryWithField($filter, $isNegation, $query, $queryContainer);
7271
}
7372

7473
/**

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

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
use Magento\Framework\DB\Select;
1212
use Magento\Framework\Search\Adapter\Mysql\ConditionManager;
1313
use Magento\Framework\Search\Adapter\Mysql\IndexBuilderInterface;
14-
use Magento\Framework\Search\Adapter\Mysql\ScoreBuilder;
1514
use Magento\Framework\Search\Request\Dimension;
15+
use Magento\Framework\Search\Request\Query\Bool;
16+
use Magento\Framework\Search\Request\QueryInterface;
17+
use Magento\Framework\Search\Request\QueryInterface as RequestQueryInterface;
1618
use Magento\Framework\Search\RequestInterface;
1719
use Magento\Search\Model\ScopeResolver\IndexScopeResolver;
1820
use Magento\Store\Model\ScopeInterface;
@@ -82,22 +84,26 @@ public function build(RequestInterface $request)
8284
['search_index' => $searchIndexTable],
8385
['entity_id' => 'entity_id']
8486
)
85-
->joinLeft(
86-
['category_index' => $this->resource->getTableName('catalog_category_product_index')],
87-
'search_index.entity_id = category_index.product_id',
88-
[]
89-
)
9087
->joinLeft(
9188
['cea' => $this->resource->getTableName('catalog_eav_attribute')],
9289
'search_index.attribute_id = cea.attribute_id',
93-
[ScoreBuilder::WEIGHT_FIELD]
94-
)
95-
->joinLeft(
96-
['cpie' => $this->resource->getTableName('catalog_product_index_eav')],
97-
'search_index.entity_id = cpie.entity_id AND search_index.attribute_id = cpie.attribute_id',
9890
[]
9991
);
10092

93+
if ($this->isNeedToAddFilters($request)) {
94+
$select
95+
->joinLeft(
96+
['category_index' => $this->resource->getTableName('catalog_category_product_index')],
97+
'search_index.entity_id = category_index.product_id',
98+
[]
99+
)
100+
->joinLeft(
101+
['cpie' => $this->resource->getTableName('catalog_product_index_eav')],
102+
'search_index.entity_id = cpie.entity_id AND search_index.attribute_id = cpie.attribute_id',
103+
[]
104+
);
105+
}
106+
101107
$select = $this->processDimensions($request, $select);
102108

103109
$isShowOutOfStock = $this->config->isSetFlag(
@@ -113,8 +119,8 @@ public function build(RequestInterface $request)
113119
$this->storeManager->getWebsite()->getId()
114120
),
115121
[]
116-
)
117-
->where('stock_index.stock_status = ?', 1);
122+
);
123+
$select->where('stock_index.stock_status = ?', 1);
118124
}
119125

120126
return $select;
@@ -179,4 +185,43 @@ private function getSelect()
179185
{
180186
return $this->getReadConnection()->select();
181187
}
188+
189+
/**
190+
* @param RequestInterface $request
191+
* @return bool
192+
*/
193+
private function isNeedToAddFilters(RequestInterface $request)
194+
{
195+
return $this->hasFilters($request->getQuery());
196+
}
197+
198+
/**
199+
* @param QueryInterface $query
200+
* @return bool
201+
*/
202+
private function hasFilters(QueryInterface $query)
203+
{
204+
$hasFilters = false;
205+
switch ($query->getType()) {
206+
case RequestQueryInterface::TYPE_BOOL:
207+
/** @var \Magento\Framework\Search\Request\Query\Bool $query */
208+
foreach ($query->getMust() as $subQuery) {
209+
$hasFilters |= $this->hasFilters($subQuery);
210+
}
211+
foreach ($query->getShould() as $subQuery) {
212+
$hasFilters |= $this->hasFilters($subQuery);
213+
}
214+
foreach ($query->getMustNot() as $subQuery) {
215+
$hasFilters |= $this->hasFilters($subQuery);
216+
}
217+
break;
218+
case RequestQueryInterface::TYPE_FILTER:
219+
$hasFilters |= true;
220+
break;
221+
default:
222+
$hasFilters |= false;
223+
break;
224+
}
225+
return $hasFilters;
226+
}
182227
}

app/code/Magento/CatalogSearch/Model/Source/Weight.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Weight
1515
*
1616
* @var int[]
1717
*/
18-
protected $_weights = [1, 2, 3, 4, 5];
18+
protected $_weights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
1919

2020
/**
2121
* Retrieve search weights as options array

app/code/Magento/CatalogSearch/Setup/InstallData.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Setup\ModuleContextInterface;
1212
use Magento\Framework\Setup\ModuleDataSetupInterface;
1313
use Magento\Indexer\Model\IndexerInterfaceFactory;
14+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
1415

1516
class InstallData implements InstallDataInterface
1617
{
@@ -21,10 +22,29 @@ class InstallData implements InstallDataInterface
2122

2223
/**
2324
* @param IndexerInterfaceFactory $indexerFactory
25+
* @param ProductAttributeRepositoryInterface $attributeRepository
2426
*/
25-
public function __construct(IndexerInterfaceFactory $indexerFactory)
26-
{
27+
public function __construct(
28+
IndexerInterfaceFactory $indexerFactory,
29+
ProductAttributeRepositoryInterface $attributeRepository
30+
) {
2731
$this->indexerFactory = $indexerFactory;
32+
$this->attributeRepository = $attributeRepository;
33+
}
34+
35+
/**
36+
* Installs data for a module
37+
*
38+
* @param ModuleDataSetupInterface $setup
39+
* @param ModuleContextInterface $context
40+
* @return void
41+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
42+
*/
43+
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
44+
{
45+
$this->setWeight('sku', 6);
46+
$this->setWeight('name', 5);
47+
$this->getIndexer('catalogsearch_fulltext')->reindexAll();
2848
}
2949

3050
/**
@@ -37,15 +57,14 @@ private function getIndexer($indexerId)
3757
}
3858

3959
/**
40-
* Installs data for a module
41-
*
42-
* @param ModuleDataSetupInterface $setup
43-
* @param ModuleContextInterface $context
60+
* @param string $attributeCode
61+
* @param int $weight
4462
* @return void
45-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4663
*/
47-
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
64+
private function setWeight($attributeCode, $weight)
4865
{
49-
$this->getIndexer('catalogsearch_fulltext')->reindexAll();
66+
$attribute = $this->attributeRepository->get($attributeCode);
67+
$attribute->setSearchWeight($weight);
68+
$this->attributeRepository->save($attribute);
5069
}
5170
}

app/code/Magento/CatalogSearch/Setup/InstallSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
9090
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_FLOAT,
9191
'unsigned' => true,
9292
'nullable' => false,
93-
'default' => '3',
93+
'default' => '1',
9494
'comment' => 'Search Weight'
9595
]
9696
);

app/code/Magento/CatalogSearch/Test/Unit/Model/Search/IndexBuilderTest.php

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@ protected function setUp()
6969

7070
$this->request = $this->getMockBuilder('\Magento\Framework\Search\RequestInterface')
7171
->disableOriginalConstructor()
72-
->setMethods(['getIndex', 'getDimensions'])
72+
->setMethods(['getIndex', 'getDimensions', 'getQuery'])
7373
->getMockForAbstractClass();
74+
$this->request->expects($this->once())
75+
->method('getQuery')
76+
->willReturn(
77+
$this->getMockBuilder('Magento\Framework\Search\Request\QueryInterface')->getMockForAbstractClass()
78+
);
7479

7580
$this->config = $this->getMockBuilder('\Magento\Framework\App\Config\ScopeConfigInterface')
7681
->disableOriginalConstructor()
@@ -157,7 +162,7 @@ public function testBuildWithoutOutOfStock()
157162
->method('getDimensions')
158163
->willReturn($dimensions);
159164

160-
$this->mockBuild($index, $tableSuffix);
165+
$this->mockBuild($index, $tableSuffix, false);
161166

162167
$this->config->expects($this->once())
163168
->method('isSetFlag')
@@ -168,11 +173,11 @@ public function testBuildWithoutOutOfStock()
168173
$website = $this->getMockBuilder('Magento\Store\Model\Website')->disableOriginalConstructor()->getMock();
169174
$website->expects($this->once())->method('getId')->willReturn(1);
170175
$this->storeManager->expects($this->once())->method('getWebsite')->willReturn($website);
171-
$this->select->expects($this->at(4))
176+
$this->select->expects($this->at(2))
172177
->method('where')
173178
->with('(someName=someValue)')
174179
->willReturnSelf();
175-
$this->select->expects($this->at(5))
180+
$this->select->expects($this->at(3))
176181
->method('joinLeft')
177182
->with(
178183
['stock_index' => 'cataloginventory_stock_status'],
@@ -181,7 +186,7 @@ public function testBuildWithoutOutOfStock()
181186
[]
182187
)
183188
->willReturnSelf();
184-
$this->select->expects($this->at(6))
189+
$this->select->expects($this->at(4))
185190
->method('where')
186191
->with('stock_index.stock_status = ?', 1)
187192
->will($this->returnSelf());
@@ -190,7 +195,7 @@ public function testBuildWithoutOutOfStock()
190195
$this->assertSame($this->select, $result);
191196
}
192197

193-
protected function mockBuild($index, $tableSuffix)
198+
protected function mockBuild($index, $tableSuffix, $hasFilters = false)
194199
{
195200
$this->request->expects($this->atLeastOnce())
196201
->method('getIndex')
@@ -220,7 +225,7 @@ function ($index, $dimensions) {
220225
)
221226
);
222227

223-
$this->select->expects($this->once())
228+
$this->select->expects($this->at(0))
224229
->method('from')
225230
->with(
226231
['search_index' => $index . '_' . $tableSuffix],
@@ -229,30 +234,31 @@ function ($index, $dimensions) {
229234
->will($this->returnSelf());
230235

231236
$this->select->expects($this->at(1))
232-
->method('joinLeft')
233-
->with(
234-
['category_index' => 'catalog_category_product_index'],
235-
'search_index.entity_id = category_index.product_id',
236-
[]
237-
)
238-
->will($this->returnSelf());
239-
240-
$this->select->expects($this->at(2))
241237
->method('joinLeft')
242238
->with(
243239
['cea' => 'catalog_eav_attribute'],
244240
'search_index.attribute_id = cea.attribute_id',
245-
['search_weight']
246-
)
247-
->will($this->returnSelf());
248-
$this->select->expects($this->at(3))
249-
->method('joinLeft')
250-
->with(
251-
['cpie' => $this->resource->getTableName('catalog_product_index_eav')],
252-
'search_index.entity_id = cpie.entity_id AND search_index.attribute_id = cpie.attribute_id',
253241
[]
254242
)
255-
->willReturnSelf();
243+
->will($this->returnSelf());
244+
if ($hasFilters) {
245+
$this->select->expects($this->at(2))
246+
->method('joinLeft')
247+
->with(
248+
['category_index' => 'catalog_category_product_index'],
249+
'search_index.entity_id = category_index.product_id',
250+
[]
251+
)
252+
->will($this->returnSelf());
253+
$this->select->expects($this->at(3))
254+
->method('joinLeft')
255+
->with(
256+
['cpie' => $this->resource->getTableName('catalog_product_index_eav')],
257+
'search_index.entity_id = cpie.entity_id AND search_index.attribute_id = cpie.attribute_id',
258+
[]
259+
)
260+
->willReturnSelf();
261+
}
256262
}
257263

258264
/**

app/code/Magento/Indexer/etc/crontab.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Cron/etc/crontab.xsd">
9-
<group id="default">
9+
<group id="index">
1010
<job name="indexer_reindex_all_invalid" instance="Magento\Indexer\Model\Processor" method="reindexAllInvalid">
1111
<schedule>* * * * *</schedule>
1212
</job>

0 commit comments

Comments
 (0)