Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Search 'relevance' Value is Always 0

Tags:

magento

I noticed that I seemingly wasn't able to sort on relevance in a default search. Whether I tried ASC or DESC, the results were always the same and rather poorly relevant at that.

Upon further investigating, I found the addSearchFilter() method in Mage_CatalogSearch_Model_Resource_Fulltext_Collection :

/**
 * Add search query filter
 *
 * @param string $query
 * @return Mage_CatalogSearch_Model_Resource_Fulltext_Collection
 */
public function addSearchFilter($query)
{
    Mage::getSingleton('catalogsearch/fulltext')->prepareResult();

    $this->getSelect()->joinInner(
        array('search_result' => $this->getTable('catalogsearch/result')),
        $this->getConnection()->quoteInto(
            'search_result.product_id=e.entity_id AND search_result.query_id=?',
            $this->_getQuery()->getId()
        ),
        array('relevance' => 'relevance')
    );

    Zend_Debug::dump($this->getData());exit;

    return $this;
}

The result of my data dump shows the results just fine, but the 'relevance' column is always 0.00000. I have not made any changes to the catalog search and this is Magento 1.6.0.

I dumped the actual SQL as well:

SELECT `e`.*, `search_result`.`relevance` FROM `catalog_product_entity` AS `e` INNER JOIN `catalogsearch_result` AS `search_result` ON search_result.product_id=e.entity_id AND search_result.query_id='33'

If anyone else has more experience with calculating relevance I would greatly appreciate direction.

like image 487
Zachary Schuessler Avatar asked Feb 02 '12 17:02

Zachary Schuessler


1 Answers

Short answer: the relevance is only used in full text search mode.

Background

In the admin interface you can configure a "Search Type" for Magento.
This setting can be found under System > Config > Catalog > Catalog Search > Search Type

If switched to Fulltext (and after reindexing), and clearing the catalogsearch_query table, Magento will use the MySQL Fulltext Search capability, specifying a WHERE condition in the query as follows:

...MATCH (s.data_index) AGAINST (:query IN BOOLEAN MODE) AS `relevance`...

This will return a floating point number that will be used as the relevance value. A plain hit will give you a relevance of 1. If the index contains the search term more then once, it will be given a higher relevance.

Also the boolean fulltext search enables the use of search modifiers like "+this -notThis".
More information on the relevance weighting of the MySQL boolean fulltext search can be found here http://dev.mysql.com/doc/refman/5.1/de/fulltext-boolean.html

If the search mode "Like" is used, the relevance always is 0 (as you noticed).

Indexing

The way Magento builds the search index isn't very intuitive, I recommend a look at the table catalogsearch_fulltext. Then tune the attributes you want to use in the search by adjusting the Used in Quick Search property for them. This setting can be found under Catalog > Attributes > Manage Attributes. Then reindex the catalog search index.
I also recommend clearing out the catalogsearch_result table after you adjust the attributes.

like image 82
Vinai Avatar answered Oct 09 '22 15:10

Vinai