Skip to content

Commit 6f9d1a4

Browse files
author
Spandana Chittimala
committed
Merge remote-tracking branch 'tango/MAGETWO-99769' into PR-05-29-2019
2 parents 5896d13 + db5c732 commit 6f9d1a4

File tree

5 files changed

+238
-3
lines changed

5 files changed

+238
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private function generateRequest($attributeType, $container, $useFulltext)
103103
}
104104
}
105105
/** @var $attribute Attribute */
106-
if (!$attribute->getIsSearchable() || in_array($attribute->getAttributeCode(), ['price', 'sku'], true)) {
106+
if (!$attribute->getIsSearchable() || in_array($attribute->getAttributeCode(), ['price'], true)) {
107107
// Some fields have their own specific handlers
108108
continue;
109109
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Magento\CatalogSearch\Model\Search\RequestGenerator\GeneratorResolver;
1010
use Magento\CatalogSearch\Model\Search\RequestGenerator\GeneratorInterface;
1111

12+
/**
13+
* Test for \Magento\CatalogSearch\Model\Search\RequestGenerator
14+
*/
1215
class RequestGeneratorTest extends \PHPUnit\Framework\TestCase
1316
{
1417
/** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
@@ -61,7 +64,7 @@ public function attributesProvider()
6164
return [
6265
[
6366
[
64-
'quick_search_container' => ['queries' => 0, 'filters' => 0, 'aggregations' => 0],
67+
'quick_search_container' => ['queries' => 1, 'filters' => 0, 'aggregations' => 0],
6568
'advanced_search_container' => ['queries' => 0, 'filters' => 0, 'aggregations' => 0],
6669
'catalog_view_container' => ['queries' => 0, 'filters' => 0, 'aggregations' => 0]
6770
],

app/code/Magento/CatalogSearch/etc/search_request.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
<queryReference clause="must" ref="visibility"/>
2020
</query>
2121
<query xsi:type="matchQuery" value="$search_term$" name="search">
22-
<match field="sku"/>
2322
<match field="*"/>
2423
</query>
2524
<query xsi:type="filteredQuery" name="category">
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\CatalogSearch\Model\Search;
10+
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Model\Product;
13+
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
14+
use Magento\Eav\Api\AttributeRepositoryInterface;
15+
use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
16+
use Magento\Elasticsearch6\Model\Client\Elasticsearch as ElasticsearchClient;
17+
use Magento\Framework\Exception\NoSuchEntityException;
18+
use Magento\Framework\Exception\StateException;
19+
use Magento\Framework\Search\Request\Builder;
20+
use Magento\Framework\Search\Request\Config as RequestConfig;
21+
use Magento\Framework\Search\Response\QueryResponse;
22+
use Magento\Framework\Search\SearchEngineInterface;
23+
use Magento\Indexer\Model\Indexer;
24+
use Magento\TestFramework\Helper\Bootstrap;
25+
use Magento\TestFramework\Helper\CacheCleaner;
26+
use Magento\TestFramework\ObjectManager;
27+
use PHPUnit\Framework\TestCase;
28+
29+
/**
30+
* Test for name over sku search weight of product attributes
31+
*
32+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
33+
* @magentoAppIsolation enabled
34+
* @magentoDbIsolation enabled
35+
*/
36+
class AttributeSearchWeightTest extends TestCase
37+
{
38+
39+
/** @var $objectManager ObjectManager */
40+
private $objectManager;
41+
42+
/**
43+
* @var ConnectionManager
44+
*/
45+
private $connectionManager;
46+
47+
/**
48+
* @var ElasticsearchClient
49+
*/
50+
private $client;
51+
52+
/**
53+
* @var ProductRepositoryInterface
54+
*/
55+
private $productRepository;
56+
57+
protected function setUp()
58+
{
59+
$this->objectManager = Bootstrap::getObjectManager();
60+
$this->connectionManager = $this->objectManager->create(ConnectionManager::class);
61+
$this->client = $this->connectionManager->getConnection();
62+
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
63+
}
64+
65+
/**
66+
* @param string $attributeName
67+
* @param int $searchWeight
68+
* @throws NoSuchEntityException
69+
* @throws StateException
70+
*/
71+
private function setAttributeSearchWeight(string $attributeName, int $searchWeight)
72+
{
73+
/** @var AttributeRepositoryInterface $attributeRepository */
74+
$attributeRepository = $this->objectManager->create(AttributeRepositoryInterface::class);
75+
76+
/** @var Attribute $attribute */
77+
$attribute = $attributeRepository->get('catalog_product', $attributeName);
78+
79+
if ($attribute) {
80+
$attribute->setSearchWeight($searchWeight);
81+
$attributeRepository->save($attribute);
82+
}
83+
}
84+
85+
/**
86+
* @throws \Throwable
87+
*/
88+
private function reindex()
89+
{
90+
CacheCleaner::cleanAll();
91+
92+
/** @var Indexer $indexer */
93+
$indexer = $this->objectManager->create(Indexer::class);
94+
$indexer->load('catalogsearch_fulltext');
95+
$indexer->reindexAll();
96+
}
97+
98+
/**
99+
* @param string $query
100+
* @return array
101+
* @throws NoSuchEntityException
102+
*/
103+
private function findProducts(string $query): array
104+
{
105+
$config = $this->objectManager->create(RequestConfig::class);
106+
107+
/** @var Builder $requestBuilder */
108+
$requestBuilder = $this->objectManager->create(
109+
Builder::class,
110+
['config' => $config]
111+
);
112+
$requestBuilder->bind('search_term', $query);
113+
$requestBuilder->setRequestName('quick_search_container');
114+
115+
/** @var QueryResponse $searchResult */
116+
$searchResults = $this->objectManager->create(SearchEngineInterface::class)
117+
->search($requestBuilder->create());
118+
119+
$products = [];
120+
foreach ($searchResults as $searchResult) {
121+
$products [] = $this->productRepository->getById($searchResult->getId());
122+
}
123+
124+
return $products;
125+
}
126+
127+
/**
128+
* @dataProvider skuOverNameAttributeSearchWeightDataProvider
129+
* @magentoConfigFixture default/catalog/search/engine elasticsearch6
130+
* @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix composite_product_search
131+
* @magentoDataFixture Magento/CatalogSearch/_files/products_for_sku_search_weight_score.php
132+
* @param string $searchQuery
133+
* @param int $skuSearchWeight
134+
* @param int $nameSearchWeight
135+
* @param string $firstMatchProductName
136+
* @param string $secondMatchProductName
137+
* @throws NoSuchEntityException
138+
* @throws \Throwable
139+
*/
140+
public function testSkuOverNameAttributeSearchWeight(
141+
string $searchQuery,
142+
int $skuSearchWeight,
143+
int $nameSearchWeight,
144+
string $firstMatchProductName,
145+
string $secondMatchProductName
146+
) {
147+
$this->setAttributeSearchWeight('sku', $skuSearchWeight);
148+
$this->setAttributeSearchWeight('name', $nameSearchWeight);
149+
$this->reindex();
150+
151+
/** @var Product $products [] */
152+
$products = $this->findProducts($searchQuery);
153+
154+
$this->assertCount(
155+
2,
156+
$products,
157+
'Expected to find 2 products, found ' . count($products) . '.'
158+
);
159+
160+
$this->assertEquals(
161+
$firstMatchProductName,
162+
$products[0]->getData('name'),
163+
'Products order is not as expected.'
164+
);
165+
$this->assertEquals(
166+
$secondMatchProductName,
167+
$products[1]->getData('name'),
168+
'Products order is not as expected.'
169+
);
170+
}
171+
172+
public function skuOverNameAttributeSearchWeightDataProvider(): array
173+
{
174+
return [
175+
['1-2-3-4', 10, 5, 'test', '1-2-3-4'],
176+
['1-2-3-4', 5, 10, '1-2-3-4', 'test'],
177+
];
178+
}
179+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
/** @var ProductRepositoryInterface $productRepository */
15+
$productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
16+
$product = Bootstrap::getObjectManager()->create(Product::class);
17+
$product->setTypeId(Type::TYPE_SIMPLE)
18+
->setAttributeSetId(4)
19+
->setWebsiteIds([1])
20+
->setName('1-2-3-4')
21+
->setSku('testsku')
22+
->setPrice(10)
23+
->setTaxClassId(0)
24+
->setVisibility(Visibility::VISIBILITY_BOTH)
25+
->setStatus(Status::STATUS_ENABLED)
26+
->setStockData(
27+
[
28+
'use_config_manage_stock' => 1,
29+
'qty' => 100,
30+
'is_qty_decimal' => 0,
31+
'is_in_stock' => 1,
32+
]
33+
);
34+
$productRepository->save($product);
35+
36+
$product = Bootstrap::getObjectManager()->create(Product::class);
37+
$product->setTypeId(Type::TYPE_SIMPLE)
38+
->setAttributeSetId(4)
39+
->setWebsiteIds([1])
40+
->setName('test')
41+
->setSku('1-2-3-4')
42+
->setPrice(10)
43+
->setTaxClassId(0)
44+
->setVisibility(Visibility::VISIBILITY_BOTH)
45+
->setStatus(Status::STATUS_ENABLED)
46+
->setStockData(
47+
[
48+
'use_config_manage_stock' => 1,
49+
'qty' => 100,
50+
'is_qty_decimal' => 0,
51+
'is_in_stock' => 1,
52+
]
53+
);
54+
$productRepository->save($product);

0 commit comments

Comments
 (0)