Esempio n. 1
0
 /**
  * Prepare results for query
  *
  * @param Mage_CatalogSearch_Model_Fulltext $object
  * @param string $queryText
  * @param Mage_CatalogSearch_Model_Query $query
  * @return Mage_CatalogSearch_Model_Mysql4_Fulltext
  */
 public function prepareResult($object, $queryText, $query)
 {
     if (!$query->getIsProcessed()) {
         $searchType = $object->getSearchType($query->getStoreId());
         $stringHelper = Mage::helper('core/string');
         /* @var $stringHelper Mage_Core_Helper_String */
         $bind = array(':query' => $queryText);
         $like = array();
         $fulltextCond = '';
         $likeCond = '';
         $separateCond = '';
         if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE || $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE) {
             $words = $stringHelper->splitWords($queryText, true, $query->getMaxQueryWords());
             $likeI = 0;
             foreach ($words as $word) {
                 $like[] = '`s`.`data_index` LIKE :likew' . $likeI;
                 $bind[':likew' . $likeI] = '%' . $word . '%';
                 $likeI++;
             }
             if ($like) {
                 $likeCond = '(' . join(' AND ', $like) . ')';
             }
         }
         if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_FULLTEXT || $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE) {
             $fulltextCond = 'MATCH (`s`.`data_index`) AGAINST (:query IN BOOLEAN MODE)';
         }
         if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE && $likeCond) {
             $separateCond = ' OR ';
         }
         $sql = sprintf("INSERT INTO `{$this->getTable('catalogsearch/result')}` " . "(SELECT STRAIGHT_JOIN '%d', `s`.`product_id`, MATCH (`s`.`data_index`) " . "AGAINST (:query IN BOOLEAN MODE) FROM `{$this->getMainTable()}` AS `s` " . "INNER JOIN `{$this->getTable('catalog/product')}` AS `e` " . "ON `e`.`entity_id`=`s`.`product_id` WHERE (%s%s%s) AND `s`.`store_id`='%d')" . " ON DUPLICATE KEY UPDATE `relevance`=VALUES(`relevance`)", $query->getId(), $fulltextCond, $separateCond, $likeCond, $query->getStoreId());
         $this->_getWriteAdapter()->query($sql, $bind);
         $query->setIsProcessed(1);
     }
     return $this;
 }
Esempio n. 2
0
 /**
  * Prepare results for query
  *
  * @param Mage_CatalogSearch_Model_Fulltext $object
  * @param string $queryText
  * @param Mage_CatalogSearch_Model_Query $query
  * @return Mage_CatalogSearch_Model_Mysql4_Fulltext
  */
 public function prepareResult($object, $queryText, $query)
 {
     if (!$query->getIsProcessed()) {
         $searchType = $object->getSearchType($query->getStoreId());
         $stringHelper = Mage::helper('core/string');
         /* @var $stringHelper Mage_Core_Helper_String */
         $words = $stringHelper->splitWords($queryText, true, $query->getMaxQueryWords());
         $query_text = array();
         $query_text_str = '';
         if (is_array($words)) {
             foreach ($words as $word) {
                 $query_text[] = '+' . $word . '*';
             }
             $query_text_str = implode(' ', $query_text);
         }
         $bind = array(':query' => $query_text_str);
         $like = array();
         $fulltextCond = '';
         $likeCond = '';
         $separateCond = '';
         if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE || $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE) {
             $likeI = 0;
             foreach ($words as $word) {
                 $like[] = '`s`.`data_index` LIKE :likew' . $likeI;
                 $bind[':likew' . $likeI] = $word . "%";
                 $likeI++;
             }
             if ($like) {
                 $likeCond = '(' . join(' AND ', $like) . ')';
             }
         }
         if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_FULLTEXT || $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE) {
             $fulltextCond = 'MATCH (`s`.`data_index`) AGAINST (:query IN BOOLEAN MODE)';
         }
         if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE && $likeCond) {
             $separateCond = ' OR ';
         }
         //$relevanceExpression = '(MATCH (`s`.`data_index`) AGAINST (:query))';
         // Add priority attributes
         for ($i = 0; $i < self::NUMBER_OF_PRIORITY_SEARCH_ATTRIBUTES; $i++) {
             $weight = $this->_getPriorityWeight($i);
             if ($weight != 0) {
                 if ($i == 0) {
                     $relevanceExpression = '(' . intval($weight) . '*(MATCH(`s`.`data_index_' . ($i + 1) . '`) AGAINST (:query)))';
                 } else {
                     $relevanceExpression .= '+(' . intval($weight) . '*(MATCH(`s`.`data_index_' . ($i + 1) . '`) AGAINST (:query)))';
                 }
             }
         }
         if ($this->_getNegateRelevance()) {
             $relevanceExpression = '-(' . $relevanceExpression . ')';
         }
         $sql = sprintf("INSERT INTO `{$this->getTable('catalogsearch/result')}` " . "(SELECT '%d', `s`.`product_id`, %s " . "FROM `{$this->getMainTable()}` AS `s` INNER JOIN `{$this->getTable('catalog/product')}` AS `e`" . "ON `e`.`entity_id`=`s`.`product_id` WHERE (%s%s%s) AND `s`.`store_id`='%d')" . " ON DUPLICATE KEY UPDATE `relevance`=VALUES(`relevance`)", $query->getId(), $relevanceExpression, $fulltextCond, $separateCond, $likeCond, $query->getStoreId());
         $this->_getWriteAdapter()->query($sql, $bind);
         //@TODO $this->computeRelevances($object, $queryText, $query);
         $query->setIsProcessed(1);
     }
     return $this;
 }
Esempio n. 3
0
 /**
  * Prepare results for query
  *
  * @param Mage_CatalogSearch_Model_Fulltext $object
  * @param string $queryText
  * @param Mage_CatalogSearch_Model_Query $query
  * @return Mage_CatalogSearch_Model_Resource_Fulltext
  */
 public function prepareResult($object, $queryText, $query)
 {
     $adapter = $this->_getWriteAdapter();
     if (!$query->getIsProcessed()) {
         $searchType = $object->getSearchType($query->getStoreId());
         $preparedTerms = Mage::getResourceHelper('catalogsearch')->prepareTerms($queryText, $query->getMaxQueryWords());
         $bind = array();
         $like = array();
         $likeCond = '';
         if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE || $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE) {
             $helper = Mage::getResourceHelper('core');
             $words = Mage::helper('core/string')->splitWords($queryText, true, $query->getMaxQueryWords());
             foreach ($words as $word) {
                 $like[] = $helper->getCILike('s.data_index', $word, array('position' => 'any'));
             }
             if ($like) {
                 $likeCond = '(' . join(' OR ', $like) . ')';
             }
         }
         $mainTableAlias = 's';
         $fields = array('query_id' => new Zend_Db_Expr($query->getId()), 'product_id');
         $select = $adapter->select()->from(array($mainTableAlias => $this->getMainTable()), $fields)->joinInner(array('e' => $this->getTable('catalog/product')), 'e.entity_id = s.product_id', array())->where($mainTableAlias . '.store_id = ?', (int) $query->getStoreId());
         if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_FULLTEXT || $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE) {
             $bind[':query'] = implode(' ', $preparedTerms[0]);
             $where = Mage::getResourceHelper('catalogsearch')->chooseFulltext($this->getMainTable(), $mainTableAlias, $select);
         }
         if ($likeCond != '' && $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE) {
             $where .= ($where ? ' OR ' : '') . $likeCond;
         } elseif ($likeCond != '' && $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE) {
             $select->columns(array('relevance' => new Zend_Db_Expr(0)));
             $where = $likeCond;
         }
         if ($where != '') {
             $select->where($where);
         }
         /*
         $sql = $adapter->insertFromSelect($select,
             $this->getTable('catalogsearch/result'),
             array(),
             Varien_Db_Adapter_Interface::INSERT_ON_DUPLICATE);
         $adapter->query($sql, $bind);
         */
         $conn = new Connection();
         $conn->setParams(array('host' => '127.0.0.1', 'port' => 9306));
         /** @var SphinxQL $sphinxQuery */
         $sphinxQuery = SphinxQL::create($conn)->select(array('*', SphinxQL::expr('WEIGHT()')))->from('fulltext')->option('ranker', 'bm25')->option('field_weights', SphinxQL::expr('(name=7, attributes=3, data_index=1)'))->option('cutoff', 5000)->option('max_matches', 1000)->limit(0, 100)->match('*', $queryText);
         $results = $sphinxQuery->execute();
         foreach ($results as $result) {
             // Ensure we log query results into the Magento table.
             $sql = sprintf("INSERT INTO {$this->getTable('catalogsearch/result')} " . " (query_id, product_id, relevance) VALUES " . " (%d, %d, %f) " . " ON DUPLICATE KEY UPDATE relevance = %f", $query->getId(), $result['id'], $result['weight()'] / 1000, $result['weight()'] / 1000);
             $this->_getWriteAdapter()->query($sql, $bind);
         }
         $conn->close();
         $query->setIsProcessed(1);
     }
     return $this;
 }
