Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 020b09e

Browse files
MAGETWO-75935: Search results is not the same for the same search queries
1 parent 540f467 commit 020b09e

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,13 @@ protected function _renderFiltersBefore()
364364
if ($this->order && 'relevance' === $this->order['field']) {
365365
$this->getSelect()->order('search_result.'. TemporaryStorage::FIELD_SCORE . ' ' . $this->order['dir']);
366366
}
367+
368+
/*
369+
* This order is required to force search results be the same
370+
* for the same requests and products with the same relevance
371+
*/
372+
$this->setOrder('entity_id');
373+
367374
return parent::_renderFiltersBefore();
368375
}
369376

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,37 @@ 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 $_) {
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(0, count($items), "Search result must not be empty");
56+
57+
if ($previousResult) {
58+
$this->assertEquals($previousResult, array_keys($items), "Search result must be the same for the same requests");
59+
}
60+
61+
$previousResult = array_keys($items);
62+
}
63+
}
64+
3465
public function filtersDataProviderSearch()
3566
{
3667
return [
@@ -42,4 +73,4 @@ public function filtersDataProviderSearch()
4273
['catalog_view_container', [], 0],
4374
];
4475
}
45-
}
76+
}
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)