/** * Returns SphinxQL search object, initialized for tag search * * @param SearchForm $model * @return \ESphinxQL */ protected function getSphinxTagSearchObject(SearchForm $model) { $objCommon = new ESphinxQL(); $objCommon->addField('id')->addIndex(Yii::app()->params['sphinx']['indexes']['torrents']); $tagPatterns = array(); if ($model->tags) { $tagPatterns[] = '"=' . str_replace(' ', '+', $model->tags) . '"'; } if ($model->category) { $tagPatterns[] = '"=' . str_replace(' ', '+', LCategory::getCategoryTag($model->category)) . '"'; $objCommon->where('category_id', $model->category); } if (count($tagPatterns)) { $searchPattern = '@tags ' . join(' ', $tagPatterns); } $objCommon->option('ranker', 'MATCHANY'); // Filter torrents by status if (!empty($model->status)) { $objCommon->where('torrent_status', Torrent::TORRENT_STATUS_GOOD); } // Filter torrents by age if (!empty($model->age)) { $objCommon->where('created_at', time() - $model->age * 86400, '>=', true); } if (!empty($model->popular)) { $objCommon->order('downloads_count', 'DESC'); } if ($model->latest) { $objCommon->order('created_at', 'DESC'); $objCommon->limit($model->latest); } // If no ordering is set - default weight+id sorting mode if (!$objCommon->getOrders()) { $objCommon->order('weight()', 'DESC'); $objCommon->order('id', 'DESC'); } if (!empty($searchPattern)) { $objCommon->search($searchPattern); } return $objCommon; }
public static function getLastTorrentIdsByCategories($count = 5, $forceRefresh = false) { $key = 'tags_last_torrents_ids'; $torrentsIds = Yii::app()->cache->get($key); try { if (empty($torrentsIds) || $forceRefresh) { $torrentsIds = array(); $tags = LCategory::$categoriesTags; foreach ($tags as $tag) { $obj = new ESphinxQL(); $obj->addField('id')->addIndex(Yii::app()->params['sphinx']['indexes']['torrents'])->option('ranker', 'SPH04')->search('@tags "' . $obj->halfEscapeMatch($tag) . '"')->where('torrent_status', LTorrent::TORRENT_STATUS_GOOD)->order('weight()', 'DESC')->order('id', 'DESC')->limit($count); $torrentsIds = array_merge($torrentsIds, Yii::app()->sphinx->cache(600)->createCommand($obj->build())->queryColumn()); } Yii::app()->cache->set($key, $torrentsIds); } } catch (Exception $e) { Yii::log('getLastTorrentIdsByCategories failed. Exception: ' . $e->getMessage() . PHP_EOL . 'Trace: ' . $e->getTrace(), CLogger::LEVEL_ERROR); if (YII_DEBUG) { throw $e; } } return $torrentsIds; }