如何以及在何处修改Magento搜索查询?

时间:2012-12-19 08:08:42

标签: magento collections e-commerce entity-attribute-value magento-1.6

首先,我在SO中发现了类似的问题,但没有任何答案。所以,问题的第一部分有点重复。我想改进Magento的搜索结果。以下是我已经完成的工作:

1。当有多个字时,使用AND代替OR进行搜索。

2。 Ajax搜索从任何地方开始搜索,而不仅仅是从字段的开头搜索。

3。修剪单词中的最后一个s以防止在复数形式搜索时出现空结果。

4。我将搜索类型从Like更改为FulltextCombine,但结果并不是更好,甚至是最差的,所以我保留了它原样。它现在是Like,因此没有relevance排序。

我想要尝试的最后一件事是adding this to the search query

SELECT ... other non-full-text-cols
MATCH (product_title) AGAINST ('lean body for her') AS rel1,
MATCH (content) AGAINST ('lean body for her') AS rel2
FROM table
WHERE MATCH (product_title,content) AGAINST ('lean body for her')
ORDER BY (rel1*1.5)+(rel2)

以下是我的查询,但我不确定它是否可行,因为我无法对其进行测试:

$this->_productCollection->addAttributeToSelect(
    array(
    'rel1' => new Zend_Db_Expr('MATCH (name) AGAINST ("'.$queryText.'")'),
    'rel2' => new Zend_Db_Expr('MATCH (short_description) AGAINST ("'.$queryText.'")')
    )
);

$this->_productCollection->getSelect()
    ->where('MATCH (name,short_description) AGAINST ("'.$queryText.'")')
    ->order('(rel1*1.5)+(rel2)');

如果在产品标题中找到搜索查询,主要思想是为结果添加奖励权重。问题是我不知道在哪里修改查询。我无法找到它的位置。我知道$this->_productCollection不是正确的对象。我查看了所有Collection.php文件,资源模型,模型甚至查询日志,但没有运气。某些文件中只有少量的1或2行部分,但不是完整的查询。我是Magento的新手,但在找到这类东西方面仍有问题。那么,当我必须扩展查询时,我必须放置我的附加内容?

Community Edition Magento,版本1.6.1.0。

注意:我知道改进搜索结果的某些扩展程序将比我的解决方案更好地工作,但是现在我必须以这种方式执行此操作。对我来说这也是一次很好的经历。

修改

所以,我想出了如何为订购添加我的自定义字段,但它是什么 我觉得不干净在class Mage_CatalogSearch_Model_Layer extends Mage_Catalog_Model_Layer prepareProductCollection方法中,我向查询添加了两个联接,并获取字段rel1rel2

$collection->getSelect()->joinLeft(
    array('cpev' => 'catalog_product_entity_varchar'),
    'cpev.entity_id = e.entity_id AND cpev.attribute_id = 96',
    array('rel1' => new Zend_Db_Expr('2.01*(LENGTH(cpev.value) - LENGTH(REPLACE(LCASE(cpev.value), LCASE("'.$queryText.'"), ""))) / LENGTH("'.$queryText.'")'))
);

$collection->getSelect()->joinLeft(
    array('cpet' => 'catalog_product_entity_text'),
    'cpet.entity_id = e.entity_id AND cpet.attribute_id = 506',
    array('rel2' => new Zend_Db_Expr('(LENGTH(cpet.value) - LENGTH(REPLACE(LCASE(cpet.value), LCASE("'.$queryText.'"), ""))) / LENGTH("'.$queryText.'")'))
);

我现在有了这些字段,但正如你所看到的那样,我有像attribute_id = 96等硬编码的东西,这些东西一点都不好,而且每次都不起作用 - 我直接从数据库表中检查了这些ID。我是这样写的,因为我无法访问nameshort_description字段,但它们都在结果中。不知道为什么。因此,cpev.valuename字段,cpet.valueshort_description字段。此外,我无法通过这些字段对结果进行排序。我尝试了$collection->addOrder('SUM(rel1+rel2)');$collection->getSelect()->order(new Zend_Db_Expr('SUM(rel1+rel2)').' DESC');,一些addAttributeToFilter等东西,但它没有用。

编辑2: 我接受了 @james '回答但最后我们买了一个扩展来改善搜索结果。

2 个答案:

答案 0 :(得分:4)

Mage_CatalogSearch_Model_Resource_Fulltext中查看prepareResult(公元1.7年第310行)并查找以下内容:

$select->columns(array('relevance'  => new Zend_Db_Expr(0)));

Magento将所有搜索结果相关性设置为0(!);在这里添加你想要的相关性(越高越好)。您可以在Zend_Db_Expr()中创建自定义查询,以便在属性匹配时生成更高的相关性。

答案 1 :(得分:3)

第一个问题的答案(1): 要进行AND搜索而不是OR,您需要重写类

Mage_CatalogSearch_Model_Resource_Fulltext

方法

public function prepareResult($object, $queryText, $query)

你想切换部分

$likeCond = '(' . join(' OR ', $like) . ')';

$likeCond = '(' . join(' AND ', $like) . ')';

请务必在之后重新索引搜索索引以产生效果。

相关问题