17
17
class Collection extends \Magento \Catalog \Model \ResourceModel \Product \Collection implements
18
18
\Magento \Search \Model \SearchCollectionInterface
19
19
{
20
+ private const INDEX_USAGE_ENFORCEMENTS = [
21
+ 'catalog_product_entity_text ' => 'CATALOG_PRODUCT_ENTITY_TEXT_ROW_ID_ATTRIBUTE_ID_STORE_ID '
22
+ ];
23
+
20
24
/**
21
25
* Attribute collection
22
26
*
@@ -197,6 +201,21 @@ protected function _hasAttributeOptionsAndSearchable($attribute)
197
201
return false ;
198
202
}
199
203
204
+ /**
205
+ * Prepare table names for the index enforcements
206
+ *
207
+ * @return array
208
+ */
209
+ private function prepareIndexEnforcements () : array
210
+ {
211
+ $ result = [];
212
+ foreach (self ::INDEX_USAGE_ENFORCEMENTS as $ table => $ index ) {
213
+ $ table = $ this ->getTable ($ table );
214
+ $ result [$ table ] = $ index ;
215
+ }
216
+ return $ result ;
217
+ }
218
+
200
219
/**
201
220
* Retrieve SQL for search entities
202
221
*
@@ -208,6 +227,7 @@ protected function _getSearchEntityIdsSql($query, $searchOnlyInCurrentStore = tr
208
227
{
209
228
$ tables = [];
210
229
$ selects = [];
230
+ $ preparedIndexEnforcements = $ this ->prepareIndexEnforcements ();
211
231
212
232
$ likeOptions = ['position ' => 'any ' ];
213
233
@@ -249,23 +269,53 @@ protected function _getSearchEntityIdsSql($query, $searchOnlyInCurrentStore = tr
249
269
250
270
$ ifValueId = $ this ->getConnection ()->getIfNullSql ('t2.value ' , 't1.value ' );
251
271
foreach ($ tables as $ table => $ attributeIds ) {
252
- $ selects [] = $ this ->getConnection ()->select ()->from (
253
- ['t1 ' => $ table ],
254
- $ linkField
255
- )->joinLeft (
256
- ['t2 ' => $ table ],
257
- $ joinCondition ,
258
- []
259
- )->where (
260
- 't1.attribute_id IN (?) ' ,
261
- $ attributeIds ,
262
- \Zend_Db::INT_TYPE
263
- )->where (
264
- 't1.store_id = ? ' ,
265
- 0
266
- )->where (
267
- $ this ->_resourceHelper ->getCILike ($ ifValueId , $ this ->_searchQuery , $ likeOptions )
268
- );
272
+ if (!empty ($ preparedIndexEnforcements [$ table ])) {
273
+ $ condition1 = $ this ->_conn ->quoteInto (
274
+ '`t1`.`attribute_id` IN (?) ' ,
275
+ $ attributeIds ,
276
+ \Zend_Db::INT_TYPE
277
+ );
278
+ $ condition2 = '`t1`.`store_id` = 0 ' ;
279
+ $ condition3 = $ this ->_conn ->quoteInto (
280
+ 'IFNULL(`t2`.`value`, `t1`.`value`) LIKE ? ' ,
281
+ $ this ->_resourceHelper ->addLikeEscape ($ this ->_searchQuery , $ likeOptions )
282
+ );
283
+
284
+ //force index statement not implemented in framework
285
+ // phpcs:ignore Magento2.SQL.RawQuery
286
+ $ select = sprintf (
287
+ 'SELECT `t1`.`%s` FROM `%s` AS `t1` FORCE INDEX(%s) LEFT JOIN `%s` AS `t2`
288
+ ON %s WHERE %s AND %s AND %s ' ,
289
+ $ linkField ,
290
+ $ table ,
291
+ $ preparedIndexEnforcements [$ table ],
292
+ $ table ,
293
+ $ joinCondition ,
294
+ $ condition1 ,
295
+ $ condition2 ,
296
+ $ condition3
297
+ );
298
+ } else {
299
+ $ select = $ this ->getConnection ()->select ();
300
+ $ select ->from (
301
+ ['t1 ' => $ table ],
302
+ $ linkField
303
+ )->joinLeft (
304
+ ['t2 ' => $ table ],
305
+ $ joinCondition ,
306
+ []
307
+ )->where (
308
+ 't1.attribute_id IN (?) ' ,
309
+ $ attributeIds ,
310
+ \Zend_Db::INT_TYPE
311
+ )->where (
312
+ 't1.store_id = ? ' ,
313
+ 0
314
+ )->where (
315
+ $ this ->_resourceHelper ->getCILike ($ ifValueId , $ this ->_searchQuery , $ likeOptions )
316
+ );
317
+ }
318
+ $ selects [] = $ select ;
269
319
}
270
320
271
321
$ sql = $ this ->_getSearchInOptionSql ($ query );
0 commit comments