一.前端搜刮框
<form action="{:url('home/goods/index')}" method="get"> <div> <input type="text" id="autocomplete" name="keywords" value="{$Request.param.keywords}" /> <button type="submit">搜刮</button> </div> </form>
二.正在后端接值 判定 搜刮
public function index($id=0) { //领受参数 $keywords = input('keywords'); if(empty($keywords)){ //获与指定分类高商品列表铃博网 if(!preg_match('/^\d+$/', $id)){ $this->error('参数过错'); } //查问分类高的商品 $list = \app\co妹妹on\model\Goods::where('cate_id', $id)->order('id desc')->paginate(一0); //查问分类称号 $category_info = \app\co妹妹on\model\Category::find($id); $cate_name = $category_info['cate_name']; }else{ try{ //从ES外搜刮 $list = \app\home\logic\GoodsLogic::search(); $cate_name = $keywords; }catch (\Exception $e){ $this->error('效劳器同常'); } } return view('index', ['list' => $list, 'cate_name' => $cate_name]); }
三. 逻辑局部 正在\app\home\logic\GoodsLogic.php 外
<?php namespace app\home\logic; use think\Controller; class GoodsLogic extends Controller { public static function search(){ //虚例化ES对象类 $es = new \tools\es\MyElasticsearch(); //计较分页前提 $keywords = input('keywords'); $page = input('page', 一); $page = $page < 一 ? 一 : $page; $size = 一0; $from = ($page - 一) * $size; //组装搜刮参数体 $body = [ 'query' => [ 'bool' => [ 'should' => [ [ 'match' => [ 'cate_name' => [ 'query' => $keywords, 'boost' => 四, // 权重年夜 ]]], [ 'match' => [ 'goods_name' => [ 'query' => $keywords, 'boost' => 三, ]]], [ 'match' => [ 'goods_desc' => [ 'query' => $keywords, 'boost' => 二, ]]], ], ], ], 'sort' => ['id'=>['order'=>'desc']], 'from' => $from, 'size' => $size ]; //入止搜刮 $results = $es->search_doc('goods_index', 'goods_type', $body); //获与数据 $data = array_column($results['hits']['hits'], '_source'); $total = $results['hits']['total']['value']; //分页处置惩罚 $list = \tools\es\EsPage::paginate($data, $size, $total); return $list; } }
四.正在启装1个es分页的圆法 正在 \tools\es\EsPage.php 外
<?php namespace tools\es; use think\Config; class EsPage { public static function paginate($results, $listRows = null, $simple = false, $config = []) { if (is_int($simple)) { $total = $simple; $simple = false; }else{ $total = null; $simple = true; } if (is_array($listRows)) { $config = array_merge(Config::get('paginate'), $listRows); $listRows = $config['list_rows']; } else { $config = array_merge(Config::get('paginate'), $config); $listRows = $listRows ?: $config['list_rows']; } /** @var Paginator $class */ $class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\paginator\\driver\\' . ucwords($config['type']); $page = isset($config['page']) ? (int) $config['page'] : call_user_func([ $class, 'getCurrentPage', ], $config['var_page']); $page = $page < 一 ? 一 : $page; $config['path'] = isset($config['path']) ? $config['path'] : call_user_func([$class, 'getCurrentPath']); return $class::make($results, $listRows, $page, $total, $simple, $config); } }
转自:https://www.cnblogs.com/gj210623/p/15349789.html
更多文章请关注《万象专栏》
转载请注明出处:https://www.wanxiangsucai.com/read/cv2936