Magento 修复产品搜索结果不准确的问题

2016年5月11日 Edited 2022年12月6日 浏览量 38 2 min read
Magento 修复产品搜索结果不准确的问题

我们常常在magento搜索产品,比如按照产品名字来搜索“iphone”,但是搜索到的结果,居然没有我们在后台设置的产品。这常常会令我们非常郁闷。下面我们来看下如何修改让magento可以让产品按照我们想要的结果来展示。

Magento的搜索产品默认结果集是按查询的词去分词了再OR查询(具体按Catalog Search设置的查询模式)。

如果我们搜索产品“ihone 6 plus”这词的话,返回的结果是产品名字或者产品可查找属性(后台属性设置 use in catalog search)中包括“iphone”、“6”、“plus”的产品。

如果我们想让搜索的结果更加精确,比如搜索“ihone 6 plus”,能够直接显示出来“ihone 6 plus”,那就最好了。下面我们来修改一下“app/code/core/Mage/CatalogSearch/Model/Resource/Fulltext.php”文件。我们可以通过重写model或者直接到community/local目录下创建相同路径的相同文件,下一步我们来修改搜索结果。

找到prepareResult()方法,再找到345行左右$likeCond  = '';然后把整个if判断注释掉,最后在下面添加:

$likeCond = '`s`.`data_index` LIKE :likew';
$bind[':likew'] = '%' . $queryText . '%';

就可以了。代码如下:

class Magease_CatalogSearch_Model_Resource_Fulltext extends Mage_CatalogSearch_Model_Resource_Fulltext {

/**
* Prepare results for query
*
* @param Mage_CatalogSearch_Model_Fulltext $object
* @param string $queryText
* @param Mage_CatalogSearch_Model_Query $query
* @return Mage_CatalogSearch_Model_Resource_Fulltext
*/
public function prepareResult($object, $queryText, $query) {
Mage::log('catalogsearch', null, 'catalogsearch.log');
$adapter = $this->_getWriteAdapter();
if (!$query->getIsProcessed()) {
$searchType = $object->getSearchType($query->getStoreId());

$preparedTerms = Mage::getResourceHelper('catalogsearch')
->prepareTerms($queryText, $query->getMaxQueryWords());

$bind = array();
$like = array();
$likeCond = '';
// if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE
// || $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE
// ) {
// $helper = Mage::getResourceHelper('core');
// $words = Mage::helper('core/string')->splitWords($queryText, true, $query->getMaxQueryWords());
// foreach ($words as $word) {
// $like[] = $helper->getCILike('s.data_index', $word, array('position' => 'any'));
// }
// if ($like) {
// $likeCond = '(' . join(' OR ', $like) . ')';
// }
// }
$likeCond = '`s`.`data_index` LIKE :likew';
$bind[':likew'] = '%' . $queryText . '%';

$mainTableAlias = 's';
$fields = array(
'query_id' => new Zend_Db_Expr($query->getId()),
'product_id',
);
$select = $adapter->select()
->from(array($mainTableAlias => $this->getMainTable()), $fields)
->joinInner(array('e' => $this->getTable('catalog/product')), 'e.entity_id = s.product_id', array())
->where($mainTableAlias . '.store_id = ?', (int) $query->getStoreId());

if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_FULLTEXT || $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE
) {
$bind[':query'] = implode(' ', $preparedTerms[0]);
$where = Mage::getResourceHelper('catalogsearch')
->chooseFulltext($this->getMainTable(), $mainTableAlias, $select);
}

if ($likeCond != '' && $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE) {
$where .= ($where ? ' OR ' : '') . $likeCond;
} elseif ($likeCond != '' && $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE) {
$select->columns(array('relevance' => new Zend_Db_Expr(0)));
$where = $likeCond;
}

if ($where != '') {
$select->where($where);
}

$sql = $adapter->insertFromSelect($select, $this->getTable('catalogsearch/result'), array(), Varien_Db_Adapter_Interface::INSERT_ON_DUPLICATE);
$adapter->query($sql, $bind);

$query->setIsProcessed(1);
}

return $this;
}

}

Previous article:
Next article:
Comments
发表评论,留下你的足迹
我们不会公开你的邮箱地址

是否允许我们在发布新内容或者进行促销活动向您发送消息?

Remind me later

Thank you! Please check your email inbox to confirm.

Oops! Notifications are disabled.

© 2014-2023 www.magease.com. All Rights Reserved. 寰云网络 版权所有    鲁ICP备 14014975号-1