public static function saveTerm($term) { $foundExact = static::model()->find('search=:term', array(':term' => $term)); $foundLonger = Yii::app()->db->createCommand()->select()->from('TopSearch')->where(array('like', 'search', "{$term}%"))->queryRow(); $foundShorter = static::model()->find('search=:truncated', array(':truncated' => substr($term, 0, strlen($term) - 1))); if ($foundExact) { $foundExact->frequency++; $foundExact->save(); return; } if ($foundLonger && $foundShorter) { $foundShorter->delete(); } if ($foundLonger) { return; } if ($foundShorter) { $foundShorter->frequency = 1; $foundShorter->search = $term; $foundShorter->save(); return; } $newTerm = new TopSearch(); $newTerm->period = round(time() / (86400 * 30)); $newTerm->frequency = 1; $newTerm->search = $term; $newTerm->save(); }
public function run() { $topsearches = TopSearch::getTopTerms(10); $output = '<div class="homepage_content"> <div class="homepage_content_title">Top Ten Searches</div> <ul>'; $num = 1; foreach ($topsearches as $search) { $output .= "<li>{$num}. <a class=\"topten_keywords\" href=\"#\">{$search}</a></li>"; $num++; } $output .= ' </ul> </div> <!-- end of homepage_content -->'; echo $output; }
public function queryWithSpellCheck($query, $options = array(), $rows = 300, $offset = 0) { $query = $this->filterQuery($query); $tokens = explode(" ", $query); $isValidQuery = true; foreach ($tokens as $tok) { if (strlen($tok) < 5) { $isValidQuery = false; } } if ($isValidQuery) { TopSearch::saveTerm($query); } $query = $this->processQuery($query); $options['spellcheck'] = 'true'; $options['spellcheck.count'] = 10; $options['spellcheck.collate'] = 'true'; // spellcheck built has an overhead of rebuilding the spell index, // we avoid hitting too frequent by using probability of 1/90 $luckyNumber = mt_rand(0, 90); if ($luckyNumber == 8) { $options['spellcheck.build'] = 'true'; } $result = $this->solr->query($query, $options, $rows, $offset); $response = $result->response; if (property_exists($result, 'spellcheck') && property_exists($result->spellcheck, 'suggestions') && property_exists($result->spellcheck->suggestions, 'collation')) { $suggestion = $result->spellcheck->suggestions->collation; $fullsuggestion = $result->spellcheck; } else { $suggestion = NULL; $fullsuggestion = NULL; } $return = new stdClass(); $return->response = $response; $return->suggestion = str_replace('+', '', $suggestion); $return->fullsuggestion = $fullsuggestion; $return->highlighting = property_exists($result, 'highlighting') ? $result->highlighting : NULL; return $return; }