示例#1
0
 public function prepareResult($object, $queryText, $query)
 {
     if (!Mage::helper('searchanise')->checkSearchaniseResult()) {
         return parent::prepareResult($object, $queryText, $query);
     }
     return $this;
 }
示例#2
0
 /**
  * override the method to prepare the result
  *
  * @param Mage_CatalogSearch_Model_Fulltext $object
  * @param string                            $queryText
  * @param Mage_CatalogSearch_Model_Query    $query
  *
  * @return Hackathon_ElasticgentoCatalogSearch_Model_Catalogsearch_Resource_Fulltext|Mage_CatalogSearch_Model_Resource_Fulltext
  */
 public function prepareResult($object, $queryText, $query)
 {
     $helper = Mage::helper('elasticgento_catalogsearch');
     if (!$helper->isSearchActive()) {
         return parent::prepareResult($object, $queryText, $query);
     }
     if (!$query->getIsProcessed()) {
         $adapter = $helper->getAdapter();
         $result = $this->fetchSearchResultFromElasticSearch($adapter, $queryText, $query);
         if ($result && $result instanceof \Elastica\ResultSet) {
             $this->fillSearchResultInMagentoResultTable($result, $query);
         }
     }
 }
 /**
  * Only used when reindexing everything. Otherwise Model/Indexer/Algolia will take care of the rest.
  */
 public function rebuildIndex($storeId = null, $productIds = null)
 {
     if ($this->config->isEnabledBackend(Mage::app()->getStore()->getId()) === false) {
         return parent::rebuildIndex($storeId, $productIds);
     }
     if (!$this->config->getApplicationID() || !$this->config->getAPIKey() || !$this->config->getSearchOnlyAPIKey()) {
         /** @var Mage_Adminhtml_Model_Session $session */
         $session = Mage::getSingleton('adminhtml/session');
         $session->addError('Algolia reindexing failed: You need to configure your Algolia credentials in System > Configuration > Algolia Search.');
         return null;
     }
     /* Avoid Indexing twice */
     if (is_array($productIds) && $productIds > 0) {
         return $this;
     }
     $this->engine->rebuildProducts($storeId);
     return $this;
 }
 /**
  * Only used when reindexing everything. Otherwise Model/Indexer/Algolia will take care of the rest
  */
 public function rebuildIndex($storeId = null, $productIds = null)
 {
     if ($this->config->isEnabledBackEnd(Mage::app()->getStore()->getId()) === false) {
         return parent::rebuildIndex($storeId, $productIds);
     }
     if (!$this->config->getApplicationID() || !$this->config->getAPIKey() || !$this->config->getSearchOnlyAPIKey()) {
         Mage::getSingleton('adminhtml/session')->addError('Algolia reindexing failed: You need to configure your Algolia credentials in System > Configuration > Algolia Search.');
         return;
     }
     /** Avoid Indexing twice */
     if (is_array($productIds) && $productIds > 0) {
         return $this;
     }
     if ($storeId == null) {
         foreach (Mage::app()->getStores() as $id => $store) {
             $this->engine->rebuildProductIndex($id, null);
         }
     } else {
         $this->engine->rebuildProductIndex($storeId, null);
     }
     return $this;
 }
示例#5
0
 /**
  * Prepare Fulltext index value for product
  *
  * @param array $indexData
  * @param array $productData
  * @param int $storeId
  * @return string
  */
 protected function _prepareProductIndex($indexData, $productData, $storeId)
 {
     if (!Mage::getStoreConfigFlag('sphinxsearch/active/indexer')) {
         return parent::_prepareProductIndex($indexData, $productData, $storeId);
     }
     $index = array();
     foreach ($this->_getSearchableAttributes('static') as $attribute) {
         $attributeCode = $attribute->getAttributeCode();
         if (isset($productData[$attributeCode])) {
             $value = $this->_getAttributeValue($attribute->getId(), $productData[$attributeCode], $storeId);
             if ($value) {
                 //For grouped products
                 if (isset($index[$attributeCode])) {
                     if (!is_array($index[$attributeCode])) {
                         $index[$attributeCode] = array($index[$attributeCode]);
                     }
                     $index[$attributeCode][] = $value;
                 } else {
                     $index[$attributeCode] = $value;
                 }
             }
         }
     }
     foreach ($indexData as $entityId => $attributeData) {
         foreach ($attributeData as $attributeId => $attributeValue) {
             $value = $this->_getAttributeValue($attributeId, $attributeValue, $storeId);
             if (!is_null($value) && $value !== false) {
                 $attributeCode = $this->_getSearchableAttribute($attributeId)->getAttributeCode();
                 if (isset($index[$attributeCode])) {
                     $index[$attributeCode][$entityId] = $value;
                 } else {
                     $index[$attributeCode] = array($entityId => $value);
                 }
             }
         }
     }
     if (!$this->_engine->allowAdvancedIndex()) {
         $product = $this->_getProductEmulator()->setId($productData['entity_id'])->setTypeId($productData['type_id'])->setStoreId($storeId);
         $typeInstance = $this->_getProductTypeInstance($productData['type_id']);
         if ($data = $typeInstance->getSearchableData($product)) {
             $index['options'] = $data;
         }
     }
     if (isset($productData['in_stock'])) {
         $index['in_stock'] = $productData['in_stock'];
     }
     return $this->_engine->prepareEntityIndex($index, $this->_separator, $productData['entity_id']);
 }
示例#6
0
文件: Fulltext.php 项目: Hikari/Solr
 /**
  * Overloaded method cleanIndex.
  * Delete search index data for store
  *
  * @param int $storeId Store View Id
  * @param int|array|null $productIds Product Entity Id
  * @return Mage_CatalogSearch_Model_Resource_Fulltext
  */
 public function cleanIndex($storeId = null, $productIds = null)
 {
     parent::cleanIndex($storeId, $productIds);
     if (Mage::getStoreConfigFlag('solr/active/admin')) {
         Mage::getModel('solr/indexer')->cleanIndex($productIds);
     }
     return $this;
 }
示例#7
0
 /**
  * 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;
 }
示例#8
0
 public function __construct()
 {
     parent::__construct();
     $this->engine = new Algolia_Algoliasearch_Model_Resource_Engine();
 }
 /**
  * Reset catalogsearch_result.is_processed to 0
  * Avoid repeating this action too many times on large updates.
  *
  * @return $this
  */
 public function resetSearchResults()
 {
     static $counter = -1;
     $counter++;
     if ($counter < 10 || $counter % 100 == 0) {
         parent::resetSearchResults();
     }
     return $this;
 }