Last active
July 15, 2022 11:20
-
-
Save forecho/960ead28b01f2f7ef958 to your computer and use it in GitHub Desktop.
Yii2 使用 CoreSeek 搜索 Demo 更多信息参考官方文档 http://www.coreseek.cn/products-install/step_by_step/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function actionSearch() | |
{ | |
$keyword = Yii::$app->request->get('keyword'); | |
if (empty($keyword)) $this->goHome(); | |
require_once(Yii::getAlias('@common/helpers') . '/sphinxapi.php'); | |
$cl = new \SphinxClient (); | |
$cl->SetServer('127.0.0.1', 9312); | |
//以下设置用于返回数组形式的结果 | |
$cl->SetArrayResult(true); | |
//取从头开始的前20条数据,0,20类似SQl语句的LIMIT 0,20 | |
$cl->SetLimits(0, 1000); | |
//在做索引时,没有进行 sql_attr_类型 设置的字段,可以作为“搜索字符串”,进行全文搜索 | |
$res = $cl->Query($keyword, "*"); //"*"表示在所有索引里面同时搜索,"索引名称(例如test或者test,test2)"则表示搜索指定的 | |
$ids = empty($res['matches']) ? [] : ArrayHelper::getColumn($res['matches'], 'id'); | |
$dataProvider = new ActiveDataProvider([ | |
'query' => User::find()->where(['id' => $ids]), | |
]); | |
return $this->render('index', [ | |
'dataProvider' => $dataProvider, | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment