强烈建议先阅读下面这篇文章
创建SeachAPI
在APP\HttpCrontroller\Api
目录下创建Search.php
namespace App\HttpController\Api;
use App\Model\ES\EsVideo;
use EasySwoole\Http\Message\Status;
class Search extends Base
{
/*
* 站内搜索服务API*/
public function index()
{
// 获取用户传递的搜索内容
$keyword = trim($this->params['keyword']);
if (empty($keyword)) {
return $this->writeJson(Status::CODE_OK, 'ok', $this->getPagingDatas(0, []));
}
$esCient = new EsVideo();
$result = $esCient->searchByVideo($keyword);
}
}
注意:
- 继承类为
App\HttpController\Api\Base.php,在[EasySwoole的基本使用][2]中重定义
writeJson`
方法. EasySwoole\Http\Message\Status
为EasySwoole
的状态码类.getPagingDatas()
方法是继承的Base.php
封装的分页方法.详细请看静态化API-Crontab方案优化.- 对于搜索出来的数据也需要进行分页的优化.
优化EsBase
在App\Model\ES\EsBase.php
添加ElasticSearch分页参数$from,$size
.修改后:
/*
* 搜索视频名/内容
* $index : 索引名
* $type : 类型名
* $default_video_fields[] : 字段名
* $from : 当前页
* $size : 默认每页显示数量
* */
public function searchByVideo($search, $from = 0, $size = 10, $fields = [])
{
$search = trim($search);
if (empty($search)) {
return [];
}
if (empty($fields)) {
$fields = $this->default_video_fields;
}
$params = [
"index" => $this->index,
"type" => $this->type,
'body' => [
'query' => [
'multi_match' => [
'query' => $search,
'fields' => $fields
]
],
'from' => $from,
'size' => $size
]
];
$result = $this->client->search($params);
return $result;
}
修改SeachAPI
添加搜索分页功能
/*
* 站内搜索服务API*/
public function index()
{
// 获取用户传递的搜索内容
$keyword = trim($this->params['keyword']);
if (empty($keyword)) {
return $this->writeJson(Status::CODE_OK, 'ok', $this->getPagingDatas(0, []));
}
$esCient = new EsVideo();
$result = $esCient->searchByVideo($keyword, $this->params['page'], $this->params['size']);
// 查询结果集
$hits = $result['hits']['hits'];
// 符合查询数量
$total = $result['hits']['total']['value'];
if(empty($total)){
return $this->writeJson(Status::CODE_OK, 'ok', $this->getPagingDatas(0, []));
}
//return $this->writeJson(Status::CODE_OK, 'ok', $hits);
$searchData = [];
foreach ($hits as $val) {
$searchData[] = [
'name' => !empty($val['_source']['name']) ? $val['_source']['name'] : '',
'cat_id' => !empty($val['_source']['cat_id']) ? $val['_source']['cat_id'] : 0,
'image' => !empty($val['_source']['image']) ? $val['_source']['image'] : '',
'type' => !empty($val['_source']['type']) ? $val['_source']['type'] : 0,
'content' => !empty($val['_source']['content']) ? $val['_source']['content'] : '',
'uploader' => !empty($val['_source']['uploader']) ? $val['_source']['uploader'] : 0,
'video_id' => !empty($val['_source']['video_id']) ? $val['_source']['video_id'] : '',
'video_duration' => !empty($val['_source']['video_duration']) ? gmstrftime("%H:%I:%S", $val['video_duration']) : '00:00:00',
'create_time' => !empty($val['_source']['create_time']) ? date('Y-m-d H:i:s', $val['create_time']) : 0,
'update_time' => !empty($val['_source']['update_time']) ? date('Y-m-d H:i:s', $val['update_time']) : 0,
'status' => !empty($val['_source']['status']) ? $val['_source']['status'] : 0,
'keyword' => $keyword,
];
}
return $this->writeJson(Status::CODE_OK, 'ok', $this->getPagingDatas($total, $searchData));
}
关于$this->params
是在继承的Base.php
中
// 请求的参数数据
public $params = [];
public function onRequest(?string $action): ?bool
{
// 如果浏览器没有传uid则任务没有权限。返回无权限
// $uid = $this->request()->getRequestParam('uid');
// if(!$uid){
// $this->writeJson(201,'无权限');
// return false;
// }
// 通过验证执行下面逻辑
$this->getParmas();
return true;
}
/*获取参数值*/
public function getParmas()
{
$params = $this->request()->getRequestParam();
$params['page'] = !empty($params['page']) ? intval($params['page']) : intval(\Yaconf::get('video_page.default_page'));
$params['size'] = !empty($params['size']) ? intval($params['size']) : intval(\Yaconf::get('video_page.default_size'));
$params['from'] = ($params['page'] - 1) * $params['size'];
$this->params = $params;
}
请求http://127.0.0.1:9511/api/search/index?keyword=二&size=2&page=2
{
"code":200,
"msg":"ok",
"result":{
"total_page":2,
"page_size":2,
"count":4,
"lists":[
{
"name":"二滑小天使",
"cat_id":0,
"image":"",
"type":0,
"content":"你好啊",
"uploader":0,
"video_id":"",
"video_duration":"00:00:00",
"create_time":0,
"update_time":0,
"status":0,
"keyword":"二"
},
{
"name":"二滑大魔王",
"cat_id":0,
"image":"",
"type":0,
"content":"你好啊",
"uploader":0,
"video_id":"",
"video_duration":"00:00:00",
"create_time":0,
"update_time":0,
"status":0,
"keyword":"二"
}
]
}
}
成功~