Esempio n. 4
0
 /**
  * Prepare results for query
  *
  * @param Mage_CatalogSearch_Model_Fulltext $object
  * @param string $queryText
  * @param Mage_CatalogSearch_Model_Query $query
  * @return Mage_CatalogSearch_Model_Resource_Fulltext
  */
 public function prepareResult($object, $queryText, $query)
 {
     $adapter = $this->_getWriteAdapter();
     if (!$query->getIsProcessed()) {
         $searchType = $object->getSearchType($query->getStoreId());
         $preparedTerms = Mage::getResourceHelper('catalogsearch')->prepareTerms($queryText, $query->getMaxQueryWords());
         $bind = array();
         $like = array();
         $likeCond = '';
         if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE || $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE) {
             $helper = Mage::getResourceHelper('core');
             $words = Mage::helper('core/string')->splitWords($queryText, true, $query->getMaxQueryWords());
             foreach ($words as $word) {
                 $like[] = $helper->getCILike('s.data_index', $word, array('position' => 'any'));
             }
             if ($like) {
                 $likeCond = '(' . join(' AND ', $like) . ')';
             }
         }
         $mainTableAlias = 's';
         $fields = array('query_id' => new Zend_Db_Expr($query->getId()), 'product_id');
         $select = $adapter->select()->from(array($mainTableAlias => $this->getMainTable()), $fields)->joinInner(array('e' => $this->getTable('catalog/product')), 'e.entity_id = s.product_id', array())->where($mainTableAlias . '.store_id = ?', (int) $query->getStoreId());
         if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_FULLTEXT || $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE) {
             $bind[':query'] = implode(' ', $preparedTerms[0]);
             $where = Mage::getResourceHelper('catalogsearch')->chooseFulltext($this->getMainTable(), $mainTableAlias, $select);
         }
         if ($likeCond != '' && $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE) {
             $where .= ($where ? ' OR ' : '') . $likeCond;
         } elseif ($likeCond != '' && $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE) {
             $select->columns(array('relevance' => new Zend_Db_Expr(0)));
             $where = $likeCond;
         }
         if ($where != '') {
             $select->where($where);
         }
         $sql = $adapter->insertFromSelect($select, $this->getTable('catalogsearch/result'), array(), Varien_Db_Adapter_Interface::INSERT_ON_DUPLICATE);
         $adapter->query($sql, $bind);
         $query->setIsProcessed(1);
     }
     return $this;
 }
Esempio n. 5
0
 /**
  * Run full reindex
  *
  * @return Enterprise_CatalogSearch_Model_Index_Action_Fulltext_Refresh
  * @throws Enterprise_Index_Model_Action_Exception
  */
 public function execute()
 {
     if (!$this->_metadata->isValid()) {
         throw new Enterprise_Index_Model_Action_Exception("Can't perform operation, incomplete metadata!");
     }
     try {
         $this->_getLastVersionId();
         $this->_metadata->setInProgressStatus()->save();
         // Reindex all products
         $this->_indexer->rebuildIndex();
         // Clear search results
         $this->_resetSearchResults();
         $this->_updateMetadata();
         $this->_app->dispatchEvent('after_reindex_process_catalogsearch_index', array());
     } catch (Exception $e) {
         $this->_metadata->setInvalidStatus()->save();
         throw new Enterprise_Index_Model_Action_Exception($e->getMessage(), $e->getCode());
     }
     return $this;
 }