From 14301327060b14e724149e7e3ea22b025a0c195a Mon Sep 17 00:00:00 2001
From: ztt <835303992@qq.com>
Date: Mon, 27 May 2024 10:14:01 +0800
Subject: [PATCH 1/2] ES

---
 app/command/SyncGoodsToEs.php               |  23 ++
 app/common/library/elasticsearch/Client.php | 183 ++++++++++++
 app/common/service/GoodsEs.php              | 146 ++++++++++
 composer.json                               |   3 +-
 composer.lock                               | 301 +++++++++++++++++++-
 config/console.php                          |   1 +
 vendor/services.php                         |   6 +-
 7 files changed, 658 insertions(+), 5 deletions(-)
 create mode 100644 app/command/SyncGoodsToEs.php
 create mode 100644 app/common/library/elasticsearch/Client.php
 create mode 100644 app/common/service/GoodsEs.php

diff --git a/app/command/SyncGoodsToEs.php b/app/command/SyncGoodsToEs.php
new file mode 100644
index 00000000..6255698c
--- /dev/null
+++ b/app/command/SyncGoodsToEs.php
@@ -0,0 +1,23 @@
+<?php
+
+namespace app\command;
+
+use think\console\Command;
+
+class SyncGoodsToEs extends Command
+{
+
+    protected function configure()
+    {
+        // 指令配置
+        $this->setName('syncGoodsToEs')
+            ->setDescription('同步商品数据到ES');
+    }
+
+
+    protected function execute(Input $input, Output $output)
+    {
+        $output->writeln('syncGoodsToEs');
+    }
+
+}
\ No newline at end of file
diff --git a/app/common/library/elasticsearch/Client.php b/app/common/library/elasticsearch/Client.php
new file mode 100644
index 00000000..0d84acf5
--- /dev/null
+++ b/app/common/library/elasticsearch/Client.php
@@ -0,0 +1,183 @@
+<?php
+namespace app\common\library\elasticsearch;
+
+use Elastic\Elasticsearch\ClientBuilder;
+use think\Exception;
+
+class Client
+{
+    public $client;
+
+    const ES_HOST_NAME = '114.55.95.135:9200';
+
+
+    private static Client $instance;
+
+    private function __construct()
+    {
+        $this->client = ClientBuilder::create()->setHosts([self::ES_HOST_NAME])->build();
+    }
+
+    private function __clone() {
+
+    }
+
+    public static function getInstance(){
+        if (is_null(self::$instance)) {
+            self::$instance = new self();
+        }
+        return self::$instance;
+    }
+
+    public function existsIndex(String $index_name) {
+        $params = [
+            'index' => $index_name
+        ];
+        return $this->client->indices()->exists($params);
+    }
+
+    /**
+     * 创建索引
+     */
+    public function createIndex(String $index_name, array $mapping = []): array
+    {
+        if ($this->existsIndex($index_name)) {
+            throw new Exception('索引已存在'. $index_name);
+        }
+
+        //只能创建一次
+        $params = [
+            'index' => $index_name,
+            'body' => [
+                'settings' => [
+                    'number_of_shards' => 5,
+                    'number_of_replicas' => 1
+                ],
+                'mappings' => $mapping
+            ]
+        ];
+        return $this->client->indices()->create($params);
+    }
+
+    /**
+     * 删除索引
+     */
+    public function deleteIndex(String $index_name = ''): array{
+        $params = ['index' => $index_name];
+        return $this->client->indices()->delete($params);
+    }
+
+    /**
+     * 添加文档
+     * @param $id
+     * @param $doc ['id'=>100, 'title'=>'phone']
+     * @param string $index_name
+     * @param string $type_name
+     */
+    public function addDoc($id, $doc, string $index_name = 'gyx_ik', string $type_name = '_doc'): array
+    {
+        $params = [
+            'index' => $index_name,
+            'type' => $type_name,
+            'id' => $id,
+            'body' => $doc
+        ];
+
+        return $this->client->index($params);
+    }
+
+    /**
+     * 判断文档存在
+     * @param int $id
+     * @param string $index_name
+     * @param string $type_name
+     * @return bool
+     */
+    public function existsDoc(int $id = 1, string $index_name = 'gyx_ik', string $type_name = '_doc'): bool
+    {
+        $params = [
+            'index' => $index_name,
+            'type' => $type_name,
+            'id' => $id
+        ];
+
+        return $this->client->exists($params);
+
+    }
+
+    /**
+     * 获取文档
+     * @param int $id
+     * @param string $index_name
+     * @param string $type_name
+     * @return array
+     */
+    public function getDoc(int $id = 1, string $index_name = 'gyx_ik', string $type_name = '_doc'): array
+    {
+        $params = [
+            'index' => $index_name,
+            'type' => $type_name,
+            'id' => $id
+        ];
+
+        return $this->client->get($params);
+    }
+
+    /**
+     * 更新文档
+     * @param int $id
+     * @param string $index_name
+     * @param string $type_name
+     * @param array $body
+     * @return array
+     */
+    public function updateDoc(int $id = 1, array $body = [], string $index_name = 'gyx_ik', string $type_name = '_doc'): array
+    {
+        //  可以灵活添加新字段,最好不要乱添加
+        $params = [
+            'index' => $index_name,
+            'type' => $type_name,
+            'id' => $id,
+            'body' => $body
+        ];
+
+        return $this->client->update($params);
+    }
+
+    /**
+     * 删除文档
+     * @param int $id
+     * @param string $index_name
+     * @param string $type_name
+     * @return array
+     */
+    public function deleteDoc(int $id = 1, string $index_name = 'gyx_ik', string $type_name = '_doc'): array
+    {
+        $params = [
+            'index' => $index_name,
+            'type' => $type_name,
+            'id' => $id
+        ];
+
+        return $this->client->delete($params);
+    }
+
+
+    /**
+     * 搜索文档:分页,排序,权重,过滤
+     * @param string $index_name
+     * @param string $type_name
+     * @param array $body
+     * @return array
+     */
+    public function searchDoc(array $body = [], string $index_name = "gyx_ik", string $type_name = "_doc"): array
+    {
+        $params = [
+            'index' => $index_name,
+            'type' => $type_name,
+            'body' => $body
+        ];
+
+        return $this->client->search($params);
+    }
+}
diff --git a/app/common/service/GoodsEs.php b/app/common/service/GoodsEs.php
new file mode 100644
index 00000000..8e6f6233
--- /dev/null
+++ b/app/common/service/GoodsEs.php
@@ -0,0 +1,146 @@
+<?php
+
+namespace app\common\service;
+
+use app\common\library\elasticsearch\Client;
+
+class GoodsEs
+{
+    private Client $esService;
+
+    public function __construct()
+    {
+        $this->esService = Client::getInstance();
+    }
+
+    /**
+     * 查询商品列表
+     * @param $params
+     * @return
+     */
+    public function list($params)
+    {
+        $page = 1;
+        $limit = 10;
+        $body = [
+            'query' => [
+                'match_all' => []
+            ],
+            'from' => ($page - 1) * $limit,
+            'size' => $limit,
+            'sort' => [
+                'id' => [
+                    'order' => 'desc'
+                ]
+            ]
+        ];
+        return $this->esService->searchDoc($body);
+    }
+
+
+
+
+    /**
+     * 更新商品
+     */
+    public function updateDoc($goods_id)
+    {
+        $goods_id = 1;
+        $doc = [
+            'id' => $goods_id,
+            'video_title' => '少爷的甜蜜良药',
+            'director' => '吴宇森',
+            'intro' => '吴宇森导演的《少爷的甜蜜良药》是吴宇森导演的经典作品,讲述了一个boyfriend和girlfriend的故事,boyfriend因为工作原因,被拒了girlfriend的offer,然后boyfriend和girlfriend一起去一个地方,然后boyfriend和girlfriend',
+            'cover' => 'https://oss-duanju-file.luochen.com/cover/202404/62249c6a-5f57-45cf-90ac-be6de5890ce0.jpg?x-oss-process=image/resize,m_fixed,h_400,w_360',
+            'episode_count' => '12',
+            'wechat_vid' => 'wx_vid_1',
+            'duration' => '01:02:03',
+            'service_types' => '2,3',
+        ];
+        $res = $this->esService->updateDoc($goods_id, $doc);
+        var_dump($res);
+    }
+
+
+    /**
+     * 创建商品
+     * @return
+     */
+    public function createDoc($goods_id)
+    {
+        $goods_id = 1;
+        $doc = [
+            'id' => $goods_id,
+            'video_title' => '少爷的甜蜜良药',
+            'director' => '吴宇森',
+            'intro' => '吴宇森导演的《少爷的甜蜜良药》是吴宇森导演的经典作品,讲述了一个boyfriend和girlfriend的故事,boyfriend因为工作原因,被拒了girlfriend的offer,然后boyfriend和girlfriend一起去一个地方,然后boyfriend和girlfriend',
+            'cover' => 'https://oss-duanju-file.luochen.com/cover/202404/62249c6a-5f57-45cf-90ac-be6de5890ce0.jpg?x-oss-process=image/resize,m_fixed,h_400,w_360',
+            'episode_count' => '12',
+            'wechat_vid' => 'wx_vid_1',
+            'duration' => '01:02:03',
+            'service_types' => '2,3',
+        ];
+        $res = $this->esService->addDoc($goods_id, $doc);
+        var_dump($res);
+    }
+
+    /**
+     * 删除商品
+     */
+    public function deleteDoc($goods_id)
+    {
+        $res = $this->esService->deleteDoc($goods_id);
+        var_dump($res);
+    }
+
+
+
+    /**
+     * 创建商品Index
+     * @return void
+     * @throws Exception
+     */
+    function createGoodsIndex()
+    {
+        $mapping = [
+            'properties' => [
+                'id' => [
+                    'type' => 'keyword'
+                ],
+                'video_title' => [
+                    'type' => 'text',
+                    "analyzer" => "ik_smart"
+                ],
+                'director' => [
+                    'type' => 'text',
+                    "analyzer" => "ik_smart"
+                ],
+                'intro' => [
+                    'type' => 'text',
+                    "analyzer" => "ik_smart"
+                ],
+                'cover' => [
+                    'type' => 'keyword'
+                ],
+                'episode_count' => [
+                    'type' => 'keyword'
+                ],
+                'wechat_vid' => [
+                    'type' => 'keyword'
+                ],
+                'duration' => [
+                    'type' => 'keyword'
+                ],
+                'service_types' => [
+                    'type' => 'keyword'
+                ],
+            ]
+        ];
+
+        $res = $this->esService->createIndex('video_list', $mapping);
+        var_dump($res);
+    }
+
+
+
+}
\ No newline at end of file
diff --git a/composer.json b/composer.json
index d87578df..71f1ccae 100644
--- a/composer.json
+++ b/composer.json
@@ -53,7 +53,8 @@
         "overtrue/easy-sms": "^2.0",
         "overtrue/wechat": "~4.0",
         "alipaysdk/easysdk": "^2.2",
