• Coding

    Getting the SQL Query for Magento Fulltext Search

    Short version: Magento searches through the catalogsearch_fulltext table. Reindex the Catalog Search Index if the data in there seems to be incorrect.

    Long version: Ever searched for a keyword of Magento and wondered, “why did this product show up under this keyword search?” If you go to the catalog/product/list.phtml and echoed out the SQL query for the Product Collection (via $_productCollection->getSelectSql(true)), you’ll get a useless SQL query that looks something like this:

    SELECT 
    	1 AS `status`, <....> 
    FROM `catalog_product_flat_1` AS `e`
    	INNER JOIN `catalogsearch_result` AS `search_result` ON 
    		search_result.product_id=e.entity_id AND 
    		search_result.query_id='19091'
    	INNER JOIN `catalog_product_index_price` AS `price_index` ON 
    		price_index.entity_id = e.entity_id AND 
    		price_index.website_id = '1' AND 
    		price_index.customer_group_id = '1'
    	INNER JOIN `catalog_category_product_index` AS `cat_index` ON 
    		cat_index.product_id=e.entity_id AND 
    		cat_index.store_id='1' AND 
    		cat_index.visibility IN(3, 4) AND 
    		cat_index.category_id='2' 
    ORDER BY `position` asc, `e`.`name` asc LIMIT 50

    So, you can see that query results are stored in the catalogsearch_result table, but how is that table built in the first place?

    If you go to Mage_CatalogSearch_Model_Resource_Fulltext (or Mage_CatalogSearch_Model_Mysql4_Fulltext if you’re using an older version of Magento), and scroll down to the prepareResult() function, you can see it building the search query if that keyword hasn’t been searched before (if (!$query->getIsProcessed())). To get the query itself, you can add Mage::log($sql); Mage::log($bind); just before $adapter->query($sql, $bind);. Remember, this will only fire on a keyword that hasn’t been searched before, unless you add a || true on the getIsProcessed() line.

    The log will show something like this:

    INSERT INTO `catalogsearch_result` 
    	(SELECT STRAIGHT_JOIN '19091', 
    		`s`.`product_id`, 
    		MATCH (`s`.`data_index`) AGAINST (:query IN BOOLEAN MODE), 
    		c.position 
    	FROM 
    		`catalog_category_product` as `c`,  
    		`catalogsearch_fulltext` AS `s` INNER JOIN `catalog_product_entity` AS `e` ON 
    			`e`.`entity_id`=`s`.`product_id` 
    	WHERE 
    		((`s`.`data_index` LIKE :likew0)) AND 
    		`s`.`store_id`='1' and 
    		s.product_id = c.product_id) 
    ON DUPLICATE KEY UPDATE `relevance`=VALUES(`relevance`)
    

    So, you can see that the query checks the catalogsearch_fulltext table. For each product id, it stores a concatenated string of all the data that’s been indexed. It’ll look something like this:

    fulltext_id	product_id	store_id	data_index
    4		1		1		123456789|Taxable Goods|Laptop|Laptop Description|Laptop Description|1200|1
    

    To control which data fields are indexed in this table, go to the Magento Admin and go to Catalog > Attributes > Manage Attribute. Select an attribute, and then scroll down to Frontend Properties. Toggle the “Use in Quick Search” settings. Then, reindex the Catalog Search Index if necessary.