Skip to content

Commit 4a25cf3

Browse files
committed
MAGETWO-39942: Low quick search performance(MySql)
- Fix unit tests
1 parent 3ba5989 commit 4a25cf3

File tree

2 files changed

+88
-36
lines changed

2 files changed

+88
-36
lines changed

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

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use Magento\Framework\Search\Adapter\Mysql\ConditionManager;
1313
use Magento\Framework\Search\Adapter\Mysql\IndexBuilderInterface;
1414
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;
1518
use Magento\Framework\Search\RequestInterface;
1619
use Magento\Search\Model\ScopeResolver\IndexScopeResolver;
1720
use Magento\Store\Model\ScopeInterface;
@@ -81,22 +84,26 @@ public function build(RequestInterface $request)
8184
['search_index' => $searchIndexTable],
8285
['entity_id' => 'entity_id']
8386
)
84-
->joinLeft(
85-
['category_index' => $this->resource->getTableName('catalog_category_product_index')],
86-
'search_index.entity_id = category_index.product_id',
87-
[]
88-
)
8987
->joinLeft(
9088
['cea' => $this->resource->getTableName('catalog_eav_attribute')],
9189
'search_index.attribute_id = cea.attribute_id',
9290
[]
93-
)
94-
->joinLeft(
95-
['cpie' => $this->resource->getTableName('catalog_product_index_eav')],
96-
'search_index.entity_id = cpie.entity_id AND search_index.attribute_id = cpie.attribute_id',
97-
[]
9891
);
9992

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+
100107
$select = $this->processDimensions($request, $select);
101108

102109
$isShowOutOfStock = $this->config->isSetFlag(
@@ -112,8 +119,8 @@ public function build(RequestInterface $request)
112119
$this->storeManager->getWebsite()->getId()
113120
),
114121
[]
115-
)
116-
->where('stock_index.stock_status = ?', 1);
122+
);
123+
$select->where('stock_index.stock_status = ?', 1);
117124
}
118125

119126
return $select;
@@ -178,4 +185,43 @@ private function getSelect()
178185
{
179186
return $this->getReadConnection()->select();
180187
}
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+
}
181227
}

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

Lines changed: 30 additions & 24 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',
245241
[]
246242
)
247243
->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',
253-
[]
254-
)
255-
->willReturnSelf();
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
/**

0 commit comments

Comments
 (0)