-        "wechatpay/wechatpay": "^1.4"
+        "wechatpay/wechatpay": "^1.4",
+        "elasticsearch/elasticsearch": "v8.6.0"
     },
     "require-dev": {
         "symfony/var-dumper": "^4.2"
diff --git a/composer.lock b/composer.lock
index 2059ec81..f0edd536 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "6a7996d88fd82c5b30b1a2ce37fdcce9",
+    "content-hash": "b5ed7820a7c4b5d436edd224b891a3ba",
     "packages": [
         {
             "name": "adbario/php-dot-notation",
@@ -432,6 +432,117 @@
             },
             "time": "2021-07-05T04:03:22+00:00"
         },
+        {
+            "name": "elastic/transport",
+            "version": "v8.8.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/elastic/elastic-transport-php.git",
+                "reference": "cdf9f63a16ec6bfb4c881ab89aa0e2a61fb7c20b"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/elastic/elastic-transport-php/zipball/cdf9f63a16ec6bfb4c881ab89aa0e2a61fb7c20b",
+                "reference": "cdf9f63a16ec6bfb4c881ab89aa0e2a61fb7c20b",
+                "shasum": ""
+            },
+            "require": {
+                "composer-runtime-api": "^2.0",
+                "php": "^7.4 || ^8.0",
+                "php-http/discovery": "^1.14",
+                "php-http/httplug": "^2.3",
+                "psr/http-client": "^1.0",
+                "psr/http-factory": "^1.0",
+                "psr/http-message": "^1.0 || ^2.0",
+                "psr/log": "^1 || ^2 || ^3"
+            },
+            "require-dev": {
+                "nyholm/psr7": "^1.5",
+                "php-http/mock-client": "^1.5",
+                "phpstan/phpstan": "^1.4",
+                "phpunit/phpunit": "^9.5"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "Elastic\\Transport\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "description": "HTTP transport PHP library for Elastic products",
+            "keywords": [
+                "PSR_17",
+                "elastic",
+                "http",
+                "psr-18",
+                "psr-7",
+                "transport"
+            ],
+            "support": {
+                "issues": "https://github.com/elastic/elastic-transport-php/issues",
+                "source": "https://github.com/elastic/elastic-transport-php/tree/v8.8.0"
+            },
+            "time": "2023-11-08T10:51:51+00:00"
+        },
+        {
+            "name": "elasticsearch/elasticsearch",
+            "version": "v8.6.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/elastic/elasticsearch-php.git",
+                "reference": "03b145a3fd1dc984d7c2c79638c9257b1a17f8b7"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/elastic/elasticsearch-php/zipball/03b145a3fd1dc984d7c2c79638c9257b1a17f8b7",
+                "reference": "03b145a3fd1dc984d7c2c79638c9257b1a17f8b7",
+                "shasum": ""
+            },
+            "require": {
+                "elastic/transport": "^8.5",
+                "guzzlehttp/guzzle": "^7.0",
+                "php": "^7.4 || ^8.0",
+                "psr/http-client": "^1.0",
+                "psr/http-message": "^1.0",
+                "psr/log": "^1|^2|^3"
+            },
+            "require-dev": {
+                "ext-yaml": "*",
+                "ext-zip": "*",
+                "mockery/mockery": "^1.5",
+                "nyholm/psr7": "^1.5",
+                "php-http/mock-client": "^1.5",
+                "phpstan/phpstan": "^1.4",
+                "phpunit/phpunit": "^9.5",
+                "symfony/finder": "~4.0",
+                "symfony/http-client": "^5.0|^6.0"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "Elastic\\Elasticsearch\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "description": "PHP Client for Elasticsearch",
+            "keywords": [
+                "client",
+                "elastic",
+                "elasticsearch",
+                "search"
+            ],
+            "support": {
+                "issues": "https://github.com/elastic/elasticsearch-php/issues",
+                "source": "https://github.com/elastic/elasticsearch-php/tree/v8.6.0"
+            },
+            "time": "2022-10-18T13:38:48+00:00"
+        },
         {
             "name": "ezyang/htmlpurifier",
             "version": "v4.14.0",
@@ -1983,6 +2094,194 @@
             "abandoned": "w7corp/easywechat",
             "time": "2021-12-27T13:56:47+00:00"
         },
+        {
+            "name": "php-http/discovery",
+            "version": "1.19.4",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/php-http/discovery.git",
+                "reference": "0700efda8d7526335132360167315fdab3aeb599"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/php-http/discovery/zipball/0700efda8d7526335132360167315fdab3aeb599",
+                "reference": "0700efda8d7526335132360167315fdab3aeb599",
+                "shasum": ""
+            },
+            "require": {
+                "composer-plugin-api": "^1.0|^2.0",
+                "php": "^7.1 || ^8.0"
+            },
+            "conflict": {
+                "nyholm/psr7": "<1.0",
+                "zendframework/zend-diactoros": "*"
+            },
+            "provide": {
+                "php-http/async-client-implementation": "*",
+                "php-http/client-implementation": "*",
+                "psr/http-client-implementation": "*",
+                "psr/http-factory-implementation": "*",
+                "psr/http-message-implementation": "*"
+            },
+            "require-dev": {
+                "composer/composer": "^1.0.2|^2.0",
+                "graham-campbell/phpspec-skip-example-extension": "^5.0",
+                "php-http/httplug": "^1.0 || ^2.0",
+                "php-http/message-factory": "^1.0",
+                "phpspec/phpspec": "^5.1 || ^6.1 || ^7.3",
+                "sebastian/comparator": "^3.0.5 || ^4.0.8",
+                "symfony/phpunit-bridge": "^6.4.4 || ^7.0.1"
+            },
+            "type": "composer-plugin",
+            "extra": {
+                "class": "Http\\Discovery\\Composer\\Plugin",
+                "plugin-optional": true
+            },
+            "autoload": {
+                "psr-4": {
+                    "Http\\Discovery\\": "src/"
+                },
+                "exclude-from-classmap": [
+                    "src/Composer/Plugin.php"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Márk Sági-Kazár",
+                    "email": "mark.sagikazar@gmail.com"
+                }
+            ],
+            "description": "Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations",
+            "homepage": "http://php-http.org",
+            "keywords": [
+                "adapter",
+                "client",
+                "discovery",
+                "factory",
+                "http",
+                "message",
+                "psr17",
+                "psr7"
+            ],
+            "support": {
+                "issues": "https://github.com/php-http/discovery/issues",
+                "source": "https://github.com/php-http/discovery/tree/1.19.4"
+            },
+            "time": "2024-03-29T13:00:05+00:00"
+        },
+        {
+            "name": "php-http/httplug",
+            "version": "2.4.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/php-http/httplug.git",
+                "reference": "625ad742c360c8ac580fcc647a1541d29e257f67"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/php-http/httplug/zipball/625ad742c360c8ac580fcc647a1541d29e257f67",
+                "reference": "625ad742c360c8ac580fcc647a1541d29e257f67",
+                "shasum": ""
+            },
+            "require": {
+                "php": "^7.1 || ^8.0",
+                "php-http/promise": "^1.1",
+                "psr/http-client": "^1.0",
+                "psr/http-message": "^1.0 || ^2.0"
+            },
+            "require-dev": {
+                "friends-of-phpspec/phpspec-code-coverage": "^4.1 || ^5.0 || ^6.0",
+                "phpspec/phpspec": "^5.1 || ^6.0 || ^7.0"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "Http\\Client\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Eric GELOEN",
+                    "email": "geloen.eric@gmail.com"
+                },
+                {
+                    "name": "Márk Sági-Kazár",
+                    "email": "mark.sagikazar@gmail.com",
+                    "homepage": "https://sagikazarmark.hu"
+                }
+            ],
+            "description": "HTTPlug, the HTTP client abstraction for PHP",
+            "homepage": "http://httplug.io",
+            "keywords": [
+                "client",
+                "http"
+            ],
+            "support": {
+                "issues": "https://github.com/php-http/httplug/issues",
+                "source": "https://github.com/php-http/httplug/tree/2.4.0"
+            },
+            "time": "2023-04-14T15:10:03+00:00"
+        },
+        {
+            "name": "php-http/promise",
+            "version": "1.3.1",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/php-http/promise.git",
+                "reference": "fc85b1fba37c169a69a07ef0d5a8075770cc1f83"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/php-http/promise/zipball/fc85b1fba37c169a69a07ef0d5a8075770cc1f83",
+                "reference": "fc85b1fba37c169a69a07ef0d5a8075770cc1f83",
+                "shasum": ""
+            },
+            "require": {
+                "php": "^7.1 || ^8.0"
+            },
+            "require-dev": {
+                "friends-of-phpspec/phpspec-code-coverage": "^4.3.2 || ^6.3",
+                "phpspec/phpspec": "^5.1.2 || ^6.2 || ^7.4"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "Http\\Promise\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Joel Wurtz",
+                    "email": "joel.wurtz@gmail.com"
+                },
+                {
+                    "name": "Márk Sági-Kazár",
+                    "email": "mark.sagikazar@gmail.com"
+                }
+            ],
+            "description": "Promise used for asynchronous HTTP requests",
+            "homepage": "http://httplug.io",
+            "keywords": [
+                "promise"
+            ],
+            "support": {
+                "issues": "https://github.com/php-http/promise/issues",
+                "source": "https://github.com/php-http/promise/tree/1.3.1"
+            },
+            "time": "2024-03-15T13:55:21+00:00"
+        },
         {
             "name": "phpoffice/phpspreadsheet",
             "version": "1.22.0",
diff --git a/config/console.php b/config/console.php
index d64e0746..dbcf36f3 100644
--- a/config/console.php
+++ b/config/console.php
@@ -12,5 +12,6 @@ return [
         'CalDealerTime' => 'app\command\CalDealerTime',
         'UpdateCollectGoodsPrice' => 'app\command\UpdateCollectGoodsPrice',
         'SyncStoreBasicData' => 'app\command\SyncStoreBasicData',
+        'syncGoodsToEs' => 'app\command\syncGoodsToEs',
     ],
 ];
