首頁 >php框架 >Laravel >詳解Laravel8 ES封裝及其使用方法

詳解Laravel8 ES封裝及其使用方法

藏色散人
藏色散人轉載
2022-11-23 17:03:552111瀏覽

這篇文章跟大家介紹Laravel8的相關知識,內容包括講解Laravel8 ES封裝以及使用方法,希望對大家有幫助!

詳解Laravel8 ES封裝及其使用方法

【相關推薦:laravel影片教學

composer 安裝

composer require elasticsearch/elasticsearch

ES 封裝

<?php
namespace App\Es;
use Elasticsearch\ClientBuilder;
class MyEs
{
    //ES客户端链接
    private $client;
    /**
     * 构造函数
     * MyElasticsearch constructor.
     */
    public function __construct()
    {
        $this->client = ClientBuilder::create()->setHosts([&#39;127.0.0.1:9200&#39;])->build();
    }
    /**
     * 判断索引是否存在
     * @param string $index_name
     * @return bool|mixed|string
     */
    public function exists_index($index_name = &#39;test_ik&#39;)
    {
        $params = [
            &#39;index&#39; => $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;
        }
    }
    /**
     * 创建索引
     * @param string $index_name
     * @return array|mixed|string
     */
    public function create_index($index_name = &#39;test_ik&#39;) { // 只能创建一次
        $params = [
            &#39;index&#39; => $index_name,
            &#39;body&#39; => [
                &#39;settings&#39; => [
                    &#39;number_of_shards&#39; => 5,
                    &#39;number_of_replicas&#39; => 1
                ]
            ]
        ];
        try {
            return $this->client->indices()->create($params);
        } catch (\Elasticsearch\Common\Exceptions\BadRequest400Exception $e) {
            $msg = $e->getMessage();
            $msg = json_decode($msg,true);
            return $msg;
        }
    }
    /**
     * 删除索引
     * @param string $index_name
     * @return array
     */
    public function delete_index($index_name = &#39;test_ik&#39;) {
        $params = [&#39;index&#39; => $index_name];
        $response = $this->client->indices()->delete($params);
        return $response;
    }
    /**
     * 添加文档
     * @param $id
     * @param $doc [&#39;id&#39;=>100, &#39;title&#39;=>&#39;phone&#39;]
     * @param string $index_name
     * @param string $type_name
     * @return array
     */
    public function add_doc($id,$doc,$index_name = &#39;test_ik&#39;,$type_name = &#39;goods&#39;) {
        $params = [
            &#39;index&#39; => $index_name,
            &#39;type&#39; => $type_name,
            &#39;id&#39; => $id,
            &#39;body&#39; => $doc
        ];
        $response = $this->client->index($params);
        return $response;
    }
    /**
     * 判断文档存在
     * @param int $id
     * @param string $index_name
     * @param string $type_name
     * @return array|bool
     */
    public function exists_doc($id = 1,$index_name = &#39;test_ik&#39;,$type_name = &#39;goods&#39;) {
        $params = [
            &#39;index&#39; => $index_name,
            &#39;type&#39; => $type_name,
            &#39;id&#39; => $id
        ];
        $response = $this->client->exists($params);
        return $response;
    }
    /**
     * 获取文档
     * @param int $id
     * @param string $index_name
     * @param string $type_name
     * @return array
     */
    public function get_doc($id = 1,$index_name = &#39;test_ik&#39;,$type_name = &#39;goods&#39;) {
        $params = [
            &#39;index&#39; => $index_name,
            &#39;type&#39; => $type_name,
            &#39;id&#39; => $id
        ];
        $response = $this->client->get($params);
        return $response;
    }
    /**
     * 更新文档
     * @param int $id
     * @param string $index_name
     * @param string $type_name
     * @param array $body [&#39;doc&#39; => [&#39;title&#39; => &#39;苹果手机iPhoneX&#39;]]
     * @return array
     */
    public function update_doc($id = 1,$index_name = &#39;test_ik&#39;,$type_name = &#39;goods&#39;, $body=[]) {
        // 可以灵活添加新字段,最好不要乱添加
        $params = [
            &#39;index&#39; => $index_name,
            &#39;type&#39; => $type_name,
            &#39;id&#39; => $id,
            &#39;body&#39; => $body
        ];
        $response = $this->client->update($params);
        return $response;
    }
    /**
     * 删除文档
     * @param int $id
     * @param string $index_name
     * @param string $type_name
     * @return array
     */
    public function delete_doc($id = 1,$index_name = &#39;test_ik&#39;,$type_name = &#39;goods&#39;) {
        $params = [
            &#39;index&#39; => $index_name,
            &#39;type&#39; => $type_name,
            &#39;id&#39; => $id
        ];
        $response = $this->client->delete($params);
        return $response;
    }
    /**
     * 搜索文档 (分页,排序,权重,过滤)
     * @param string $index_name
     * @param string $type_name
     * @param array $body
     * $body = [
                &#39;query&#39; => [
                    &#39;match&#39; => [
                        &#39;fang_name&#39; => [
                            &#39;query&#39; => $fangName
                        ]
                    ]
                ],
                &#39;highlight&#39;=>[
                    &#39;fields&#39;=>[
                        &#39;fang_name&#39;=>[
                            &#39;pre_tags&#39;=>[
                                &#39;<span style="color: red">&#39;
                            ],
                            &#39;post_tags&#39;=>[
                                &#39;</span>&#39;
                            ]
                        ]
                    ]
                ]
            ];
     * @return array
     */
    public function search_doc($index_name = "test_ik",$type_name = "goods",$body=[]) {
        $params = [
            &#39;index&#39; => $index_name,
            &#39;type&#39; => $type_name,
            &#39;body&#39; => $body
        ];
        $results = $this->client->search($params);
        return $results;
    }
}

將資料表中所有資料加入ES

public function esAdd()
    {
        $data = Good::get()->toArray();
        $es = new MyEs();
        if (!$es->exists_index(&#39;goods&#39;)) {
            //创建es索引,es的索引相当于MySQL的数据库
            $es->create_index(&#39;goods&#39;);
        }
        foreach ($data as $model) {
            $es->add_doc($model[&#39;id&#39;], $model, &#39;goods&#39;, &#39;_doc&#39;);
        }
    }

每在MySQL 中新增一條數據,在es 裡也加入一條

#直接將程式碼補在MySQL 加入到函式庫的邏輯方法裡即可

        //添加至MySQL
        $res=Good::insertGetId($arr);
        $es = new MyEs();
        if (!$es->exists_index(&#39;goods&#39;)) {
            $es->create_index(&#39;goods&#39;);
        }
        //添加至es
        $es->add_doc($res, $arr, &#39;goods&#39;, &#39;_doc&#39;);
        return $res;

進行MySQL 資料修改時,也更新es 的資料

直接將程式碼補在MySQL 修改資料的邏輯方法裡即可

      //修改MySQL的数据
        $res=Good::where(&#39;id&#39;,$id)->update($arr);
        $es = new MyEs();
        if (!$es->exists_index(&#39;goods&#39;)) {
            $es->create_index(&#39;goods&#39;);
        }
        //修改es的数据
        $es->update_doc($id, &#39;goods&#39;, &#39;_doc&#39;,[&#39;doc&#39;=>$arr]);
        return $res;

透過ES 實現搜尋功能

public function search()
    {
        //获取搜索值
        $search = \request()->get(&#39;search&#39;);
        if (!empty($search)) {
            $es = new MyEs();
            $body = [
                &#39;query&#39; => [
                    &#39;match&#39; => [
                        &#39;title&#39; => [
                            &#39;query&#39; => $search
                        ]
                    ]
                ],
                &#39;highlight&#39;=>[
                    &#39;fields&#39;=>[
                        &#39;title&#39;=>[
                            &#39;pre_tags&#39;=>[
                                &#39;<span style="color: red">&#39;
                            ],
                            &#39;post_tags&#39;=>[
                                &#39;</span>&#39;
                            ]
                        ]
                    ]
                ]
            ];
            $res = $es->search_doc(&#39;goods&#39;, &#39;_doc&#39;, $body);
            $data = array_column($res[&#39;hits&#39;][&#39;hits&#39;], &#39;_source&#39;);
            foreach ($data as $key=>&$v){
                 $v[&#39;title&#39;] = $res[&#39;hits&#39;][&#39;hits&#39;][$key][&#39;highlight&#39;][&#39;title&#39;][0];
            }
            unset($v);
            return $data;
        }
        $data = Good::get();
        return $data;
    }

另,補充es 分頁搜尋

如果是在微信小程式中使用的話,運用上拉觸底事件即可

此功能是在上面搜尋功能之上新增程式碼實現的

1. 接收前台小程式傳遞來的目前頁

2. 呼叫es 封裝類別的搜尋方法時,多傳兩個參數

3. 在es 封裝類別的搜尋方法中增加兩個形參

搜尋後搜尋值高亮顯示

如果是在微信小程式中使用的話,是直接將標籤和值一起輸出到頁面的,加入解析富文本的標籤可以將標籤轉換格式,達到高亮效果

<rich-text nodes="{{item.title}}"></rich-text>

原文作者:amateur

轉自連結:https://learnku.com/articles/66177

以上是詳解Laravel8 ES封裝及其使用方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:learnku.com。如有侵權,請聯絡admin@php.cn刪除