Skip to content

Commit d6d666b

Browse files
committed
MC-32378: Set up ElasticSearch so that an "AND" query is performed when searching
1 parent 6284b10 commit d6d666b

File tree

7 files changed

+179
-4
lines changed

7 files changed

+179
-4
lines changed

app/code/Magento/Elasticsearch/Model/Adapter/FieldMapper/AddDefaultSearchField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class AddDefaultSearchField implements FieldsMappingPreprocessorInterface
2121
/**
2222
* Add default search field (catch all field) to the fields.
2323
*
24-
* Emulates catch all field (_all) for elasticsearch version 6.0+
24+
* Emulates catch all field (_all) for elasticsearch
2525
*
2626
* @param array $mapping
2727
* @return array

app/code/Magento/Elasticsearch/Model/Adapter/FieldMapper/CopySearchableFieldsToSearchField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CopySearchableFieldsToSearchField implements FieldsMappingPreprocessorInte
2121
/**
2222
* Add "copy_to" parameter for default search field to index fields.
2323
*
24-
* Emulates catch all field (_all) for elasticsearch version 6.0+
24+
* Emulates catch all field (_all) for elasticsearch
2525
*
2626
* @param array $mapping
2727
* @return array

app/code/Magento/Elasticsearch/i18n/en_US.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
"Elasticsearch HTTP Password","Elasticsearch HTTP Password"
1111
"Elasticsearch Server Timeout","Elasticsearch Server Timeout"
1212
"Test Connection","Test Connection"
13-
"Minimum terms to match","Minimum terms to match"
13+
"Minimum Terms to Match","Minimum Terms to Match"

app/code/Magento/Elasticsearch7/etc/adminhtml/system.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
<field id="engine">elasticsearch7</field>
8080
</depends>
8181
</field>
82-
<field id="elasticsearch7_minimum_should_match" translate="label" type="text" sortOrder="93" showInDefault="1">
82+
<field id="elasticsearch7_minimum_should_match" translate="label" type="text" sortOrder="89" showInDefault="1">
8383
<label>Minimum Terms to Match</label>
8484
<depends>
8585
<field id="engine">elasticsearch7</field>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Elasticsearch\Controller;
9+
10+
use Magento\TestFramework\TestCase\AbstractController;
11+
12+
class QuickSearchTest extends AbstractController
13+
{
14+
/**
15+
* Tests quick search with "Minimum Terms to Match" sets to "100%".
16+
*
17+
* @magentoAppArea frontend
18+
* @magentoDbIsolation disabled
19+
* @magentoConfigFixture current_store catalog/search/elasticsearch7_minimum_should_match 100%
20+
* @magentoConfigFixture current_store catalog/search/elasticsearch6_minimum_should_match 100%
21+
* @magentoDataFixture Magento/Elasticsearch/_files/products_for_search.php
22+
* @magentoDataFixture Magento/CatalogSearch/_files/full_reindex.php
23+
*/
24+
public function testQuickSearchWithImprovedPriceRangeCalculation()
25+
{
26+
$this->dispatch('/catalogsearch/result/?q=24+MB04');
27+
$responseBody = $this->getResponse()->getBody();
28+
$this->assertContains('search product 2', $responseBody);
29+
$this->assertNotContains('search product 1', $responseBody);
30+
}
31+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
include __DIR__ . '/../../Catalog/_files/category.php';
9+
10+
use Magento\Catalog\Api\CategoryLinkManagementInterface;
11+
use Magento\Catalog\Model\Product;
12+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
13+
use Magento\Catalog\Model\Product\Visibility;
14+
15+
$products = [
16+
[
17+
'type' => 'simple',
18+
'id' => 201,
19+
'name' => 'search product 1',
20+
'sku' => '24 MB06',
21+
'status' => Status::STATUS_ENABLED,
22+
'visibility' => Visibility::VISIBILITY_BOTH,
23+
'attribute_set' => 4,
24+
'website_ids' => [1],
25+
'price' => 10,
26+
'category_id' => 333,
27+
'meta_title' => 'Key Title',
28+
'meta_keyword' => 'meta keyword',
29+
'meta_description' => 'meta description',
30+
],
31+
[
32+
'type' => 'simple',
33+
'id' => 202,
34+
'name' => 'search product 2',
35+
'sku' => '24 MB04',
36+
'status' => Status::STATUS_ENABLED,
37+
'visibility' => Visibility::VISIBILITY_BOTH,
38+
'attribute_set' => 4,
39+
'website_ids' => [1],
40+
'price' => 10,
41+
'category_id' => 333,
42+
'meta_title' => 'Last Title',
43+
'meta_keyword' => 'meta keyword',
44+
'meta_description' => 'meta description',
45+
],
46+
[
47+
'type' => 'simple',
48+
'id' => 203,
49+
'name' => 'search product 3',
50+
'sku' => '24 MB02',
51+
'status' => Status::STATUS_ENABLED,
52+
'visibility' => Visibility::VISIBILITY_BOTH,
53+
'attribute_set' => 4,
54+
'website_ids' => [1],
55+
'price' => 20,
56+
'category_id' => 333,
57+
'meta_title' => 'First Title',
58+
'meta_keyword' => 'meta keyword',
59+
'meta_description' => 'meta description',
60+
],
61+
[
62+
'type' => 'simple',
63+
'id' => 204,
64+
'name' => 'search product 4',
65+
'sku' => '24 MB01',
66+
'status' => Status::STATUS_ENABLED,
67+
'visibility' => Visibility::VISIBILITY_BOTH,
68+
'attribute_set' => 4,
69+
'website_ids' => [1],
70+
'price' => 30,
71+
'category_id' => 333,
72+
'meta_title' => 'A title',
73+
'meta_keyword' => 'meta keyword',
74+
'meta_description' => 'meta description',
75+
],
76+
];
77+
78+
/** @var CategoryLinkManagementInterface $categoryLinkManagement */
79+
$categoryLinkManagement = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
80+
->create(CategoryLinkManagementInterface::class);
81+
82+
$categoriesToAssign = [];
83+
84+
foreach ($products as $data) {
85+
/** @var $product Product */
86+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(Product::class);
87+
$product
88+
->setTypeId($data['type'])
89+
->setId($data['id'])
90+
->setAttributeSetId($data['attribute_set'])
91+
->setWebsiteIds($data['website_ids'])
92+
->setName($data['name'])
93+
->setSku($data['sku'])
94+
->setPrice($data['price'])
95+
->setMetaTitle($data['meta_title'])
96+
->setMetaKeyword($data['meta_keyword'])
97+
->setMetaDescription($data['meta_keyword'])
98+
->setVisibility($data['visibility'])
99+
->setStatus($data['status'])
100+
->setStockData(['use_config_manage_stock' => 0])
101+
->save();
102+
103+
$categoriesToAssign[$data['sku']][] = $data['category_id'];
104+
}
105+
106+
foreach ($categoriesToAssign as $sku => $categoryIds) {
107+
$categoryLinkManagement->assignProductToCategories($sku, $categoryIds);
108+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Magento\Framework\Registry;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
13+
Bootstrap::getInstance()->getInstance()->reinitialize();
14+
15+
/** @var Registry $registry */
16+
$registry = Bootstrap::getObjectManager()->get(Registry::class);
17+
18+
$registry->unregister('isSecureArea');
19+
$registry->register('isSecureArea', true);
20+
21+
/** @var ProductRepositoryInterface $productRepository */
22+
$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
23+
24+
$productSkus = ['24 MB06', '24 MB04', '24 MB02', '24 MB01'];
25+
foreach ($productSkus as $sku) {
26+
try {
27+
$product = $productRepository->get($sku, false, null, true);
28+
$productRepository->delete($product);
29+
} catch (NoSuchEntityException $e) {
30+
}
31+
}
32+
33+
include dirname(dirname(__DIR__)) . '/Catalog/_files/category_rollback.php';
34+
35+
$registry->unregister('isSecureArea');
36+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)