diff --git a/vendor/services.php b/vendor/services.php
index 846f604f..face640b 100755
--- a/vendor/services.php
+++ b/vendor/services.php
@@ -1,6 +1,6 @@
-<?php 
-// This file is automatically generated at:2023-02-22 11:45:22
-declare (strict_types = 1);
+<?php 
+// This file is automatically generated at:2024-05-27 09:06:36
+declare (strict_types = 1);
 return array (
   0 => 'think\\app\\Service',
   1 => 'think\\queue\\Service',

From 937cdd5ad4dbee736d68a7a61cbca4dfab836a08 Mon Sep 17 00:00:00 2001
From: ztt <835303992@qq.com>
Date: Mon, 27 May 2024 15:58:52 +0800
Subject: [PATCH 2/2] 1

---
 app/command/SyncGoodsToEs.php               |   7 +-
 app/common/library/elasticsearch/Client.php | 602 +++++++++++++++++++-
 app/common/service/GoodsEs.php              |  39 +-
 config/console.php                          |   2 +-
 4 files changed, 620 insertions(+), 30 deletions(-)

diff --git a/app/command/SyncGoodsToEs.php b/app/command/SyncGoodsToEs.php
index 6255698c..136cfedf 100644
--- a/app/command/SyncGoodsToEs.php
+++ b/app/command/SyncGoodsToEs.php
@@ -2,7 +2,10 @@
 
 namespace app\command;
 
+use app\common\service\GoodsEs;
 use think\console\Command;
+use think\console\Output;
+use think\console\Input;
 
 class SyncGoodsToEs extends Command
 {
@@ -17,7 +20,9 @@ class SyncGoodsToEs extends Command
 
     protected function execute(Input $input, Output $output)
     {
-        $output->writeln('syncGoodsToEs');
+        $goodsService = new GoodsEs();
+        $goods = $goodsService->list([]);
+        var_dump($goods);
     }
 
 }
\ No newline at end of file
diff --git a/app/common/library/elasticsearch/Client.php b/app/common/library/elasticsearch/Client.php
index 0d84acf5..5c042c22 100644
--- a/app/common/library/elasticsearch/Client.php
+++ b/app/common/library/elasticsearch/Client.php
@@ -11,13 +11,34 @@ class Client
     const ES_HOST_NAME = '114.55.95.135:9200';
 
 
-    private static Client $instance;
+    private static  $instance;
 
-    private function __construct()
+    /**
+     * @var 索引名称
+     */
+    protected $index;
+
+
+    public $alias;
+
+    protected $retries = 2;
+
+    /**
+     * 查询参数
+     * @var array $queryParams
+     */
+    protected $queryParams = [];
+
+    private $debug;
+
+    public function __construct()
     {
+
+        var_dump(self::ES_HOST_NAME);
         $this->client = ClientBuilder::create()->setHosts([self::ES_HOST_NAME])->build();
     }
 
+
     private function __clone() {
 
     }
@@ -29,6 +50,81 @@ class Client
         return self::$instance;
     }
 
