Skip to content

Commit 7e6e098

Browse files
authored
Merge pull request #3846 from magento-epam/EPAM-PR-43
- fixed MAGETWO-58764: [GitHub] Minimal Query Length For Catalog Search #6681
2 parents 5a8ef03 + dc41ba8 commit 7e6e098

File tree

13 files changed

+253
-7
lines changed

13 files changed

+253
-7
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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\CatalogGraphQl\Model\Search\Adapter\Mysql\Query\Builder;
9+
10+
use Magento\Framework\DB\Helper\Mysql\Fulltext;
11+
use Magento\Framework\Search\Adapter\Mysql\Field\ResolverInterface;
12+
use Magento\Framework\Search\Adapter\Mysql\Query\Builder\Match as BuilderMatch;
13+
use Magento\Framework\Search\Adapter\Preprocessor\PreprocessorInterface;
14+
use Magento\Framework\Search\Request\Query\BoolExpression;
15+
use Magento\Search\Helper\Data;
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
class Match extends BuilderMatch
21+
{
22+
/**
23+
* @var Data
24+
*/
25+
private $searchHelper;
26+
27+
/**
28+
* @param ResolverInterface $resolver
29+
* @param Fulltext $fulltextHelper
30+
* @param Data $searchHelper
31+
* @param string $fulltextSearchMode
32+
* @param PreprocessorInterface[] $preprocessors
33+
*/
34+
public function __construct(
35+
ResolverInterface $resolver,
36+
Fulltext $fulltextHelper,
37+
Data $searchHelper,
38+
$fulltextSearchMode = Fulltext::FULLTEXT_MODE_BOOLEAN,
39+
array $preprocessors = []
40+
) {
41+
parent::__construct($resolver, $fulltextHelper, $fulltextSearchMode, $preprocessors);
42+
$this->searchHelper = $searchHelper;
43+
}
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
protected function prepareQuery($queryValue, $conditionType)
49+
{
50+
$replaceSymbols = str_split(self::SPECIAL_CHARACTERS, 1);
51+
$queryValue = str_replace($replaceSymbols, ' ', $queryValue);
52+
foreach ($this->preprocessors as $preprocessor) {
53+
$queryValue = $preprocessor->process($queryValue);
54+
}
55+
56+
$stringPrefix = '';
57+
if ($conditionType === BoolExpression::QUERY_CONDITION_MUST) {
58+
$stringPrefix = '+';
59+
} elseif ($conditionType === BoolExpression::QUERY_CONDITION_NOT) {
60+
$stringPrefix = '-';
61+
}
62+
63+
$queryValues = explode(' ', $queryValue);
64+
65+
foreach ($queryValues as $queryKey => $queryValue) {
66+
if (empty($queryValue)) {
67+
unset($queryValues[$queryKey]);
68+
} else {
69+
$stringSuffix = $this->searchHelper->getMinQueryLength() > strlen($queryValue) ? '' : '*';
70+
$queryValues[$queryKey] = $stringPrefix . $queryValue . $stringSuffix;
71+
}
72+
}
73+
74+
$queryValue = implode(' ', $queryValues);
75+
76+
return $queryValue;
77+
}
78+
}

