这篇文章主要介绍“Laravel8 ES怎么封装及使用”,在日常操作中,相信很多人在Laravel8 ES怎么封装及使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Laravel8 ES怎么封装及使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
composer 安装
composer require elasticsearch/elasticsearch
ES 封装
将数据表中所有数据添加至 ES 每在 MySQL 里添加一条数据,在 es 里也添加一条<?phpnamespace App\Es;use Elasticsearch\ClientBuilder;class MyEs{ //ES客户端链接 private $client; public function __construct() { $this->client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build(); } public function exists_index($index_name = 'test_ik') { $params = [ 'index' => $index_name ]; try { return $this->client->indices()->exists($params); } catch (\Elasticsearch\Common\Exceptions\BadRequest400Exception $e) { $msg = $e->getMessage(); $msg = json_decode($msg,true); return $msg; } } public function create_index($index_name = 'test_ik') { // 只能创建一次 $params = [ 'index' => $index_name, 'body' => [ 'settings' => [ 'number_of_shards' => 5, 'number_of_replicas' => 1 ] ] ]; try { return $this->client->indices()->create($params); } catch (\Elasticsearch\Common\Exceptions\BadRequest400Exception $e) { $msg = $e->getMessage(); $msg = json_decode($msg,true); return $msg; } } public function delete_index($index_name = 'test_ik') { $params = ['index' => $index_name]; $response = $this->client->indices()->delete($params); return $response; } public function add_doc($id,$doc,$index_name = 'test_ik',$type_name = 'goods') { $params = [ 'index' => $index_name, 'type' => $type_name, 'id' => $id, 'body' => $doc ]; $response = $this->client->index($params); return $response; } public function exists_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') { $params = [ 'index' => $index_name, 'type' => $type_name, 'id' => $id ]; $response = $this->client->exists($params); return $response; } public function get_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') { $params = [ 'index' => $index_name, 'type' => $type_name, 'id' => $id ]; $response = $this->client->get($params); return $response; } public function update_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods', $body=[]) { // 可以灵活添加新字段,最好不要乱添加 $params = [ 'index' => $index_name, 'type' => $type_name, 'id' => $id, 'body' => $body ]; $response = $this->client->update($params); return $response; } public function delete_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') { $params = [ 'index' => $index_name, 'type' => $type_name, 'id' => $id ]; $response = $this->client->delete($params); return $response; } public function search_doc($index_name = "test_ik",$type_name = "goods",$body=[]) { $params = [ 'index' => $index_name, 'type' => $type_name, 'body' => $body ]; $results = $this->client->search($params); return $results; }}
public function esAdd() { $data = Good::get()->toArray(); $es = new MyEs(); if (!$es->exists_index('goods')) { //创建es索引,es的索引相当于MySQL的数据库 $es->create_index('goods'); } foreach ($data as $model) { $es->add_doc($model['id'], $model, 'goods', '_doc'); } }
直接将代码补在 MySQL 添加入库的逻辑方法里即可
进行 MySQL 数据修改时,也更新 es 的数据 //添加至MySQL $res=Good::insertGetId($arr); $es = new MyEs(); if (!$es->exists_index('goods')) { $es->create_index('goods'); } //添加至es $es->add_doc($res, $arr, 'goods', '_doc'); return $res;
直接将代码补在 MySQL 修改数据的逻辑方法里即可
通过 ES 实现搜索功能 另,补充 es 分页搜索 //修改MySQL的数据 $res=Good::where('id',$id)->update($arr); $es = new MyEs(); if (!$es->exists_index('goods')) { $es->create_index('goods'); } //修改es的数据 $es->update_doc($id, 'goods', '_doc',['doc'=>$arr]); return $res;
public function search() { //获取搜索值 $search = \request()->get('search'); if (!empty($search)) { $es = new MyEs(); $body = [ 'query' => [ 'match' => [ 'title' => [ 'query' => $search ] ] ], 'highlight'=>[ 'fields'=>[ 'title'=>[ 'pre_tags'=>[ '<span style="color: red">' ], 'post_tags'=>[ '</span>' ] ] ] ] ]; $res = $es->search_doc('goods', '_doc', $body); $data = array_column($res['hits']['hits'], '_source'); foreach ($data as $key=>&$v){ $v['title'] = $res['hits']['hits'][$key]['highlight']['title'][0]; } unset($v); return $data; } $data = Good::get(); return $data; }
如果是在微信小程序中使用的话,运用上拉触底事件即可
此功能是在上面搜索功能之上添加代码实现的
接收前台小程序传递来的当前页
调用 es 封装类的搜索方法时,多传两个参数
在 es 封装类的搜索方法中增加两个形参
搜索后搜索值高亮显示
如果是在微信小程序中使用的话,是直接将标签和值一起输出到页面的,加入解析富文本的标签可以将标签转化格式,达到高亮效果
<rich-text nodes="{{item.title}}"></rich-text>
到此,关于“Laravel8 ES怎么封装及使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!