Magento:改进默认搜索引擎

时间:2013-05-04 20:51:11

标签: magento

所以,我稍微更改了默认的Magento搜索引擎,它的工作方式与我想要的方式非常接近。 (即OR术语搜索到AND)。但是,还有一件事我想实施。当一个人搜索一系列术语时,例如Green Apple A,我希望首先出现Green Apple A产品。现在,使用AND运算符,结果按照从数据库中提取的顺序排列。因此,Green Apple A可能会出现在任何地方。

这是准备结果的函数..这对我来说有点复杂,我想知道是否有一种简单的方法来“追加”搜索结果,查找输入术语的特定序列并连接结果,给予优先权,所以它首先出现。

(对不起长代码。我通常不喜欢发布大量代码)

来自Fulltext.php

中的/stores/my_website/app/code/local/Mage/CatalogSearch/Model/Resource
public function prepareResult($object, $queryText, $query)
{

    $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(' AND ', $like) . ')';
            }
        }
        $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 ? ' AND ' : '') . $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;
}

1 个答案:

答案 0 :(得分:0)

我认为您可以尝试使用此免费扩展程序进行默认搜索

http://www.magentocommerce.com/magento-connect/catalog/product/view/id/12202/s/enhanced-default-search-9697