Magento Forum

   
Searching for products with code
 
Jakub@WebCatch
Sr. Member
 
Avatar
Total Posts:  215
Joined:  2009-11-06
Nottingham, UK
 

I ma trying to use magento’s search functionality to get all the products relevant to a given search term and send them out in an xml feed.

So far I managed to come up with this piece of code:

require_once ( "app/Mage.php" );
Mage::app('default');

$search_term $_GET['query'];

$query = new Mage_CatalogSearch_Model_Query;
$query->loadByQuery($search_term);

foreach(
$query->getResultCollection() as $item){
    
if($item->type_id=="configurable")
        echo 
$item->sku."<br />";
}

So, I have the products, but the problem is that they are sorted by sku and not by relevance (which is the default setting when you search on the website). Anybody knows how to sort the search results by relevance?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Jakub@WebCatch
Sr. Member
 
Avatar
Total Posts:  215
Joined:  2009-11-06
Nottingham, UK
 

bump

Anyone able to help?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mirasvit
Guru
 
Avatar
Total Posts:  638
Joined:  2009-08-22
 

Hi.
It is possible that you need something like this extension Sphinx Search Ultimate http://mirasvit.com/magento-extensions/sphinx-search-ultimate.html

 
Magento Community Magento Community
Magento Community
Magento Community
 
TreInnova
Guru
 
Avatar
Total Posts:  462
Joined:  2009-02-20
Fano
 
Jakub@WebCatch - 10 March 2010 03:38 AM

I ma trying to use magento’s search functionality to get all the products relevant to a given search term and send them out in an xml feed.

So far I managed to come up with this piece of code:

require_once ( "app/Mage.php" );
Mage::app('default');

$search_term $_GET['query'];

$query = new Mage_CatalogSearch_Model_Query;
$query->loadByQuery($search_term);

foreach(
$query->getResultCollection() as $item){
    
if($item->type_id=="configurable")
        echo 
$item->sku."<br />";
}

So, I have the products, but the problem is that they are sorted by sku and not by relevance (which is the default setting when you search on the website). Anybody knows how to sort the search results by relevance?

Hi we can take a look to the loadByQueryFunction():

public function loadByQuery(Mage_Core_Model_Abstract $object$value)
    
{
        $select 
$this->_getReadAdapter()->select()
            ->
from($this->getMainTable())
            ->
where('synonym_for=? OR query_text=?'$value)
            ->
where('store_id=?'$object->getStoreId())
            ->
order('synonym_for ASC')
            ->
limit(1);
        if (
$data $this->_getReadAdapter()->fetchRow($select)) {
            $object
->setData($data);
            
$this->_afterLoad($object);
        
}

        
return $this;
    
}
This is called from Mage_CatalogSearch_Model_Query loadByQuery
public function loadByQuery($text)
    
{
        $this
->_getResource()->loadByQuery($this$text);
        
$this->_afterLoad();
        
$this->setOrigData();
        return 
$this;
    
}
Regards
Filippo

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top