| 
 | 
 | 
 <?php
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 namespace Lib\Es;
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 use Elasticsearch\ClientBuilder;
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 /**
 | 
| 
 | 
 | 
  * @see https://github.com/elastic/elasticsearch-php/tree/7.17
 | 
| 
 | 
 | 
  * @author:dc
 | 
| 
 | 
 | 
  * @time 2023/6/5 10:13
 | 
| 
 | 
 | 
  * Class Es
 | 
| 
 | 
 | 
  * @package GlobalSo\Tool\Es
 | 
| 
 | 
 | 
  */
 | 
| 
 | 
 | 
 class Es {
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 //    private $host = 'https://es-gqtfpyon.public.tencentelasticsearch.com:9200'; // 黑格那个服务器
 | 
| 
 | 
 | 
 //    private $host = 'http://elastic:1qOtfZhqy4B7IXdIpl_W@192.168.80.129:9200';
 | 
| 
 | 
 | 
 //    private $host = 'https://es-az664rii.public.tencentelasticsearch.com:9200'; // aicc 服务器
 | 
| 
 | 
 | 
     private $host = [
 | 
| 
 | 
 | 
 //        'http://elastic:1qOtfZhqy4B7IXdIpl_W@192.168.80.129:9200',
 | 
| 
 | 
 | 
         'https://elastic:rEHsd8xf4xGJKHdD@es-gqtfpyon.public.tencentelasticsearch.com:9200'
 | 
| 
 | 
 | 
     ]; //内网地址  公网要加ip白名单
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     /**
 | 
| 
 | 
 | 
      * @var \Elasticsearch\Client
 | 
| 
 | 
 | 
      */
 | 
| 
 | 
 | 
     protected $client;
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     /**
 | 
| 
 | 
 | 
      * @var string
 | 
| 
 | 
 | 
      */
 | 
| 
 | 
 | 
     protected $index;
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     /**
 | 
| 
 | 
 | 
      * Es constructor.
 | 
| 
 | 
 | 
      * @param $index
 | 
| 
 | 
 | 
      */
 | 
| 
 | 
 | 
     public function __construct($index)
 | 
| 
 | 
 | 
     {
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         $this->index = $index;
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         $this->client = ClientBuilder::create()
 | 
| 
 | 
 | 
             ->setHosts($this->host)
 | 
| 
 | 
 | 
 //            ->setBasicAuthentication($user, $password)
 | 
| 
 | 
 | 
 //            ->setCABundle('path/to/http_ca.crt')
 | 
| 
 | 
 | 
             ->build();
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     /**
 | 
| 
 | 
 | 
      * 搜索
 | 
| 
 | 
 | 
      * @param array $body
 | 
| 
 | 
 | 
      * @param int $from
 | 
| 
 | 
 | 
      * @param int $size
 | 
| 
 | 
 | 
      * @param array $sort
 | 
| 
 | 
 | 
      * @return array
 | 
| 
 | 
 | 
      * @author:dc
 | 
| 
 | 
 | 
      * @time 2023/6/5 14:35
 | 
| 
 | 
 | 
      */
 | 
| 
 | 
 | 
     public function search(array $body,int $from=0,int $size=20,array $sort= []){
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         $params = [
 | 
| 
 | 
 | 
             'index' => $this->getIndex(),
 | 
| 
 | 
 | 
             'body'  => $body,
 | 
| 
 | 
 | 
             'from'  =>  $from,
 | 
| 
 | 
 | 
             'size'  =>  $size
 | 
| 
 | 
 | 
         ];
 | 
| 
 | 
 | 
         // 排序
 | 
| 
 | 
 | 
         if($sort){
 | 
| 
 | 
 | 
             $params['sort']    =   $sort;
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         try {
 | 
| 
 | 
 | 
             $response = $this->client->search($params);
 | 
| 
 | 
 | 
         }catch (\Throwable $e) {
 | 
| 
 | 
 | 
             logs("搜索数据es:".$e->getMessage());
 | 
| 
 | 
 | 
             return [];
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         return $response;
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     /**
 | 
| 
 | 
 | 
      * @param array $params
 | 
| 
 | 
 | 
      * @return array|callable
 | 
| 
 | 
 | 
      * @author:dc
 | 
| 
 | 
 | 
      * @time 2023/6/9 15:50
 | 
| 
 | 
 | 
      */
 | 
| 
 | 
 | 
     public function get(array $param){
 | 
| 
 | 
 | 
         $params = [
 | 
| 
 | 
 | 
             'index' => $this->getIndex()
 | 
| 
 | 
 | 
         ];
 | 
| 
 | 
 | 
         if(!empty($param['id'])){
 | 
| 
 | 
 | 
             $params['id']   =   $param['id'];
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         try {
 | 
| 
 | 
 | 
             $response = $this->client->get($params);
 | 
| 
 | 
 | 
         }catch (\Throwable $e) {
 | 
| 
 | 
 | 
             logs("读取数据es:".$e->getMessage());
 | 
| 
 | 
 | 
             return [];
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         return $response;
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     /**
 | 
| 
 | 
 | 
      * 获取索引
 | 
| 
 | 
 | 
      * @return string
 | 
| 
 | 
 | 
      * @author:dc
 | 
| 
 | 
 | 
      * @time 2023/6/5 10:51
 | 
| 
 | 
 | 
      */
 | 
| 
 | 
 | 
     public function getIndex():string{
 | 
| 
 | 
 | 
         return $this->index;
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     /**
 | 
| 
 | 
 | 
      * 保存修改
 | 
| 
 | 
 | 
      * @param string $id
 | 
| 
 | 
 | 
      * @param array $data
 | 
| 
 | 
 | 
      * @return int
 | 
| 
 | 
 | 
      * @author:dc
 | 
| 
 | 
 | 
      * @time 2025/3/1 14:53
 | 
| 
 | 
 | 
      */
 | 
| 
 | 
 | 
     public function save(string $id, array $data){
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         $params['index'] = $this->getIndex();
 | 
| 
 | 
 | 
         $params['id'] = $id;
 | 
| 
 | 
 | 
         $params['body'] = $data;
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         try {
 | 
| 
 | 
 | 
             $response = $this->client->index($params);
 | 
| 
 | 
 | 
         }catch (\Throwable $e) {
 | 
| 
 | 
 | 
             logs("创建或者修改es:".$e->getMessage());
 | 
| 
 | 
 | 
             return 500;
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         return $response['_shards']['successful']?200:500;
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     /**
 | 
| 
 | 
 | 
      * 创建数据
 | 
| 
 | 
 | 
      * @param array $params
 | 
| 
 | 
 | 
      * @return int
 | 
| 
 | 
 | 
      * @author:dc
 | 
| 
 | 
 | 
      * @time 2025/3/1 14:25
 | 
| 
 | 
 | 
      */
 | 
| 
 | 
 | 
     public function create(array $data, string $id = ''){
 | 
| 
 | 
 | 
         $params['index'] = $this->getIndex();
 | 
| 
 | 
 | 
         $params['body'] =   $data;
 | 
| 
 | 
 | 
         if($id){
 | 
| 
 | 
 | 
             $params['id']   =   $id;
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
         try {
 | 
| 
 | 
 | 
         $response = $this->client->create($params);
 | 
| 
 | 
 | 
         }catch (\Throwable $e) {
 | 
| 
 | 
 | 
             logs("新增es:".$e->getMessage());
 | 
| 
 | 
 | 
             return 500;
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         return $response['_shards']['successful']?200:500;
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     /**
 | 
| 
 | 
 | 
      * 更新文档
 | 
| 
 | 
 | 
      * @param string $id
 | 
| 
 | 
 | 
      * @param array $data
 | 
| 
 | 
 | 
      * @return int
 | 
| 
 | 
 | 
      * @author:dc
 | 
| 
 | 
 | 
      * @time 2025/3/1 14:30
 | 
| 
 | 
 | 
      */
 | 
| 
 | 
 | 
     public function update(string $id, array $data){
 | 
| 
 | 
 | 
         $params['index'] = $this->getIndex();
 | 
| 
 | 
 | 
         $params = [
 | 
| 
 | 
 | 
             'index' => $this->getIndex(),
 | 
| 
 | 
 | 
             'id'    => $id,
 | 
| 
 | 
 | 
             'body'  => [
 | 
| 
 | 
 | 
                 'doc' => $data
 | 
| 
 | 
 | 
             ]
 | 
| 
 | 
 | 
         ];
 | 
| 
 | 
 | 
         try {
 | 
| 
 | 
 | 
             $response = $this->client->update($params);
 | 
| 
 | 
 | 
         }catch (\Throwable $e) {
 | 
| 
 | 
 | 
             logs("更新es:".$e->getMessage());
 | 
| 
 | 
 | 
             return 500;
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         return $response['_shards']['successful']?200:500;
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     /**
 | 
| 
 | 
 | 
      * 删除
 | 
| 
 | 
 | 
      * @param string $id
 | 
| 
 | 
 | 
      * @return array|callable|false
 | 
| 
 | 
 | 
      * @author:dc
 | 
| 
 | 
 | 
      * @time 2023/6/5 16:26
 | 
| 
 | 
 | 
      */
 | 
| 
 | 
 | 
     public function delete(string $id){
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         try {
 | 
| 
 | 
 | 
             $response = $this->client->delete([
 | 
| 
 | 
 | 
                 'index' => $this->getIndex(),
 | 
| 
 | 
 | 
                 'id' => $id
 | 
| 
 | 
 | 
             ]);
 | 
| 
 | 
 | 
         } catch (\Throwable $e) {
 | 
| 
 | 
 | 
             logs('删除es数据:'.$e->getMessage());
 | 
| 
 | 
 | 
             return false;
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         return $response;
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     /**
 | 
| 
 | 
 | 
      * @return \Elasticsearch\Client
 | 
| 
 | 
 | 
      * @author:dc
 | 
| 
 | 
 | 
      * @time 2025/3/1 16:23
 | 
| 
 | 
 | 
      */
 | 
| 
 | 
 | 
     public function getClient()
 | 
| 
 | 
 | 
     {
 | 
| 
 | 
 | 
         return $this->client;
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     /**
 | 
| 
 | 
 | 
      * 更新索引 Mapping
 | 
| 
 | 
 | 
      * @param array $params
 | 
| 
 | 
 | 
      * @author:dc
 | 
| 
 | 
 | 
      * @time 2023/6/29 11:17
 | 
| 
 | 
 | 
      */
 | 
| 
 | 
 | 
     public function putMapping(array $params,array $setting=[]){
 | 
| 
 | 
 | 
         $params = [
 | 
| 
 | 
 | 
             "body"  =>  [
 | 
| 
 | 
 | 
                 "mappings" => $params,
 | 
| 
 | 
 | 
             ]
 | 
| 
 | 
 | 
         ];
 | 
| 
 | 
 | 
         $params['index'] = $this->getIndex();
 | 
| 
 | 
 | 
         if($setting){
 | 
| 
 | 
 | 
             $params['body']['settings'] = $setting;
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         try {
 | 
| 
 | 
 | 
             $response = $this->client->indices()->create($params);
 | 
| 
 | 
 | 
         }catch (\Throwable $e) {
 | 
| 
 | 
 | 
             logs('es 创建索引 映射:'.$e->getMessage());
 | 
| 
 | 
 | 
             return false;
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         return $response['acknowledged']??false;
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     /**
 | 
| 
 | 
 | 
      * @return array|false
 | 
| 
 | 
 | 
      * @author:dc
 | 
| 
 | 
 | 
      * @time 2024/1/9 10:12
 | 
| 
 | 
 | 
      */
 | 
| 
 | 
 | 
     public function getMapping(){
 | 
| 
 | 
 | 
         $params = [];
 | 
| 
 | 
 | 
         $params['index'] = $this->getIndex();
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         try {
 | 
| 
 | 
 | 
             $response = $this->client->indices()->getMapping($params);
 | 
| 
 | 
 | 
         }catch (\Throwable $e) {
 | 
| 
 | 
 | 
             return [];
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         return $response[$this->getIndex()]['mappings']['properties']??[];
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     /**
 | 
| 
 | 
 | 
      * 判断是否存在索引
 | 
| 
 | 
 | 
      * @return bool
 | 
| 
 | 
 | 
      * @author:dc
 | 
| 
 | 
 | 
      * @time 2023/6/29 12:37
 | 
| 
 | 
 | 
      */
 | 
| 
 | 
 | 
     public function hasIndex(){
 | 
| 
 | 
 | 
         $params['index'] = $this->getIndex();
 | 
| 
 | 
 | 
         $response = $this->client->indices()->exists($params);
 | 
| 
 | 
 | 
         return $response;
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 } | 
...
 | 
...
 | 
 |