Skip to content

Commit

Permalink
Merge pull request #1021 from Hlavtox/implement-search-priority
Browse files Browse the repository at this point in the history
Make backport return items ordered by position
  • Loading branch information
Hlavtox authored Sep 10, 2024
2 parents 9c52edb + daf849c commit 09ac0a7
Showing 1 changed file with 40 additions and 23 deletions.
63 changes: 40 additions & 23 deletions src/Product/CoreSearchBackport.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ public function get80($expr)
$context = Context::getContext();
$db = Db::getInstance(_PS_USE_SQL_SLAVE_);

$scoreArray = [];
$fuzzyLoop = 0;
$wordCnt = 0;
$eligibleProducts2Full = [];
Expand All @@ -342,15 +343,15 @@ public function get80($expr)

$sql_param_search = Search::getSearchParamFromWord($word);
$sql = 'SELECT DISTINCT si.id_product ' .
'FROM ' . _DB_PREFIX_ . 'search_word sw ' .
'LEFT JOIN ' . _DB_PREFIX_ . 'search_index si ON sw.id_word = si.id_word ' .
'LEFT JOIN ' . _DB_PREFIX_ . 'product_shop product_shop ON (product_shop.`id_product` = si.`id_product`) ' .
'WHERE sw.id_lang = ' . (int) $context->language->id . ' ' .
'AND sw.id_shop = ' . $context->shop->id . ' ' .
'AND product_shop.`active` = 1 ' .
'AND product_shop.`visibility` IN ("both", "search") ' .
'AND product_shop.indexed = 1 ' .
'AND sw.word LIKE ';
'FROM ' . _DB_PREFIX_ . 'search_word sw ' .
'LEFT JOIN ' . _DB_PREFIX_ . 'search_index si ON sw.id_word = si.id_word ' .
'LEFT JOIN ' . _DB_PREFIX_ . 'product_shop product_shop ON (product_shop.`id_product` = si.`id_product`) ' .
'WHERE sw.id_lang = ' . (int) $context->language->id . ' ' .
'AND sw.id_shop = ' . $context->shop->id . ' ' .
'AND product_shop.`active` = 1 ' .
'AND product_shop.`visibility` IN ("both", "search") ' .
'AND product_shop.indexed = 1 ' .
'AND sw.word LIKE ';

while (!($result = $db->executeS($sql . "'" . $sql_param_search . "';", true, false))) {
if (
Expand All @@ -373,6 +374,8 @@ public function get80($expr)
} else {
$eligibleProducts2 = array_intersect($eligibleProducts2, $productIds);
}

$scoreArray[] = 'sw.word LIKE \'' . $sql_param_search . '\'';
}
$wordCnt += count($words);
if ($eligibleProducts2) {
Expand All @@ -386,27 +389,41 @@ public function get80($expr)
return [];
}

$sqlScore = '';
if (!empty($scoreArray) && is_array($scoreArray)) {
$sqlScore = ',( ' .
'SELECT SUM(weight) ' .
'FROM ' . _DB_PREFIX_ . 'search_word sw ' .
'LEFT JOIN ' . _DB_PREFIX_ . 'search_index si ON sw.id_word = si.id_word ' .
'WHERE sw.id_lang = ' . (int) $context->language->id . ' ' .
'AND sw.id_shop = ' . $context->shop->id . ' ' .
'AND si.id_product = p.id_product ' .
'AND (' . implode(' OR ', $scoreArray) . ') ' .
') position';
}

$sqlGroups = '';
if (Group::isFeatureActive()) {
$groups = FrontController::getCurrentCustomerGroups();
$sqlGroups = 'AND cg.`id_group` ' . (count($groups) ? 'IN (' . implode(',', $groups) . ')' : '=' . (int) Group::getCurrent()->id);
}

$results = $db->executeS(
'SELECT DISTINCT cp.`id_product` ' .
'FROM `' . _DB_PREFIX_ . 'category_product` cp ' .
(Group::isFeatureActive() ? 'INNER JOIN `' . _DB_PREFIX_ . 'category_group` cg ON cp.`id_category` = cg.`id_category`' : '') . ' ' .
'INNER JOIN `' . _DB_PREFIX_ . 'category` c ON cp.`id_category` = c.`id_category` ' .
'INNER JOIN `' . _DB_PREFIX_ . 'product` p ON cp.`id_product` = p.`id_product` ' .
Shop::addSqlAssociation('product', 'p', false) . ' ' .
'WHERE c.`active` = 1 ' .
'AND product_shop.`active` = 1 ' .
'AND product_shop.`visibility` IN ("both", "search") ' .
'AND product_shop.indexed = 1 ' .
'AND cp.id_product IN (' . implode(',', $eligibleProducts2Full) . ')' . $sqlGroups,
true,
false
);
'SELECT DISTINCT cp.`id_product` ' . $sqlScore . ' ' .
'FROM `' . _DB_PREFIX_ . 'category_product` cp ' .
(Group::isFeatureActive() ? 'INNER JOIN `' . _DB_PREFIX_ . 'category_group` cg ON cp.`id_category` = cg.`id_category`' : '') . ' ' .
'INNER JOIN `' . _DB_PREFIX_ . 'category` c ON cp.`id_category` = c.`id_category` ' .
'INNER JOIN `' . _DB_PREFIX_ . 'product` p ON cp.`id_product` = p.`id_product` ' .
Shop::addSqlAssociation('product', 'p', false) . ' ' .
'WHERE c.`active` = 1 ' .
'AND product_shop.`active` = 1 ' .
'AND product_shop.`visibility` IN ("both", "search") ' .
'AND product_shop.indexed = 1 ' .
'AND cp.id_product IN (' . implode(',', $eligibleProducts2Full) . ')' . $sqlGroups . '
ORDER BY position DESC, p.id_product ASC',
true,
false
);

return array_column($results, 'id_product');
}
Expand Down

0 comments on commit 09ac0a7

Please sign in to comment.