Skip to content

Commit e282654

Browse files
committed
ACP2E-2119: address CR comments
1 parent 4a6603e commit e282654

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

app/code/Magento/CatalogGraphQl/DataProvider/Product/SearchCriteriaBuilder.php

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class SearchCriteriaBuilder
6666
/**
6767
* @var SearchConfig
6868
*/
69-
private mixed $config;
69+
private SearchConfig $searchConfig;
7070

7171
/**
7272
* @param Builder $builder
@@ -76,7 +76,7 @@ class SearchCriteriaBuilder
7676
* @param Visibility $visibility
7777
* @param SortOrderBuilder|null $sortOrderBuilder
7878
* @param Config|null $eavConfig
79-
* @param SearchConfig|null $config
79+
* @param SearchConfig|null $searchConfig
8080
*/
8181
public function __construct(
8282
Builder $builder,
@@ -86,7 +86,7 @@ public function __construct(
8686
Visibility $visibility,
8787
SortOrderBuilder $sortOrderBuilder = null,
8888
Config $eavConfig = null,
89-
SearchConfig $config = null
89+
SearchConfig $searchConfig = null
9090
) {
9191
$this->scopeConfig = $scopeConfig;
9292
$this->filterBuilder = $filterBuilder;
@@ -95,7 +95,7 @@ public function __construct(
9595
$this->visibility = $visibility;
9696
$this->sortOrderBuilder = $sortOrderBuilder ?? ObjectManager::getInstance()->get(SortOrderBuilder::class);
9797
$this->eavConfig = $eavConfig ?? ObjectManager::getInstance()->get(Config::class);
98-
$this->config = $config ?? ObjectManager::getInstance()->get(SearchConfig::class);
98+
$this->searchConfig = $searchConfig ?? ObjectManager::getInstance()->get(SearchConfig::class);
9999
}
100100

101101
/**
@@ -108,7 +108,11 @@ public function __construct(
108108
*/
109109
public function build(array $args, bool $includeAggregation): SearchCriteriaInterface
110110
{
111-
$matchTypes = $this->getMatchType($args);
111+
$matchTypes = [];
112+
if (isset($args['filter'])) {
113+
$matchTypes = $this->getPartialMatchFilters($args);
114+
$args = $this->removeMatchTypeFromArguments($args);
115+
}
112116
$searchCriteria = $this->builder->build('products', $args);
113117
$isSearch = isset($args['search']);
114118
$this->updateRangeFilters($searchCriteria);
@@ -150,19 +154,22 @@ public function build(array $args, bool $includeAggregation): SearchCriteriaInte
150154
* Update dynamically the search match type based on requested params
151155
*
152156
* @param string $requestName
153-
* @param array $matchType
157+
* @param array $matchTypes
154158
* @return void
155159
*/
156-
private function updateMatchTypeRequestConfig(string $requestName, array $matchType): void
160+
private function updateMatchTypeRequestConfig(string $requestName, array $matchTypes): void
157161
{
158-
$data = $this->config->get($requestName);
162+
$data = $this->searchConfig->get($requestName);
159163
foreach ($data['queries'] as $queryName => $match) {
160-
if (isset($matchType[$queryName]) && $matchType[$queryName] === 'PARTIAL') {
161-
$match['match'][0]['matchCondition'] = 'match_phrase_prefix';
164+
$attributeName = str_replace('_query', '', $queryName);
165+
if (array_key_exists($attributeName, $matchTypes)) {
166+
foreach ($match as $index => $matchItem) {
167+
$match[$index]['matchCondition'] = 'match_phrase_prefix';
168+
}
162169
$data['queries'][$queryName] = $match;
163170
}
164171
}
165-
$this->config->merge([$requestName => $data]);
172+
$this->searchConfig->merge([$requestName => $data]);
166173
}
167174

168175
/**
@@ -171,22 +178,38 @@ private function updateMatchTypeRequestConfig(string $requestName, array $matchT
171178
* @param array $args
172179
* @return array
173180
*/
174-
private function getMatchType(array &$args): array
181+
private function getPartialMatchFilters(array $args): array
175182
{
176183
$matchType = [];
177-
if (isset($args['filter'])) {
178-
foreach ($args['filter'] as $fieldName => $conditions) {
179-
foreach ($conditions as $filter => $value) {
180-
if ($filter === 'match_type') {
181-
$matchType[$fieldName.'_query'] = $value;
182-
unset($args['filter'][$fieldName][$filter]);
183-
}
184+
foreach ($args['filter'] as $fieldName => $conditions) {
185+
foreach ($conditions as $filter => $value) {
186+
if ($filter === 'match_type') {
187+
$matchType[$fieldName] = $value;
184188
}
185189
}
186190
}
187191
return $matchType;
188192
}
189193

194+
/**
195+
* Remove the match_type to avoid search criteria containing it
196+
*
197+
* @param array $args
198+
* @return array
199+
*/
200+
private function removeMatchTypeFromArguments(array $args): array
201+
{
202+
foreach ($args['filter'] as $fieldName => $conditions) {
203+
foreach ($conditions as $filter => $value) {
204+
if ($filter === 'match_type') {
205+
unset($args['filter'][$fieldName][$filter]);
206+
}
207+
}
208+
}
209+
210+
return $args;
211+
}
212+
190213
/**
191214
* Add filter by visibility
192215
*

app/code/Magento/GraphQl/etc/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ input FilterRangeTypeInput @doc(description: "Defines a filter that matches a ra
7777

7878
input FilterMatchTypeInput @doc(description: "Defines a filter that performs a fuzzy search.") {
7979
match: String @doc(description: "Use this attribute to fuzzy match the specified string. For example, to filter on a specific SKU, specify a value such as `24-MB01`.")
80-
match_type: FilterMatchTypeEnum @doc(description: "Filter match type for providing results. Possible values FULL or PARTIAL.")
80+
match_type: FilterMatchTypeEnum @doc(description: "Filter match type for fine-tuned results. Possible values FULL or PARTIAL. If match_type is not provided, returned results will default to FULL match.")
8181
}
8282

8383
enum FilterMatchTypeEnum {

0 commit comments

Comments
 (0)