コード例 #1
0
ファイル: Observer.php プロジェクト: enyasar/magento-solarium
 /**
  * This is an observer function for the event 'start_index_events_catalog_product_delete'.
  * It gets called after one or more products have been deleted.
  *
  * @param Varien_Event_Observer $observer
  */
 public function startIndexEventsCatalogProductDelete($observer)
 {
     $productIds = array();
     $events = Mage::getSingleton('index/indexer')->getProcessByCode('catalogsearch_fulltext')->getUnprocessedEventsCollection();
     /** @var Mage_Index_Model_Event $event */
     foreach ($events as $event) {
         if ('catalog_product' == $event->getEntity() && 'delete' == $event->getType()) {
             $productId = $event->getEntityPk();
             if ($productId) {
                 $productIds[] = $productId;
             }
         }
     }
     if (!empty($productIds) && JeroenVermeulen_Solarium_Model_Engine::isEnabled()) {
         /** @var JeroenVermeulen_Solarium_Model_Engine $engine */
         $engine = Mage::getSingleton('jeroenvermeulen_solarium/engine');
         $engine->cleanIndex(null, $productIds);
     }
 }
コード例 #2
0
ファイル: Fulltext.php プロジェクト: enyasar/magento-solarium
 /**
  * This function is called when a visitor searches
  *
  * @param Mage_CatalogSearch_Model_Fulltext $object
  * @param string $queryText
  * @param Mage_CatalogSearch_Model_Query $query
  * @return JeroenVermeulen_Solarium_Model_Resource_CatalogSearch_Fulltext
  */
 public function prepareResult($object, $queryText, $query)
 {
     if (JeroenVermeulen_Solarium_Model_Engine::isEnabled($query->getStoreId())) {
         $helper = Mage::helper('jeroenvermeulen_solarium');
         $adapter = $this->_getWriteAdapter();
         $searchResultTable = $this->getTable('catalogsearch/result');
         $catSearchHelper = Mage::helper('catalogsearch');
         $stringHelper = Mage::helper('core/string');
         $thisQueryLength = $stringHelper->strlen($queryText);
         $wordsFull = $stringHelper->splitWords($queryText, true);
         $wordsLike = $stringHelper->splitWords($queryText, true, $catSearchHelper->getMaxQueryWords());
         /* Validate strings and return normal search to handle messages */
         if ($catSearchHelper->getMaxQueryLength() < $thisQueryLength || $catSearchHelper->getMinQueryLength() > $thisQueryLength || count($wordsFull) > count($wordsLike)) {
             return parent::prepareResult($object, $queryText, $query);
         }
         /** @var JeroenVermeulen_Solarium_Model_Engine $engine */
         $engine = Mage::getSingleton('jeroenvermeulen_solarium/engine');
         if ($engine->isWorking()) {
             $searchResult = $engine->search($query->getStoreId(), $queryText);
             $searchResult->setUserQuery($queryText);
             Mage::register('solarium_search_result', $searchResult);
             if (!$searchResult->getResultCount()) {
                 // Autocorrect
                 if ($engine->getConf('results/autocorrect', $query->getStoreId())) {
                     $searchResult->autoCorrect();
                 }
             }
             $resultProducts = $searchResult->getResultProducts();
             if (!$searchResult->getResultCount()) {
                 // No results, we need to check if the index is empty.
                 if ($engine->isEmpty($query->getStoreId())) {
                     Mage::Log(sprintf('%s - Warning: index is empty', __CLASS__), Zend_Log::WARN);
                 } else {
                     $query->setIsProcessed(1);
                 }
             } else {
                 $columns = array('query_id', 'product_id', 'relevance');
                 $insertRows = array();
                 $queryId = $query->getId();
                 foreach ($resultProducts as $data) {
                     $insertRows[] = array($queryId, $data['product_id'], $data['relevance']);
                 }
                 $adapter->beginTransaction();
                 $adapter->delete($searchResultTable, 'query_id = ' . $queryId);
                 $adapter->insertArray($searchResultTable, $columns, $insertRows);
                 $adapter->commit();
                 $query->setIsProcessed(1);
             }
             // Autocorrect notification
             if ($searchResult->didAutoCorrect()) {
                 $catSearchHelper->addNoteMessage($helper->__("Showing results for '%s' instead.", $searchResult->getResultQuery()));
             }
             // "Did you mean" suggestions
             $suggestions = $searchResult->getBetterSuggestions();
             if ($suggestions) {
                 $suggestHtml = '';
                 foreach ($suggestions as $searchTerm => $result_count) {
                     $title = $helper->__('Results') . ': ' . $result_count;
                     $href = Mage::getUrl('catalogsearch/result', array('q' => $searchTerm));
                     $suggestHtml .= sprintf('&nbsp; <a title="%s" href="%s">%s</a>', $title, $href, $searchTerm);
                 }
                 $catSearchHelper->addNoteMessage($helper->__('Did you mean:') . $suggestHtml);
             }
             /** @deprecated The registry key 'solarium_suggest' is deprecated, it was used in 1.6.0 till 1.6.2 */
             Mage::register('solarium_suggest', $searchResult->getBetterSuggestions());
         }
     }
     if (!$query->getIsProcessed()) {
         Mage::log('Solr disabled or something went wrong, fallback to Magento Fulltext Search', Zend_Log::WARN);
         return parent::prepareResult($object, $queryText, $query);
     }
     return $this;
 }