Magento搜索查询在API中产生空结果

时间:2014-07-11 15:04:16

标签: magento

我有这段代码:

//to-do
public function searchVehicles($terms, $offset=1, $order='ASC')
{
    if (trim($terms) == '') {
        return array();
    }

    $query = $this->_getQuery($terms);
    $query->setStoreId(1);
    if ($query->getId()) {
        $query->setPopularity($query->getPopularity()+1);
    }
    else {
        $query->setPopularity(1);
    }
    $query->prepare();
    $query->save();

    $collection = Mage::getResourceModel('catalog/product_collection');
    $collection->getSelect()->joinInner(
        array('search_result' => $collection->getTable('catalogsearch/result')),
        $collection->getConnection()->quoteInto(
            'search_result.product_id=e.entity_id AND search_result.query_id=?',
            $query->getId()
        ),
        array('relevance' => 'relevance')
    );
    $collection->setStore(1);

    //Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    //Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);

    return $this->_listProductCollection($collection, $offset, $order);
}

它位于Resource类中,可通过SOAP访问。

在我们开始之前:是的,我记得要进行缓存刷新和重新编译过程 - 我澄清一下,因为这对像我这样的新手xDDD来说是个常见问题。

现在:我可以访问这样的方法但返回[]

特别注意$this->_listProductCollection($collection, $offset, $order); 工作,因为我在同一资源中从其他方法获取的其他集合中使用相同的方法,并且没问题。

让我回顾一下我的代码的用意,因为我是Magento的新手(我使用的是1.6.2版本)。

  1. 代码基于CatalogSearch / ResultController控制器的indexAction()方法,并尝试了解它。
  2. 空查询将产生空结果,不会打扰Magento搜索引擎。
  3. 网站中只有一个商店(id = 1),搜索查询的创建方式如下:

    private function _getQuery($terms)
    {
        $query = Mage::getModel('catalogsearch/query')->loadByQuery($terms);
        if (!$query->getId()) {
            $query->setQueryText($terms);
        }
        return $query;
    }
    
  4. 查询增加了它的受欢迎程度(我从控制器获取此代码。我认为这仅用于统计目的。)

  5. 准备了查询(我认为这意味着:MySQL内部查询已经准备好了)所以我可以稍后再获取它。
  6. 保存查询 - AFAIK这意味着迭代和缓存查询结果,因此后续相同的查询将仅获取存储的结果,而不是再次处理搜索。
  7. 此时查询将有一个ID。
  8. 我获得了整个产品系列,并将其与搜索结果表一起加入。 SEEMS结果表 - 至少(queryId, matchedProductId)。我只保留匹配结果中的ID和商店1的产品。
  9. 我列出了产品。
  10. 请注意,过滤器目前已注释。
  11. 然而,当我点击这个API入口点时,返回的列表是[](空列表),虽然在通常的搜索栏中搜索给了我预期的结果。

    问题:我错过了什么?我在这个过程中误解了什么?

0 个答案:

没有答案