+    /**
+     * 查询条件
+     * @param $params
+     * @return $this
+     */
+    public function select($params = '')
+    {
+        $this->queryParams['select'] = str_replace(' ', '', trim($params, " ,\t\r\n"));
+        return $this;
+    }
+
+    /**
+     * 分页
+     * @param $number
+     * @return $this
+     */
+    public function from($number = 0)
+    {
+        $this->queryParams['from'] = $number ? ($number-1)*10 : 0;
+        return $this;
+    }
+
+    /**
+     * 页码
+     * @param $number
+     * @return $this
+     */
+    public function size($number = 10)
+    {
+        $this->queryParams['size'] = $number;
+        return $this;
+    }
+
+    public function equal($params = [])
+    {
+        $this->queryParams['equal'][] = $params;
+        return $this;
+    }
+
+    public function in($params = [])
+    {
+        $this->queryParams['in'][] = $params;
+        return $this;
+    }
+
+    public function like($params = [])
+    {
+        $this->queryParams['like'][] = $params;
+        return $this;
+    }
+
+    public function orlike($params = [])
+    {
+        $this->queryParams['orlike'][] = $params;
+        return $this;
+    }
+
+    public function order($params = [])
+    {
+        $this->queryParams['order'][] = $params;
+        return $this;
+    }
+
+    /**
+     * 根据条件删除文档
+     * @param array $params
+     * @return array
+     */
+
+    public function deleteDoc($params = [])
+    {
+        return $this->client->deleteByQuery($params);
+    }
+
+
     public function existsIndex(String $index_name) {
         $params = [
             'index' => $index_name
@@ -67,6 +163,182 @@ class Client
         return $this->client->indices()->delete($params);
     }
 
+    /**
+     * 根据id删除文档
+     * @param int $id
+     * @return array
+     */
+    public function deleteDocById($id = 0)
+    {
+        $params = [
+            'index' => $this->index,
+            'id' => $id
+        ];
+        $response = $this->client->delete($params);
+        return $response;
+    }
+
+    /**
+     * 组装查询条件
+     * @param array $params
+     * @return $this
+     */
+    private function parseQueryParams()
+    {
+        $queryParams = [
+            'index' => $this->index,
+            'body' => [
+                'query' => [
+                    'bool' => [
+                        'must' => [],
+                        'filter' => []
+                    ]
+                ]
+            ],
+            'from' => $this->queryParams['from'] ?? 0,
+            'size' => $this->queryParams['size'] ?? 10
+        ];
+
+        $filter = $must = [];
+        if (!empty($this->queryParams['select'])) {
+            $queryParams['_source'] = explode(',', $this->queryParams['select']);
+        }
+
+        if (!empty($this->queryParams['equal'])) {
+            foreach ($this->queryParams['equal'] as $key => $row) {
+                foreach ($row as $filed => $value) {
+                    $filter[] = [
+                        'term' => [$filed => $value]
+                    ];
+                }
+            }
+        }
+
+        if (!empty($this->queryParams['in'])) {
+            foreach ($this->queryParams['in'] as $key => $row) {
+                foreach ($row as $filed => $value) {
+                    $filter[] = [
+                        'terms' => [$filed => array_values(array_unique(array_filter($value)))]
+                    ];
+                }
+            }
+        }
+
+        if (!empty($this->queryParams['like'])) {
+            foreach ($this->queryParams['like'] as $key => $row) {
+                foreach ($row as $filed => $value) {
+                    /*$must[] = [
+
+                        'wildcard' => [$filed => '*'. $value. '*']
+
+                    ];*/
+                    $must[] = [
+//                        'match' => [$filed => $value]
+                        'match_phrase' => [$filed => $value]
+                    ];
+                }
+            }
+
+            $queryParams['body']['query']['bool']['must'] = $must;
+
+        }
+
+        if (!empty($this->queryParams['orlike'])) {
+            foreach ($this->queryParams['orlike'] as $key => $row) {
+                foreach ($row as $filed => $value) {
+                    $orlike[] = [
+                        'match_phrase' => [$filed => $value]
+                    ];
+                }
+            }
+
+            $queryParams['body']['query']['bool']['must']['bool']['should'] = $orlike;
+        }
+
+        if (!empty($this->queryParams['order'])) {
+            $queryParams['body']['sort'] = [
+                key($this->queryParams['order']) => [
+                    'order' => current($this->queryParams['order'])
+                ]
+            ];
+        }
+
+        $queryParams['body']['query']['bool']['filter'] = $filter;
+
+        $this->queryParams = $queryParams;
+
+        return $this;
+
+    }
+
+
+    /**
+     * @param bool $isTotal isTotal=true时, 返回总数
+     * @return array|string
+     */
+
+    public function query($isTotal = false)
+    {
+        try {
+            $this->parseQueryParams();
+
+            if ($this->debug) {
+                return \GuzzleHttp\json_encode($this->queryParams);
+            }
+
+            if ($isTotal === true) {
+
+                unset(
+
+                    $this->queryParams['from'],
+
+                    $this->queryParams['size'],
+
+                    $this->queryParams['_source']
+
+                );
+
+                $count = $this->client->count($this->queryParams);
+
+                return (int)$count['count'];
+
+            }
+
+            if (!empty($this->queryParams)) {
+
+                $result = $this->client->search($this->queryParams);
+
+                if (!empty($result['hits']['hits'])) {
+
+                    $return = [];
+
+                    foreach ($result['hits']['hits'] as $row) {
+
+                        $return[] = $row['_source'];
+
+                    }
+
+                    return $return;
+
+                } else {
+
+                    return [];
+
+                }
+
+            }
+
+        } catch (\Exception $e) {
+
+            $msg = $e->getMessage();
+
+            $msg = '服务器开小差了~';
+
+            throw new Exception($msg);
+
+        }
+    }
+
     /**
      * 添加文档
      * @param $id
@@ -86,6 +358,36 @@ class Client
         return $this->client->index($params);
     }
 
+    public function getIndex()
+    {
+        return $this->index;
+    }
+
+    /**
+     * 返回ES实例
+     */
+
+    public static function setEs($index = '')
+    {
+        $class = get_called_class();
+        if (!self::$instance || !self::$instance instanceof $class) {
+            self::$instance = new static();
+        }
+        if ($index) {
+            self::$instance->index = $index;
+        }
+        return self::$instance;
+
+    }
+
+    protected function settings()
+    {
+        return [
+            'number_of_shards' => 1,
+            'number_of_replicas' => 0
+        ];
+    }
+
     /**
      * 判断文档存在
      * @param int $id
@@ -144,40 +446,306 @@ class Client
         return $this->client->update($params);
     }
 
+
     /**
-     * 删除文档
-     * @param int $id
+     * 搜索文档:分页,排序,权重,过滤
      * @param string $index_name
      * @param string $type_name
+     * @param array $body
      * @return array
      */
-    public function deleteDoc(int $id = 1, string $index_name = 'gyx_ik', string $type_name = '_doc'): array
+    public function searchDoc(array $body = [], string $index_name = "goods_list", string $type_name = "_doc"): array
     {
         $params = [
             'index' => $index_name,
             'type' => $type_name,
-            'id' => $id
+            'body' => $body
         ];
 
-        return $this->client->delete($params);
+        return $this->client->search($params);
+    }
+
+    /**
+
+     * @return bool
+
+     * @throws Exception
+
+     */
+
+    public function createIndexNew($mapping)
+    {
+        try {
+            $params = [
+                'index' => $this->index
+            ];
+
+            $check = $this->indices()->exists($params);
+
+            if ($check) {
+                throw new Exception('index: ' . $this->index . ' already exists');
+            }
+
+            $params = [
+                'index' => $this->index,
+                'body' => [
+                    'settings' => $this->settings(),
+                    'mappings' => [
+                        '_source' => [
+                            'enabled' => true,
+                        ],
+                        'properties' => $mapping
+                    ]
+                ]
+            ];
+
+            $result = $this->indices()->create($params);
+
+            return $result['acknowledged'] === true;
+
+        } catch (\Exception $e) {
+
+            throw new Exception($e->getMessage());
+
+        }
+
     }
 
+    /**
+
+     * 删除索引
+
+     *
+
+     * @return bool
+
+     * @throws Missing404Exception
+
+     */
+
+    public function deleteIndexNew()
+    {
+
+        try {
+
+            $params = [
+
+                'index' => $this->index
+
+            ];
+
+            $check = $this->indices()->exists($params);
+
+            if (!$check) {
+
+                return true;
+
+            }
+
+            $result = $this->indices()->delete([
+
+                'index' => $this->index
+
+            ]);
+
+            return $result['acknowledged'] === true;
+
+        } catch (Missing404Exception $e) {
+
+            throw new Missing404Exception('no such index ' . $this->index);
+
+        }
+
+    }
 
     /**
-     * 搜索文档:分页,排序,权重,过滤
-     * @param string $index_name
-     * @param string $type_name
-     * @param array $body
-     * @return array
+
+     * 批量写入
+
+     * type 1.增加/修改 2.删除
+
+     * $data = [
+
+     *      ['id' => 1, 'name' => 'llf', 'age' => 30],
+
+     *      ['id' => 1, 'name' => 'llf', 'age' => 30],
+
+     *      ['id' => 1, 'name' => 'llf', 'age' => 30],
+
+     *      ['id' => 1, 'name' => 'llf', 'age' => 30],
+
+     * ];
+
+     * @param array $data
+
+     */
+
+    public function doBulkDocument($type = 1,$data = [])
+
+    {
+
+        try {
+
+            $params = ['body' => []];
+
+            if($type == 1){
+
+                foreach ($data as $key => $row) {
+
+                    $params['body'][] = [
+
+                        'index' => [
+
+                            '_index' => $this->index,
+
+                            '_id' => $row['info_id'].'-'.$row['info_type']
+
+                        ]
+
+                    ];
+
+                    $params['body'][] = $row;
+
+                    if (($key+1)%10000 == 0) {
+
+                        $this->client->bulk($params);
+
+                        $params = ['body' => []];
+
+                    }
+
+                }
+
+            }else{
+
+                foreach ($data as $key => $row) {
+
+                    $params['body'][] = [
+
+                        'delete' => [
+
+                            '_index' => $this->index,
+
+                            '_id' => $row['info_id'].'-'.$row['info_type']
+
+                        ]
+
+                    ];
+
+                }
+
+            }
+
+            if (!empty($params['body'])) {
+
+                $this->client->bulk($params);
+
+                return true;
+
+            }
+
+        } catch (\Exception $e) {
+
+            throw new Exception($e->getMessage());
+
+        }
+
+    }
+
+    /**
+
+     * @return array @todo
+
      */
-    public function searchDoc(array $body = [], string $index_name = "gyx_ik", string $type_name = "_doc"): array
+
+    public function updateIndex()
+
     {
+
+        $putParams = [
+
+            'index' => $this->index,
+
+            //'type' => '_doc',
+
+            'body' => [
+
+                'properties' => $this->mappings()
+
+            ]
+
+        ];
+
+        return $this->indices()->putMapping($putParams);
+
+    }
+
+    /**
+
+     * 方便调试, 直接返回拼接的query
+
+     * @return $this
+
+     */
+
+    public function setDebug()
+
+    {
+
+        $this->debug = true;
+
+        return $this;
+
+    }
+
+    private function indices()
+
+    {
+
+        return $this->client->indices();
+
+    }
+
+    public function analyze($text = '')
+
+    {
+
         $params = [
-            'index' => $index_name,
-            'type' => $type_name,
-            'body' => $body
+
+            'index' => $this->index,
+
+            'body' => [
+
+                'analyzer' => 'ik_smart',
+
+                'text' => $text
+
+            ]
+
         ];
 
-        return $this->client->search($params);
+        return $this->indices()->analyze($params);
+
+    }
+
+    public function update($id = 0, $updateParams = [])
+
+    {
+
+        $params = [
+
+            'index' => $this->index,
+
+            'id' => $id,
+
+            'body' => [
+
+                'doc' => $updateParams
+
+            ]
+
+        ];
+
+        return $this->client->update($params);
     }
 }
