Algolia_Algoliasearch_Helper_Entity_Suggestionhelper::getPopularQueries PHP Method

getPopularQueries() public method

public getPopularQueries ( $storeId )
    public function getPopularQueries($storeId)
    {
        if ($this->_popularQueries == null) {
            // load from cache if we can
            $cachedPopularQueries = Mage::app()->loadCache($this->_popularQueriesCacheId);
            if ($cachedPopularQueries) {
                $this->_popularQueries = unserialize($cachedPopularQueries);
            } else {
                $collection = Mage::getResourceModel('catalogsearch/query_collection');
                $collection->getSelect()->where('num_results >= ' . $this->config->getMinNumberOfResults() . ' AND popularity >= ' . $this->config->getMinPopularity() . ' AND query_text != "__empty__"');
                $collection->getSelect()->limit(12);
                $collection->setOrder('popularity', 'DESC');
                $collection->setOrder('num_results', 'DESC');
                $collection->setOrder('updated_at', 'ASC');
                if ($storeId) {
                    $collection->getSelect()->where('store_id = ?', (int) $storeId);
                }
                $collection->load();
                $suggestions = array();
                /** @var $suggestion Mage_Catalog_Model_Category */
                foreach ($collection as $suggestion) {
                    if (strlen($suggestion['query_text']) >= 3) {
                        $suggestions[] = $suggestion['query_text'];
                    }
                }
                $this->_popularQueries = array_slice($suggestions, 0, 9);
                try {
                    //save to cache
                    $cacheContent = serialize($this->_popularQueries);
                    $tags = array(Mage_CatalogSearch_Model_Query::CACHE_TAG);
                    Mage::app()->saveCache($cacheContent, $this->_popularQueriesCacheId, $tags, 604800);
                } catch (Exception $e) {
                    // Exception = no caching
                    Mage::logException($e);
                }
            }
        }
        return $this->_popularQueries;
    }