Skip to content

Commit 22a78fc

Browse files
author
Bohdan Berezhniy
committed
make search compatible with localizations
1 parent fda9678 commit 22a78fc

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

Model/ResourceModel/Post/Collection.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
2828
*/
2929
protected $_eventObject = 'blog_post_collection';
3030

31+
/**
32+
* @var string[]
33+
*/
34+
protected $_ftiCollumns = ['title', 'meta_keywords', 'meta_description', 'identifier', 'content'];
35+
3136
/**
3237
* @var \Magento\Store\Model\StoreManagerInterface
3338
*/
@@ -400,10 +405,7 @@ public function addSearchFilter($term)
400405
$tagPostIds = array_slice($tagPostIds, 0, 200);
401406
}
402407

403-
$fullExpression = '(0 ' .
404-
'+ FORMAT(MATCH (title, meta_keywords, meta_description, identifier, content) AGAINST ('
405-
. $this->getConnection()->quote($term)
406-
. '), 4) ' .
408+
$fullExpression = $this->getSearchRateExpression($term, $this->_ftiCollumns) .
407409
'+ IF(main_table.post_id IN (' . implode(',', $tagPostIds) . '), "1", "0"))';
408410

409411
$fullExpression = new \Zend_Db_Expr($fullExpression);
@@ -419,19 +421,25 @@ public function addSearchFilter($term)
419421
]
420422
);
421423

422-
$fullExpression = '(0 ' .
423-
'+ FORMAT(MATCH (title, meta_keywords, meta_description, identifier, content) AGAINST ('
424-
. $this->getConnection()->quote($term)
425-
. '), 4))';
426-
427-
$fullExpression = new \Zend_Db_Expr($fullExpression);
424+
$fullExpression = new \Zend_Db_Expr($this->getSearchRateExpression($term, $this->_ftiCollumns));
428425
$this->getSelect()->columns(['search_rate' => $fullExpression]);
426+
429427
//$this->expressionFieldsToSelect['search_rate'] = $fullExpression;
430428
}
431429

432430
return $this;
433431
}
434432

433+
/**
434+
* @param $term
435+
* @param array $columns
436+
* @return string
437+
*/
438+
public function getSearchRateExpression($term, array $columns): string
439+
{
440+
return '(0 + FORMAT(MATCH (' . implode(',', $columns) . ') AGAINST (' . $this->getConnection()->quote($term) . '), 4)) ';
441+
}
442+
435443
/**
436444
* Add tag filter to collection
437445
* @param array|int|string|\Magefan\Blog\Model\Tag $tag

0 commit comments

Comments
 (0)