public static function search($phrase, $exact = false, $offset = 0, $max = 10) { $words = array_values(myTools::stemPhrase($phrase)); $nb_words = count($words); if (!$words) { return array(); } $con = Propel::getConnection(); $query = ' SELECT DISTINCT %s, COUNT(*) AS nb, SUM(%s) AS total_weight FROM %s '; if (sfConfig::get('app_permanent_tag')) { $query .= sprintf(' LEFT JOIN %s ON %s = %s WHERE %s = ? AND ', QuestionTagPeer::TABLE_NAME, QuestionTagPeer::QUESTION_ID, SearchIndexPeer::QUESTION_ID, QuestionTagPeer::NORMALIZED_TAG); } else { $query .= 'WHERE'; } $query .= ' (' . implode(' OR ', array_fill(0, $nb_words, SearchIndexPeer::WORD . ' = ?')) . ') GROUP BY %s '; // AND query? if ($exact) { $query .= ' HAVING nb = ' . $nb_words; } $query .= ' ORDER BY nb DESC, total_weight DESC'; $query = sprintf($query, SearchIndexPeer::QUESTION_ID, SearchIndexPeer::WEIGHT, SearchIndexPeer::TABLE_NAME, SearchIndexPeer::QUESTION_ID); $stmt = $con->prepareStatement($query); $stmt->setOffset($offset); $stmt->setLimit($max); $placeholder_offset = 1; if (sfConfig::get('app_permanent_tag')) { $stmt->setString(1, sfConfig::get('app_permanent_tag')); $placeholder_offset = 2; } for ($i = 0; $i < $nb_words; $i++) { $stmt->setString($i + $placeholder_offset, $words[$i]); } $rs = $stmt->executeQuery(ResultSet::FETCHMODE_NUM); $questions = array(); while ($rs->next()) { $questions[] = self::retrieveByPK($rs->getInt(1)); } return $questions; }
public function getWords() { // body $raw_text = str_repeat(' ' . strip_tags($this->getHtmlBody()), sfConfig::get('app_search_body_weight')); // title $raw_text .= str_repeat(' ' . $this->getTitle(), sfConfig::get('app_search_title_weight')); // title and body stemming $stemmed_words = myTools::stemPhrase($raw_text); // unique words with weight $words = array_count_values($stemmed_words); // add tags $max = 0; foreach ($this->getPopularTags(20) as $tag => $count) { if (!$max) { $max = $count; } $stemmed_tag = PorterStemmer::stem($tag); if (!isset($words[$stemmed_tag])) { $words[$stemmed_tag] = 0; } $words[$stemmed_tag] += ceil($count / $max * sfConfig::get('app_search_tag_weight')); } return $words; }