app/code/Magento/CatalogGraphQl/etc/graphql/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,6 @@
7878
</argument>
7979
</arguments>
8080
</virtualType>
81+
<preference for="Magento\Framework\Search\Adapter\Mysql\Query\Builder\Match"
82+
type="Magento\CatalogGraphQl\Model\Search\Adapter\Mysql\Query\Builder\Match" />
8183
</config>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
11+
<actionGroup name="SetMinimalQueryLengthActionGroup">
12+
<arguments>
13+
<argument name="minLength" type="string" defaultValue="1"/>
14+
</arguments>
15+
<amOnPage url="{{AdminCatalogSearchConfigurationPage.url}}" stepKey="navigateToConfigurationPage"/>
16+
<waitForPageLoad stepKey="wait1"/>
17+
<scrollTo selector="{{AdminCatalogSearchConfigurationSection.catalogSearchTab}}" stepKey="scrollToCatalogSearchTab"/>
18+
<conditionalClick selector="{{AdminCatalogSearchConfigurationSection.catalogSearchTab}}" dependentSelector="{{AdminCatalogSearchConfigurationSection.minQueryLength}}" visible="false" stepKey="expandCatalogSearchTab"/>
19+
<waitForElementVisible selector="{{AdminCatalogSearchConfigurationSection.minQueryLength}}" stepKey="waitTabToCollapse"/>
20+
<see userInput="{{MinMaxQueryLength.Hint}}" selector="{{AdminCatalogSearchConfigurationSection.minQueryLengthHint}}" stepKey="seeHint1"/>
21+
<see userInput="{{MinMaxQueryLength.Hint}}" selector="{{AdminCatalogSearchConfigurationSection.maxQueryLengthHint}}" stepKey="seeHint2"/>
22+
<uncheckOption selector="{{AdminCatalogSearchConfigurationSection.minQueryLengthInherit}}" stepKey="uncheckSystemValue"/>
23+
<fillField selector="{{AdminCatalogSearchConfigurationSection.minQueryLength}}" userInput="{{minLength}}" stepKey="setMinQueryLength"/>
24+
<click selector="{{AdminCatalogSearchConfigurationSection.catalogSearchTab}}" stepKey="collapseTab"/>
25+
<click selector="{{ContentManagementSection.Save}}" stepKey="saveConfig"/>
26+
<waitForPageLoad stepKey="waitForConfigSaved"/>
27+
<see userInput="You saved the configuration." stepKey="seeSuccessMessage"/>
28+
</actionGroup>
29+
</actionGroups>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<entities xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:DataGenerator/etc/dataProfileSchema.xsd">
11+
<entity name="SetMinQueryLengthToDefault" type="catalog_search_config_def">
12+
<requiredEntity type="enable">DefaultMinQueryLength</requiredEntity>
13+
</entity>
14+
<entity name="UncheckMinQueryLengthAndSet" type="catalog_search_config_query_length">
15+
<requiredEntity type="number">SetMinQueryLengthToOne</requiredEntity>
16+
</entity>
17+
<entity name="DefaultMinQueryLength" type="enable">
18+
<data key="inherit">true</data>
19+
</entity>
20+
<entity name="DefaultMinQueryLengthDisable" type="enable">
21+
<data key="inherit">0</data>
22+
</entity>
23+
<entity name="SetMinQueryLengthToOne" type="number">
24+
<data key="value">1</data>
25+
</entity>
26+
27+
</entities>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<entities xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:DataGenerator/etc/dataProfileSchema.xsd">
11+
<entity name="MinMaxQueryLength" type="constant">
12+
<data key="Hint">This value must be compatible with the corresponding setting in the configured search engine</data>
13+
</entity>
14+
</entities>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<operations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:DataGenerator/etc/dataOperation.xsd">
10+
<operation name="CatalogSearchConfigDefault" dataType="catalog_search_config_def" type="create" auth="adminFormKey" url="/admin/system_config/save/section/catalog/" method="POST">
11+
<object key="groups" dataType="catalog_search_config_def">
12+
<object key="search" dataType="catalog_search_config_def">
13+
<object key="fields" dataType="catalog_search_config_def">
14+
<object key="min_query_length" dataType="enable">
15+
<field key="inherit">boolean</field>
16+
</object>
17+
</object>
18+
</object>
19+
</object>
20+
</operation>
21+
<operation name="CatalogSearchConfigQueryLength" dataType="catalog_search_config_query_length" type="create" auth="adminFormKey" url="/admin/system_config/save/section/catalog/" method="POST">
22+
<object key="groups" dataType="catalog_search_config_query_length">
23+
<object key="search" dataType="catalog_search_config_query_length">
24+
<object key="fields" dataType="catalog_search_config_query_length">
25+
<object key="min_query_length" dataType="number">
26+
<field key="value">integer</field>
27+
</object>
28+
</object>
29+
</object>
30+
</object>
31+
</operation>
32+
</operations>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<sections xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/SectionObject.xsd">
11+
<section name="AdminCatalogSearchConfigurationSection">
12+
<element name="minQueryLength" type="input" selector="#catalog_search_min_query_length"/>
13+
<element name="minQueryLengthInherit" type="checkbox" selector="#catalog_search_min_query_length_inherit"/>
14+
<element name="minQueryLengthHint" type="text" selector="#row_catalog_search_min_query_length .value span"/>
15+
<element name="maxQueryLengthHint" type="text" selector="#row_catalog_search_max_query_length .value span"/>
16+
</section>
17+
</sections>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
11+
<test name="MinimalQueryLengthForCatalogSearchTest">
12+
<annotations>
13+
<features value="CatalogSearch"/>
14+
<title value="Minimal query length for catalog search"/>
15+
<description value="Minimal query length for catalog search"/>
16+
<severity value="AVERAGE"/>
17+
<testCaseId value="MC-6325"/>
18+
<useCaseId value="MAGETWO-58764"/>
19+
<group value="CatalogSearch"/>
20+
</annotations>
21+
<before>
22+
<createData entity="ApiCategory" stepKey="createCategory"/>
23+
<createData entity="ApiSimpleProduct" stepKey="createProduct">
24+
<requiredEntity createDataKey="createCategory"/>
25+
</createData>
26+
<actionGroup ref="LoginAsAdmin" stepKey="loginAsAdmin"/>
27+
</before>
28+
<after>
29+
<deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
30+
<deleteData createDataKey="createProduct" stepKey="deleteProduct"/>
31+
<createData entity="SetMinQueryLengthToDefault" stepKey="setMinimumQueryLengthToDefault"/>
32+
<actionGroup ref="logout" stepKey="logout"/>
33+
</after>
34+
<actionGroup ref="SetMinimalQueryLengthActionGroup" stepKey="setMinQueryLength"/>
35+
<comment userInput="Go to Storefront and search for product" stepKey="searchProdUsingMinQueryLength"/>
36+
<amOnPage url="{{StorefrontHomePage.url}}" stepKey="goToHomePage"/>
37+
<fillField selector="{{StorefrontQuickSearchResultsSection.searchTextBox}}" userInput="s" stepKey="fillAttribute"/>
38+
<waitForPageLoad stepKey="waitForSearchTextBox"/>
39+
<click selector="{{StorefrontQuickSearchResultsSection.searchTextBoxButton}}" stepKey="clickSearchTextBoxButton"/>
40+
<waitForPageLoad stepKey="waitForSearch"/>
41+
<see selector="{{StorefrontCategoryMainSection.productName}}" userInput="$$createProduct.name$$" stepKey="seeProductNameInCategoryPage"/>
42+
</test>
43+
</tests>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
<field id="engine" canRestore="1">
2020
<backend_model>Magento\CatalogSearch\Model\Adminhtml\System\Config\Backend\Engine</backend_model>
2121
</field>
22-
<field id="min_query_length" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
22+
<field id="min_query_length" translate="label comment" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
2323
<label>Minimal Query Length</label>
2424
<validate>validate-digits</validate>
25+
<comment>This value must be compatible with the corresponding setting in the configured search engine. Be aware: a low query length limit may cause the performance impact.</comment>
2526
</field>
26-
<field id="max_query_length" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
27+
<field id="max_query_length" translate="label comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
2728
<label>Maximum Query Length</label>
2829
<validate>validate-digits</validate>
30+
<comment>This value must be compatible with the corresponding setting in the configured search engine.</comment>
2931
</field>
3032
<field id="max_count_cacheable_search_terms" translate="label comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
3133
<label>Number of top search results to cache</label>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</seo>
1414
<search>
1515
<engine>mysql</engine>
16-
<min_query_length>1</min_query_length>
16+
<min_query_length>3</min_query_length>
1717
<max_query_length>128</max_query_length>
1818
<max_count_cacheable_search_terms>100</max_count_cacheable_search_terms>
1919
<autocomplete_limit>8</autocomplete_limit>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ name,name
3838
"Maximum Query Length","Maximum Query Length"
3939
"Rebuild Catalog product fulltext search index","Rebuild Catalog product fulltext search index"
4040
"Please enter a valid price range.","Please enter a valid price range."
41+
"This value must be compatible with the corresponding setting in the configured search engine. Be aware: a low query length limit may cause the performance impact.","This value must be compatible with the corresponding setting in the configured search engine. Be aware: a low query length limit may cause the performance impact."
42+
"This value must be compatible with the corresponding setting in the configured search engine.","This value must be compatible with the corresponding setting in the configured search engine."

