Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public function actionSearch()
{
// ---------SphinxSearch----------
if($_SERVER['REQUEST_METHOD'] == 'GET')
{
$searchText = HtmlPurifier::process(Yii::$app->request->get('search'));
$searchText = trim($searchText);
$sphinxQuery = new Query();
$sphinxResult = $sphinxQuery->from('post_index')->match($searchText)->all();
$totalSphinx = count($sphinxResult);
if($totalSphinx > 0)
{
foreach ($sphinxResult as $sphinxRow)
{
$ids[] = $sphinxRow['id'];
}
arsort($ids);
$searchResult = PostModel::find()->where(['id'=>$ids, 'status'=>1])->orderBy(['date_added' => SORT_DESC]);
$countQuery = clone $searchResult;
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize'=>$this->postPerPage]);
$models = $searchResult->offset($pages->offset)->limit($pages->limit)->all();
return $this->render('search_page', ['searchText' => $searchText, 'searchResult' => $models, 'pages' => $pages, 'totalPosts' => $totalSphinx]);
}
else { return $this->render('search_page', ['searchText' => $searchText, 'totalPosts' => 0]); }
}
else { return $this->redirect(URL::home()); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.