Magento Enterprise – Sort Products by Creation Date with SOLR

magento-enterprisesolrsorting

I need to sort product collection by creation date. It's static attribute in the product entity table. SOLR can index and sort by eav attributes. One option is to create a new attribute and copy date on product save event. Any other suggestions? Ideas what is the best way to add custom data to the SOLR index?

Best Answer

If you are referring to the product created_at and updated_at attributes, then simply commenting out the ->addVisibleFilter() in the Catalog Product Attribute Grid in the admin and simply set the Search and Sort fields to Yes, and re-index, you'll notice the date(s) are now in the catalogsearch_fulltext table which the Enterprise Solr will recognize and add the data to its doc records as well.

NOTE: Don't forgot to remove the core code change or do it via local code pool obviously if you like.

class Mage_Adminhtml_Block_Catalog_Product_Attribute_Grid extends Mage_Eav_Block_Adminhtml_Attribute_Grid_Abstract
{
    /**
     * Prepare product attributes grid collection object
     *
     * @return Mage_Adminhtml_Block_Catalog_Product_Attribute_Grid
     */
    protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel('catalog/product_attribute_collection');
            //->addVisibleFilter();
        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

After this you'll want to let Solr know its a sortable metadata field.

Enterprise/Search/Model/Adapter/Abstract.php you'll notice:

/**
 * Store common Solr metadata fields
 * All fields, that come up from search engine will be filtered by these keys
 *
 * @var array
 */
protected $_usedFields = array('sku', 'visibility', 'in_stock');

You may want to also look over the method: _prepareIndexProductData in

Enterprise/Search/Model/Adapter/Abstract.php

/**
 * Prepare index data for using in search engine metadata.
 * Prepare fields for advanced search, navigation, sorting and fulltext fields for each search weight for
 * quick search and spell.
 *
 * @param array $productIndexData
 * @param int $productId
 * @param int $storeId
 *
 * @return  array|bool
 */
protected function _prepareIndexProductData($productIndexData, $productId, $storeId)

I've only tested the ability to edit the "non-visible" attributes and confirmed the addition to the fulltext table, I don't have Solr running locally at the moment to test it, but in theory should work without any need to mirror redundant data.

Hope this helps!

Related Topic