diff --git a/classes/Search.php b/classes/Search.php index ac23d8778..214134b21 100644 --- a/classes/Search.php +++ b/classes/Search.php @@ -337,20 +337,20 @@ class SearchCore return $features; } - protected static function getProductsToIndex($nbLanguages, $limit = 50) - { - // Adjust the limit to get only "whole" products, in every languages (and at least one) - $limit = max(1, round($limit / $nbLanguages) * $nbLanguages); - return Db::getInstance()->executeS(' - SELECT p.id_product, pl.id_lang, pl.name pname, p.reference, p.ean13, p.upc, pl.description_short, pl.description, cl.name cname, m.name mname - FROM '._DB_PREFIX_.'product p - LEFT JOIN '._DB_PREFIX_.'product_lang pl ON p.id_product = pl.id_product - LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = p.id_category_default AND pl.id_lang = cl.id_lang) - LEFT JOIN '._DB_PREFIX_.'manufacturer m ON m.id_manufacturer = p.id_manufacturer - WHERE p.indexed = 0 - LIMIT '.(int)$limit); - } - + protected static function getProductsToIndex($nbLanguages, $limit = 50) + { + // Adjust the limit to get only "whole" products, in every languages (and at least one) + $limit = max(1, round($limit / $nbLanguages) * $nbLanguages); + return Db::getInstance()->executeS(' + SELECT p.id_product, pl.id_lang, pl.id_shop, pl.name pname, p.reference, p.ean13, p.upc, pl.description_short, pl.description, cl.name cname, m.name mname + FROM '._DB_PREFIX_.'product p + LEFT JOIN '._DB_PREFIX_.'product_lang pl ON p.id_product = pl.id_product + LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = p.id_category_default AND pl.id_lang = cl.id_lang) + LEFT JOIN '._DB_PREFIX_.'manufacturer m ON m.id_manufacturer = p.id_manufacturer + WHERE p.indexed = 0 + LIMIT '.(int)$limit); + } + public static function indexation($full = false, $id_product = false) { $db = Db::getInstance(); @@ -366,7 +366,7 @@ class SearchCore } else { - // Do it even if you already know the product id in order to be sure that it exists + // Do it even if you already know the product id in order to be sure that it exists $products = $db->executeS(' SELECT id_product FROM '._DB_PREFIX_.'product @@ -414,14 +414,14 @@ class SearchCore $wordIdsByWord[$wordId['id_shop']][$wordId['id_lang']]['_'.$wordId['word']] = (int)$wordId['id_word']; } - // Retrieve the number of languages - $nbLanguages = Db::getInstance()->getValue('SELECT COUNT(*) FROM '._DB_PREFIX_.'lang'); - - // Products are processed 50 by 50 in order to avoid overloading MySQL - while ($products = Search::getProductsToIndex($nbLanguages, 50) AND count($products) > 0) - { + // Retrieve the number of languages + $nbLanguages = Db::getInstance()->getValue('SELECT COUNT(*) FROM '._DB_PREFIX_.'lang'); + + // Products are processed 50 by 50 in order to avoid overloading MySQL + while ($products = Search::getProductsToIndex($nbLanguages, 50) AND count($products) > 0) + { // Now each non-indexed product is processed one by one, langage by langage - foreach ($products as $product) + foreach ($products as $product) { $product['tags'] = Search::getTags($db, (int)$product['id_product'], (int)$product['id_lang']); $product['attributes'] = Search::getAttributes($db, (int)$product['id_product'], (int)$product['id_lang']); @@ -437,9 +437,9 @@ class SearchCore if (!empty($word)) { $word = Tools::substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH); - // Remove accents - $word = Tools::replaceAccentedChars($word); - + // Remove accents + $word = Tools::replaceAccentedChars($word); + if (!isset($pArray[$word])) $pArray[$word] = 0; $pArray[$word] += $weightArray[$key]; @@ -459,23 +459,24 @@ class SearchCore $wordIdsByWord[$product['id_shop']][$product['id_lang']]['_'.$word] = 0; } - $existingWords = $db->executeS(' - SELECT DISTINCT word FROM '._DB_PREFIX_.'search_word - WHERE word IN ('.implode(',', $queryArray2).') - AND id_lang = '.(int)$product['id_lang']); - - foreach($existingWords as $data) - unset($queryArray[Tools::replaceAccentedChars($data['word'])]); - + $existingWords = $db->executeS(' + SELECT DISTINCT word FROM '._DB_PREFIX_.'search_word + WHERE word IN ('.implode(',', $queryArray2).') + AND id_lang = '.(int)$product['id_lang'].' + AND id_shop = '.(int)$product['id_shop']); + + foreach($existingWords as $data) + unset($queryArray[Tools::replaceAccentedChars($data['word'])]); + if (count($queryArray)) { // The words are inserted... $db->Execute(' INSERT IGNORE INTO '._DB_PREFIX_.'search_word (id_lang, id_shop, word) VALUES '.implode(',',$queryArray)); - } - if (count($queryArray2)) - { + } + if (count($queryArray2)) + { // ...then their IDs are retrieved and added to the cache $addedWords = $db->executeS(' SELECT sw.id_word, sw.word @@ -484,7 +485,7 @@ class SearchCore AND sw.id_lang = '.(int)$product['id_lang'].' AND sw.id_shop = '.(int)$product['id_shop'].' LIMIT '.count($queryArray2)); - // replace accents from the retrieved words so that words without accents or with differents accents can still be linked + // replace accents from the retrieved words so that words without accents or with differents accents can still be linked foreach ($addedWords AS $wordId) $wordIdsByWord[$product['id_shop']][$product['id_lang']]['_'.Tools::replaceAccentedChars($wordId['word'])] = (int)$wordId['id_word']; } @@ -506,12 +507,12 @@ class SearchCore if (!in_array($product['id_product'], $productsArray)) $productsArray[] = (int)$product['id_product']; - } + } Search::setProductsAsIndexed($productsArray); // One last save is done at the end in order to save what's left Search::saveIndex($queryArray3); - } + } return true; } @@ -594,4 +595,4 @@ class SearchCore return Product::getProductsProperties((int)$id_lang, $result); } -} \ No newline at end of file +}