@@ -109,9 +109,9 @@ public function __construct(
109
109
*/
110
110
public function build (array $ args , bool $ includeAggregation ): SearchCriteriaInterface
111
111
{
112
- $ matchTypes = [];
112
+ $ partialMatchFilters = [];
113
113
if (isset ($ args ['filter ' ])) {
114
- $ matchTypes = $ this ->getPartialMatchFilters ($ args );
114
+ $ partialMatchFilters = $ this ->getPartialMatchFilters ($ args );
115
115
$ args = $ this ->removeMatchTypeFromArguments ($ args );
116
116
}
117
117
$ searchCriteria = $ this ->builder ->build ('products ' , $ args );
@@ -130,8 +130,8 @@ public function build(array $args, bool $includeAggregation): SearchCriteriaInte
130
130
}
131
131
$ searchCriteria ->setRequestName ($ requestName );
132
132
133
- if (count ($ matchTypes )) {
134
- $ this ->updateMatchTypeRequestConfig ($ requestName , $ matchTypes );
133
+ if (count ($ partialMatchFilters )) {
134
+ $ this ->updateMatchTypeRequestConfig ($ requestName , $ partialMatchFilters );
135
135
}
136
136
137
137
if ($ isSearch ) {
@@ -155,20 +155,18 @@ public function build(array $args, bool $includeAggregation): SearchCriteriaInte
155
155
* Update dynamically the search match type based on requested params
156
156
*
157
157
* @param string $requestName
158
- * @param array $matchTypes
158
+ * @param array $partialMatchFilters
159
159
* @return void
160
160
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
161
161
*/
162
- private function updateMatchTypeRequestConfig (string $ requestName , array $ matchTypes ): void
162
+ private function updateMatchTypeRequestConfig (string $ requestName , array $ partialMatchFilters ): void
163
163
{
164
164
$ data = $ this ->searchConfig ->get ($ requestName );
165
- foreach ($ data ['queries ' ] as $ queryName => $ match ) {
166
- $ attributeName = str_replace ('_query ' , '' , $ queryName );
167
- if (isset ($ match ['match ' ]) && in_array ($ attributeName , $ matchTypes , true )) {
168
- foreach ($ match ['match ' ] as $ index => $ matchItem ) {
169
- $ match ['match ' ][$ index ]['matchCondition ' ] = 'match_phrase_prefix ' ;
165
+ foreach ($ data ['queries ' ] as $ queryName => $ query ) {
166
+ foreach ($ query ['match ' ] ?? [] as $ index => $ matchItem ) {
167
+ if (in_array ($ matchItem ['field ' ] ?? null , $ partialMatchFilters , true )) {
168
+ $ data ['queries ' ][$ queryName ]['match ' ][$ index ]['matchCondition ' ] = 'match_phrase_prefix ' ;
170
169
}
171
- $ data ['queries ' ][$ queryName ] = $ match ;
172
170
}
173
171
}
174
172
$ this ->searchConfig ->merge ([$ requestName => $ data ]);
@@ -182,15 +180,13 @@ private function updateMatchTypeRequestConfig(string $requestName, array $matchT
182
180
*/
183
181
private function getPartialMatchFilters (array $ args ): array
184
182
{
185
- $ matchType = [];
183
+ $ partialMatchFilters = [];
186
184
foreach ($ args ['filter ' ] as $ fieldName => $ conditions ) {
187
- foreach ($ conditions as $ filter => $ value ) {
188
- if ($ filter === 'match_type ' && $ value === 'PARTIAL ' ) {
189
- $ matchType [$ fieldName ] = $ fieldName ;
190
- }
185
+ if (isset ($ conditions ['match_type ' ]) && $ conditions ['match_type ' ] === 'PARTIAL ' ) {
186
+ $ partialMatchFilters [] = $ fieldName ;
191
187
}
192
188
}
193
- return $ matchType ;
189
+ return $ partialMatchFilters ;
194
190
}
195
191
196
192
/**
@@ -202,11 +198,9 @@ private function getPartialMatchFilters(array $args): array
202
198
*/
203
199
private function removeMatchTypeFromArguments (array $ args ): array
204
200
{
205
- foreach ($ args ['filter ' ] as $ fieldName => $ conditions ) {
206
- foreach ($ conditions as $ filter => $ value ) {
207
- if ($ filter === 'match_type ' ) {
208
- unset($ args ['filter ' ][$ fieldName ][$ filter ]);
209
- }
201
+ foreach ($ args ['filter ' ] as &$ conditions ) {
202
+ if (isset ($ conditions ['match_type ' ])) {
203
+ unset($ conditions ['match_type ' ]);
210
204
}
211
205
}
212
206
0 commit comments