dev/tests/functional/tests/app/Magento/CatalogSearch/Test/TestCase/SearchEntityResultsTest.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@
5757
<variation name="SearchEntityResultsTestVariation12" summary="Search for simple product name using 2 symbols query length" ticketId="MAGETWO-36542">
5858
<data name="catalogSearch/data/query_text/value" xsi:type="string">catalogProductSimple::default::name</data>
5959
<data name="queryLength" xsi:type="string">2</data>
60-
<constraint name="Magento\CatalogSearch\Test\Constraint\AssertCatalogSearchNoResultMessage" />
61-
<constraint name="Magento\CatalogSearch\Test\Constraint\AssertCatalogSearchNoResult" />
60+
<constraint name="Magento\CatalogSearch\Test\Constraint\AssertProductCanBeOpenedFromSearchResult" />
6261
</variation>
6362
<variation name="SearchEntityResultsTestVariation13" summary="Search for simple product name using 3 symbols query length" ticketId="MAGETWO-36542">
64-
<data name="issue" xsi:type="string">MAGETWO-65509: [FT] Magento\CatalogSearch\Test\TestCase\SearchEntityResultsTest fails on Jenkins</data>
6563
<data name="tag" xsi:type="string">stable:no</data>
6664
<data name="catalogSearch/data/query_text/value" xsi:type="string">catalogProductSimple::default::name</data>
6765
<data name="queryLength" xsi:type="string">3</data>

lib/internal/Magento/Framework/Search/Adapter/Mysql/Query/Builder/Match.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Framework\Search\Adapter\Mysql\Query\Builder;
79

810
use Magento\Framework\DB\Helper\Mysql\Fulltext;
@@ -28,7 +30,7 @@ class Match implements QueryInterface
2830
*/
2931
const SPECIAL_CHARACTERS = '-+~/\\<>\'":*$#@()!,.?`=%&^';
3032

31-
const MINIMAL_CHARACTER_LENGTH = 3;
33+
const MINIMAL_CHARACTER_LENGTH = 1;
3234

3335
/**
3436
* @var string[]

0 commit comments

Comments
 (0)