public function testMatch()
 {
     $match = Match::create(self::$sphinxql)->match('test');
     $this->assertEquals('test', $match->compile()->getCompiled());
     $match = Match::create(self::$sphinxql)->match('test case');
     $this->assertEquals('(test case)', $match->compile()->getCompiled());
     $match = Match::create(self::$sphinxql)->match(function ($m) {
         $m->match('a')->orMatch('b');
     });
     $this->assertEquals('(a | b)', $match->compile()->getCompiled());
     $sub = new Match(self::$sphinxql);
     $sub->match('a')->orMatch('b');
     $match = Match::create(self::$sphinxql)->match($sub);
     $this->assertEquals('(a | b)', $match->compile()->getCompiled());
     $match = Match::create(self::$sphinxql)->match('test|case');
     $this->assertEquals('test\\|case', $match->compile()->getCompiled());
     $match = Match::create(self::$sphinxql)->match(SphinxQL::expr('test|case'));
     $this->assertEquals('test|case', $match->compile()->getCompiled());
 }
 public function testHaving()
 {
     $this->refill();
     $result = SphinxQL::create(self::$conn)->select(SphinxQL::expr('count(*) as cnt'))->from('rt')->groupBy('gid')->having('cnt', '>', 1)->execute();
     $this->assertCount(2, $result);
     $this->assertEquals('2', $result[1]['cnt']);
     $result = SphinxQL::create(self::$conn)->select(SphinxQL::expr('count(*) as cnt'))->from('rt')->groupBy('gid')->having('gid', 304)->execute();
     $this->assertCount(1, $result);
 }
示例#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;
 }
示例#4
0
 public function testGroupBy()
 {
     $this->refill();
     $result = SphinxQL::create($this->conn)->select(SphinxQL::expr('count(*)'))->from('rt')->groupBy('gid')->execute();
     $this->assertCount(5, $result);
     $this->assertEquals('3', $result[3]['count(*)']);
 }