Skip to content

Instantly share code, notes, and snippets.

@AlexMcowkin
Created April 7, 2016 04:16
Show Gist options
  • Save AlexMcowkin/5b7f174c5aeacc3e8264e57f369d585c to your computer and use it in GitHub Desktop.
Save AlexMcowkin/5b7f174c5aeacc3e8264e57f369d585c to your computer and use it in GitHub Desktop.
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