• es的封装 拿去直接用


    可以放置到你的类库文件,使用的时候直接引用

    下载扩展包

    全文检索

    composer require elasticsearch/elasticsearch
    

      

    <?php
    
    namespace appcommonlib;
    
    use ElasticsearchClientBuilder;
    
    class ES
    {
        //ES客户端链接
        private $client;
    
        /**
         * 初始化ES连接
         * ES constructor.
         */
        public function __construct($index)
        {
            $params = array(
                '127.0.0.1:9200'
            );
            $this->client = ClientBuilder::create()->setHosts($params)->build();
            // 查看是否建立索引,如果没有,则创建
            if (!$this->exists_index($index)) {
                $this->create_index($index);
            }
    
            return $this->client;
        }
    
        /**
         * 判断索引是否存在
         * @param string $index_name
         * @return bool|mixed|string
         */
        public function exists_index($index_name = 'test_ik')
        {
            $params = [
                'index' => $index_name
            ];
            try {
                return $this->client->indices()->exists($params);
            } catch (Exception $e) {
                return false;
            }
        }
    
        /**
         * 创建索引
         * @param string $index_name
         * @return array|mixed|string
         */
        public function create_index($index_name = 'test_ik')
        {
            // 只能创建一次
            $params = [
                'index' => $index_name,
                'body' => [
                    'settings' => [
                        'number_of_shards' => 1,
                        'number_of_replicas' => 1
                    ]
                ]
            ];
            try {
                return $this->client->indices()->create($params);
            } catch (Exception $e) {
                return false;
            }
        }
    
        /**
         * 删除索引
         * @param string $index_name
         * @return array
         */
        public function delete_index($index_name = 'test_ik')
        {
            $params = ['index' => $index_name];
            $response = $this->client->indices()->delete($params);
            return $response;
        }
    
        /**
         * 添加文档
         * @param $params
         * $params = [
         * 'index' => "es",
         * 'type' => "article",
         * "body" => [
         * "title" => "",
         * ]
         * ];
         * @return array
         */
        public function add_doc($params)
        {
    
            return $this->client->index($params);
        }
    
        /**
         * 判断文档存在
         * @param int $id
         * @param string $index_name
         * @param string $type_name
         * @return array|bool
         */
        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;
        }
    
        /**
         * 获取文档
         * @param int $id
         * @param string $index_name
         * @param string $type_name
         * @return array
         */
        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;
        }
    
    
        /**
         * 修改文档
         *$params = [
            'index' => "es",
            'type' => "article",
            'id' => "OIwzxXgBzF70K-DobSSC",
            "body" => [
                "doc" => [
                    "title" => "6100万颗心的共同记忆 再次C位亮相,闪耀全球!",
                    "desn" => "刚刚过去的这个清明节,与往年一样,有人凭寄哀思,有人缅怀忠魂。但也有一些瞬间,让人记起久久不能释怀,给这个特殊节气增添了一些格外不同的味道。"
                ]
            ]
        ];
         * @param array $params
         * @return array
         */
        public function update_doc($params = [])
        {
            $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 = 'test_ik', $type_name = 'goods')
        {
            $params = [
                'index' => $index_name,
                'type' => $type_name,
                'id' => $id
            ];
    
            $response = $this->client->delete($params);
            return $response;
        }
    
        /**
         * 搜索文档 (分页,排序,权重,过滤)
         * @param string $index_name
         * @param string $type_name
         * @param array $body
         * $body = [
         * 'query' => [
         * 'bool' => [
         * 'should' => [
         * [
         * 'match' => [
         * 'cate_name' => [
         * 'query' => $keywords,
         * 'boost' => 4, // 权重大
         * ]
         * ]
         * ],
         * [
         * 'match' => [
         * 'goods_name' => [
         * 'query' => $keywords,
         * 'boost' => 3,
         * ]
         * ]
         * ],
         * [
         * 'match' => [
         * 'goods_introduce' => [
         * 'query' => $keywords,
         * 'boost' => 2,
         * ]
         * ]
         * ]
         * ],
         * ],
         * ],
         * 'sort' => ['id'=>['order'=>'desc']],
         * 'from' => $from,
         * 'size' => $size
         * ];
         * @return array
         */
        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;
        }
    
    }

    带全文检索,直接可以去用

    <?php
    
    namespace appcommonlib;
    
    use ElasticsearchClientBuilder;
    
    class ES
    {
        //ES客户端链接
        private $client;
    
        /**
         * 初始化ES连接
         * ES constructor.
         */
        public function __construct($index)
        {
            $params = array(
                '127.0.0.1:9200'
            );
            $this->client = ClientBuilder::create()->setHosts($params)->build();
            // 查看是否建立索引,如果没有,则创建
            if (!$this->exists_index($index)) {
                $this->create_index($index);
            }
    
            return $this->client;
        }
    
        /**
         * 判断索引是否存在
         * @param string $index_name
         * @return bool|mixed|string
         */
        public function exists_index($index_name = 'test_ik')
        {
            $params = [
                'index' => $index_name
            ];
            try {
                return $this->client->indices()->exists($params);
            } catch (Exception $e) {
                return false;
            }
        }
    
        /**
         * 创建索引
         * @param string $index_name
         * @return array|mixed|string
         */
        public function create_index($index_name = 'test_ik')
        {
            // 只能创建一次
            $params = [
                'index' => $index_name,
                'body' => [
                    'settings' => [
                        'number_of_shards' => 1,
                        'number_of_replicas' => 1
                    ]
                ]
            ];
            try {
                return $this->client->indices()->create($params);
            } catch (Exception $e) {
                return false;
            }
        }
    
        /**
         * 删除索引
         * @param string $index_name
         * @return array
         */
        public function delete_index($index_name = 'test_ik')
        {
            $params = ['index' => $index_name];
            $response = $this->client->indices()->delete($params);
            return $response;
        }
    
        /**
         * 添加文档
         * @param $params
         * $params = [
         * 'index' => "es",
         * 'type' => "article",
         * "body" => [
         * "title" => "",
         * ]
         * ];
         * @return array
         */
        public function add_doc($params)
        {
    
            return $this->client->index($params);
        }
    
        /**
         * 判断文档存在
         * @param int $id
         * @param string $index_name
         * @param string $type_name
         * @return array|bool
         */
        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;
        }
    
        /**
         * 获取文档
         * @param int $id
         * @param string $index_name
         * @param string $type_name
         * @return array
         */
        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;
        }
    
        /**
         * 修改文档
         *$params = [
            'index' => "es",
            'type' => "article",
            'id' => "OIwzxXgBzF70K-DobSSC",
            "body" => [
                "doc" => [
                    "title" => "6100万颗心的共同记忆 再次C位亮相,闪耀全球!",
                    "desn" => "刚刚过去的这个清明节,与往年一样,有人凭寄哀思,有人缅怀忠魂。但也有一些瞬间,让人记起久久不能释怀,给这个特殊节气增添了一些格外不同的味道。"
                ]
            ]
        ];
         * @param array $params
         * @return array
         */
        public function update_doc($params = [])
        {
            $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 = 'test_ik', $type_name = 'goods')
        {
            $params = [
                'index' => $index_name,
                'type' => $type_name,
                'id' => $id
            ];
    
            $response = $this->client->delete($params);
            return $response;
        }
    
        /**
         * 搜索文档 (分页,排序,权重,过滤)
         * @param string $index_name
         * @param string $type_name
         * @param array $body
         * $body = [
         * 'query' => [
         * 'bool' => [
         * 'should' => [
         * [
         * 'match' => [
         * 'cate_name' => [
         * 'query' => $keywords, 要搜索的值
         * 'boost' => 4, // 权重大
         * ]
         * ]
         * ],
         * [
         * 'match' => [
         * 'goods_name(字段名)' => [
         * 'query' => $keywords,
         * 'boost' => 3,
         * ]
         * ]
         * ],
         * [
         * 'match' => [
         * 'goods_introduce' => [
         * 'query' => $keywords,
         * 'boost' => 2,
         * ]
         * ]
         * ]
         * ],
         * ],
         * ],
         * 'sort' => ['id'=>['order'=>'desc']],
         * 'from' => $from,
         * 'size' => $size
         * ];
         * @return array
         */
        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 get_all($index_name,$type_name){
            $params = [
                'index'=>$index_name,
                'type' => $type_name
            ];
            $response = $this->client->search($params);
            return $response;
        }
    
    }
  • 相关阅读:
    update jdk安装与配置
    Ubuntu 14.04 FTP服务器--vsftpd的安装和配置
    ubuntu 修改开机分辨率
    win7下通过easyBCD引导安装Ubuntu14.04
    IOS swift2.0 Get HTTP 数据请求
    oracle数据库中文汉字排序
    ORACLE日期函数大全!
    Oralce常用命令
    在linux环境下安装redis
    PHP常用函数大全
  • 原文地址:https://www.cnblogs.com/cyxng/p/14654354.html
Copyright © 2020-2023  润新知