PrestaShop 1.7搜索引擎问题求助:匹配及结果精度异常
Hey there, let's work through these two search problems you're facing with PrestaShop 1.7—both are solvable with targeted adjustments to the core search logic or backend settings.
1. No Results for Continuous Strings (e.g., VR50HD, EVOLVE50)
The root issue here is that PrestaShop's default search relies on whitespace or special characters to split and match keywords. When users input a continuous string without hyphens/spaces, it doesn't align with the formatted product names (like VR-50HD or EVOLVE 50) in your catalog.
Solutions:
Adjust Search Query to Support Pattern Matching
Modify the core search logic to generate flexible regex patterns for continuous strings. Openclasses/Search.phpand update the keyword processing section (around thegetProductsmethod):$originalKeyword = Tools::getValue('s'); // Generate regex pattern to match both continuous and formatted versions // Example: Convert VR50HD to VR[- ]?50[- ]?HD $pattern = '/([A-Za-z]+)([0-9]+)([A-Za-z]+)/'; $regexKeyword = preg_replace($pattern, '$1[- ]?$2[- ]?$3', $originalKeyword); // Update the search condition to use REGEXP for product names $sql .= ' AND (product_name REGEXP "' . pSQL($regexKeyword) . '" OR product_description REGEXP "' . pSQL($regexKeyword) . '")';Note: Always back up core files before modifying them, or wrap this logic in a custom module to avoid losing changes during PrestaShop updates.
Switch to a More Powerful Search Engine
For long-term flexibility, integrate an Elasticsearch-based module (like the official PrestaShop Elasticsearch addon). Elasticsearch lets you define custom analyzers that automatically handle both continuous and hyphenated/space-separated keywords, eliminating the need for manual regex tweaks.
2. Low Precision: Irrelevant Results with "50" When Searching VR-50HD
This happens because PrestaShop's default search splits keywords by punctuation—so "VR-50HD" gets broken into "VR", "50", and "HD", leading to any product with "50" appearing in results.
Solutions:
Enable Exact Phrase Matching
Go to your PrestaShop backend:Preferences > Search, then enable the Exact match option. This forces the search to prioritize full keyword matches over split terms.Modify Search to Use Boolean Phrase Queries
Update theMATCH AGAINSTlogic inclasses/Search.phpto use MySQL's boolean mode for phrase matching:$keyword = pSQL(Tools::getValue('s')); // Wrap the keyword in quotes for exact phrase matching $searchCondition = 'MATCH(product_name, product_description) AGAINST(\'"' . $keyword . '"\' IN BOOLEAN MODE)'; $sql .= ' AND (' . $searchCondition . ')';This ensures only products containing the exact phrase "VR-50HD" (or its formatted variants if combined with the regex tweak above) appear in results.
Tweak Search Weights
If you still want partial matches but prioritize exact titles, adjust the weight calculation in the search query. For example:$sql .= ' AND ( MATCH(product_name) AGAINST(\'"' . $keyword . '"\' IN BOOLEAN MODE) * 3 + MATCH(product_description) AGAINST(\'"' . $keyword . '"\' IN BOOLEAN MODE) ) > 0';This gives product titles 3x more weight than descriptions, pushing exact title matches to the top.
内容的提问来源于stack exchange,提问作者艁ukasz Wójcik