diff --git a/app/common/service/GoodsEs.php b/app/common/service/GoodsEs.php
index 8e6f6233..f5106e6f 100644
--- a/app/common/service/GoodsEs.php
+++ b/app/common/service/GoodsEs.php
@@ -8,9 +8,11 @@ class GoodsEs
 {
     private Client $esService;
 
+    private $index_name = 'goods_list';
+
     public function __construct()
     {
-        $this->esService = Client::getInstance();
+        $this->esService = Client::setEs('goods_List');
     }
 
     /**
@@ -34,7 +36,13 @@ class GoodsEs
                 ]
             ]
         ];
-        return $this->esService->searchDoc($body);
+
+        return $this->esService
+            ->equal(['title' => 1])
+            ->from(0)
+            ->size(10)
+            ->order(['id' => 'desc'])
+            ->query($body);
     }
 
 
@@ -43,7 +51,7 @@ class GoodsEs
     /**
      * 更新商品
      */
-    public function updateDoc($goods_id)
+    public function update($goods_id)
     {
         $goods_id = 1;
         $doc = [
@@ -61,12 +69,22 @@ class GoodsEs
         var_dump($res);
     }
 
+    /**
+     * 查询商品详情
+     */
+    public function detail($goods_id)
+    {
+        $goods_id = 1;
+        $res = $this->esService->getDoc($goods_id);
+        var_dump($res);
+    }
+
 
     /**
      * 创建商品
      * @return
      */
-    public function createDoc($goods_id)
+    public function create($goods_id)
     {
         $goods_id = 1;
         $doc = [
@@ -87,7 +105,7 @@ class GoodsEs
     /**
      * 删除商品
      */
-    public function deleteDoc($goods_id)
+    public function delete($goods_id)
     {
         $res = $this->esService->deleteDoc($goods_id);
         var_dump($res);
@@ -104,12 +122,11 @@ class GoodsEs
     {
         $mapping = [
             'properties' => [
-                'id' => [
-                    'type' => 'keyword'
+                'goods_id' => [
+                    'type' => 'integer'
                 ],
-                'video_title' => [
-                    'type' => 'text',
-                    "analyzer" => "ik_smart"
+                'goods_type' => [
+                    'type' => 'keyword'
                 ],
                 'director' => [
                     'type' => 'text',
@@ -137,7 +154,7 @@ class GoodsEs
             ]
         ];
 
-        $res = $this->esService->createIndex('video_list', $mapping);
+        $res = $this->esService->createIndex($this->index_name, $mapping);
         var_dump($res);
     }
 
diff --git a/config/console.php b/config/console.php
index dbcf36f3..09465dc3 100644
--- a/config/console.php
+++ b/config/console.php
@@ -12,6 +12,6 @@ return [
         'CalDealerTime' => 'app\command\CalDealerTime',
         'UpdateCollectGoodsPrice' => 'app\command\UpdateCollectGoodsPrice',
         'SyncStoreBasicData' => 'app\command\SyncStoreBasicData',
-        'syncGoodsToEs' => 'app\command\syncGoodsToEs',
+        'syncGoodsToEs' => 'app\command\SyncGoodsToEs',
     ],
 ];