Changeset 4823
- Timestamp:
- 08/27/08 00:37:17 (5 months ago)
- Location:
- branches/1.0
- Files:
-
- 5 modified
-
lib/Doctrine/Search.php (modified) (1 diff)
-
lib/Doctrine/Search/Query.php (modified) (2 diffs)
-
lib/Doctrine/Table.php (modified) (1 diff)
-
lib/Doctrine/Template/Searchable.php (modified) (1 diff)
-
tests/SearchTestCase.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/1.0/lib/Doctrine/Search.php
r4814 r4823 68 68 } 69 69 70 71 /**72 * search73 * 74 * @param string $query75 * @return Doctrine_Collection The collection of search results76 */ 77 public function search($ query)70 /** 71 * Searchable keyword search 72 * 73 * @param string $string Keyword string to search for 74 * @param Doctrine_Query $query Query object to alter. Adds where condition to limit the results using the search index 75 * @return mixed The Doctrine_Collection or array of ids and relevancy 76 */ 77 public function search($string, $query = null) 78 78 { 79 79 $q = new Doctrine_Search_Query($this->_table); 80 81 $q->query($query); 82 83 return $this->_options['connection']->fetchAll($q->getSql(), $q->getParams());; 80 81 if ($query instanceof Doctrine_Query) { 82 $q->query($string, false); 83 84 $newQuery = $query->copy(); 85 $query->getSql(); 86 $newQuery->addWhere($query->getRootAlias() . '.id IN(' . $q->getSql() . ')', $q->getParams()); 87 88 return $newQuery; 89 } else { 90 $q->query($string); 91 return $this->_options['connection']->fetchAll($q->getSql(), $q->getParams()); 92 } 84 93 } 85 94 -
branches/1.0/lib/Doctrine/Search/Query.php
r4332 r4823 71 71 72 72 73 public function query($text )73 public function query($text, $includeRelevance = true) 74 74 { 75 75 $text = trim($text); … … 79 79 $weighted = false; 80 80 if (strpos($text, '^') === false) { 81 $select = 'SELECT COUNT(keyword) AS relevance, ' . $foreignId; 82 $from = 'FROM ' . $this->_table->getTableName(); 83 } else { 84 // organize terms according weights 85 $weighted = true; 86 87 $select = 'SELECT SUM(sub_relevance) AS relevance, ' . $foreignId; 88 $from = 'FROM ' ; 81 if ($includeRelevance) { 82 $select = 'SELECT COUNT(keyword) AS relevance, ' . $foreignId; 83 } else { 84 $select = 'SELECT ' . $foreignId; 85 } 86 } else { 87 if ($includeRelevance) { 88 $select = 'SELECT SUM(sub_relevance) AS relevance, ' . $foreignId; 89 } else { 90 $select = 'SELECT ' . $foreignId; 91 } 89 92 } 90 93 94 $from = 'FROM ' . $this->_table->getTableName(); 91 95 $where = 'WHERE '; 92 96 $where .= $this->parseClause($text); 93 97 94 98 $groupby = 'GROUP BY ' . $foreignId; 95 $orderby = 'ORDER BY relevance DESC'; 96 97 $this->_sql = $select . ' ' . $from . ' ' . $where . ' ' . $groupby . ' ' . $orderby; 99 if ($includeRelevance) { 100 $orderBy = 'ORDER BY relevance DESC'; 101 } else { 102 $orderBy = null; 103 } 104 $this->_sql = $select . ' ' . $from . ' ' . $where . ' ' . $groupby; 105 if (isset($orderBy) && $orderBy !== null) { 106 $this->_sql .= ' ' . $orderBy; 107 } 98 108 } 99 109 -
branches/1.0/lib/Doctrine/Table.php
r4786 r4823 2273 2273 } 2274 2274 2275 // Forward the method on to the record instance and see if it has anything or one of its behaviors 2276 try { 2277 return call_user_func_array(array($this->getRecordInstance(), $method . 'TableProxy'), $arguments); 2278 } catch (Exception $e) {} 2279 2275 2280 throw new Doctrine_Table_Exception(sprintf('Unknown method %s::%s', get_class($this), $method)); 2276 2281 } -
branches/1.0/lib/Doctrine/Template/Searchable.php
r4439 r4823 68 68 $this->_plugin->batchUpdateIndex($limit, $offset); 69 69 } 70 71 /** 72 * Searchable keyword search proxy for Doctrine_Table 73 * 74 * @param string $string Keyword string to search for 75 * @param Doctrine_Query $query Query object to alter. Adds where condition to limit the results using the search index 76 * @return mixed The Doctrine_Collection or array of ids and relevancy 77 */ 78 public function searchTableProxy($string, $query = null) 79 { 80 return $this->_plugin->search($string, $query); 81 } 70 82 } -
branches/1.0/tests/SearchTestCase.php
r3884 r4823 61 61 62 62 $e->save(); 63 } 64 65 public function testSearchFromTableObject() 66 { 67 $results = Doctrine::getTable('SearchTest')->search('orm'); 68 $this->assertEqual($results[0]['id'], 1); 69 $query = Doctrine_Query::create() 70 ->from('SearchTest s'); 71 $query = Doctrine::getTable('SearchTest')->search('orm', $query); 72 $this->assertEqual($query->getSql(), 'SELECT s.id AS s__id, s.title AS s__title, s.content AS s__content FROM search_test s WHERE s.id IN(SELECT id FROM search_test_index WHERE keyword = ? GROUP BY id)'); 73 $results = $query->fetchArray(); 74 $this->assertEqual($results[0]['id'], 1); 63 75 } 64 76 … … 173 185 $oQuery = new Doctrine_Search_Query("SearchTest"); 174 186 $oQuery->query("^test"); 175 $this->assertEqual($oQuery->getSql(), "SELECT SUM(sub_relevance) AS relevance, id FROM WHERE keyword = ? GROUP BY id ORDER BY relevance DESC");187 $this->assertEqual($oQuery->getSql(), "SELECT SUM(sub_relevance) AS relevance, id FROM search_test WHERE keyword = ? GROUP BY id ORDER BY relevance DESC"); 176 188 } 177 189 }