Skip to content

Commit 29bc9ad

Browse files
authored
Merge pull request #1523 from magento-performance/MAGETWO-75935
[PERFORMANCE] - PAT updates
2 parents 9c14af7 + 3bbb6b3 commit 29bc9ad

File tree

4 files changed

+122
-5
lines changed

4 files changed

+122
-5
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
6161
/**
6262
* @var string|null
6363
*/
64-
private $order = null;
64+
private $relevanceOrderDirection = null;
6565

6666
/**
6767
* @var string
@@ -361,9 +361,19 @@ protected function _renderFiltersBefore()
361361
[]
362362
);
363363

364-
if ($this->order && 'relevance' === $this->order['field']) {
365-
$this->getSelect()->order('search_result.'. TemporaryStorage::FIELD_SCORE . ' ' . $this->order['dir']);
364+
if ($this->relevanceOrderDirection) {
365+
$this->getSelect()->order(
366+
'search_result.'. TemporaryStorage::FIELD_SCORE . ' ' . $this->relevanceOrderDirection
367+
);
366368
}
369+
370+
/*
371+
* This order is required to force search results be the same
372+
* for the same requests and products with the same relevance
373+
* NOTE: this does not replace existing orders but ADDs one more
374+
*/
375+
$this->setOrder('entity_id');
376+
367377
return parent::_renderFiltersBefore();
368378
}
369379

@@ -385,10 +395,12 @@ protected function _renderFilters()
385395
*/
386396
public function setOrder($attribute, $dir = Select::SQL_DESC)
387397
{
388-
$this->order = ['field' => $attribute, 'dir' => $dir];
389-
if ($attribute !== 'relevance') {
398+
if ($attribute === 'relevance') {
399+
$this->relevanceOrderDirection = $dir;
400+
} else {
390401
parent::setOrder($attribute, $dir);
391402
}
403+
392404
return $this;
393405
}
394406

dev/tests/integration/testsuite/Magento/CatalogSearch/Model/ResourceModel/Fulltext/CollectionTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,45 @@ public function testLoadWithFilterSearch($request, $filters, $expectedCount)
3131
$this->assertCount($expectedCount, $items);
3232
}
3333

34+
/**
35+
* @magentoDataFixture Magento/Framework/Search/_files/products_with_the_same_search_score.php
36+
*/
37+
public function testSearchResultsAreTheSameForSameRequests()
38+
{
39+
$howManySearchRequests = 3;
40+
$previousResult = null;
41+
42+
$objManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
43+
44+
foreach (range(1, $howManySearchRequests) as $i) {
45+
/** @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $fulltextCollection */
46+
$fulltextCollection = $objManager->create(
47+
\Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection::class,
48+
['searchRequestName' => 'quick_search_container']
49+
);
50+
51+
$fulltextCollection->addFieldToFilter('search_term', 'shorts');
52+
$fulltextCollection->setOrder('relevance');
53+
$fulltextCollection->load();
54+
$items = $fulltextCollection->getItems();
55+
$this->assertGreaterThan(
56+
0,
57+
count($items),
58+
sprintf("Search #%s result must not be empty", $i)
59+
);
60+
61+
if ($previousResult) {
62+
$this->assertEquals(
63+
$previousResult,
64+
array_keys($items),
65+
"Search result must be the same for the same requests"
66+
);
67+
}
68+
69+
$previousResult = array_keys($items);
70+
}
71+
}
72+
3473
public function filtersDataProviderSearch()
3574
{
3675
return [
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Catalog\Api\ProductRepositoryInterface;
8+
use Magento\Catalog\Model\Product;
9+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
10+
use Magento\Catalog\Model\Product\Type;
11+
use Magento\Catalog\Model\Product\Visibility;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
14+
Bootstrap::getInstance()->reinitialize();
15+
16+
/** @var ProductRepositoryInterface $productRepository */
17+
$productRepository = Bootstrap::getObjectManager()
18+
->create(ProductRepositoryInterface::class);
19+
20+
$howManyProducts = 5;
21+
22+
foreach (range(1, $howManyProducts) as $productId) {
23+
$product = Bootstrap::getObjectManager()->create(Product::class);
24+
$product->setTypeId(Type::TYPE_SIMPLE)
25+
->setAttributeSetId(4)
26+
->setWebsiteIds([1])
27+
->setName('Cool short' . $productId)
28+
->setSku('cool_shorts_' . $productId)
29+
->setPrice(42)
30+
->setShortDescription("Some description about shorts")
31+
->setTaxClassId(0)
32+
->setDescription('Some description about <b>shorts</b>')
33+
->setVisibility(Visibility::VISIBILITY_BOTH)
34+
->setStatus(Status::STATUS_ENABLED)
35+
->setStockData(
36+
[
37+
'use_config_manage_stock' => 1,
38+
'qty' => 100,
39+
'is_qty_decimal' => 0,
40+
'is_in_stock' => 1,
41+
]
42+
);
43+
44+
$productRepository->save($product);
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
/** @var \Magento\Framework\Registry $registry */
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
$registry = $objectManager->get(\Magento\Framework\Registry::class);
9+
10+
$registry->unregister('isSecureArea');
11+
$registry->register('isSecureArea', true);
12+
13+
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
14+
$collection = $objectManager->create(\Magento\Catalog\Model\ResourceModel\Product\Collection::class);
15+
$collection->addAttributeToSelect('id')->load();
16+
if ($collection->count() > 0) {
17+
$collection->delete();
18+
}
19+
20+
$registry->unregister('isSecureArea');
21+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)