From ed8e443f3a8b1e83178ec0670378a5aeed3eab4f Mon Sep 17 00:00:00 2001 From: kuaifan Date: Sat, 8 Mar 2025 11:04:43 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96ES=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ElasticSearch/ElasticSearchBase.php | 28 ++++++++++++++++++- .../ElasticSearch/ElasticSearchKeyValue.php | 19 ++++--------- .../ElasticSearch/ElasticSearchUserMsg.php | 11 ++------ 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/app/Module/ElasticSearch/ElasticSearchBase.php b/app/Module/ElasticSearch/ElasticSearchBase.php index d807be58b..61301b6fd 100644 --- a/app/Module/ElasticSearch/ElasticSearchBase.php +++ b/app/Module/ElasticSearch/ElasticSearchBase.php @@ -19,7 +19,7 @@ class ElasticSearchBase * * @var \Elastic\Elasticsearch\Client */ - public $client; + protected $client; /** * 当前操作的索引名称 @@ -279,4 +279,30 @@ class ElasticSearchBase return ['error' => $e->getMessage(), 'hits' => ['total' => ['value' => 0], 'hits' => []]]; } } + + /** + * 索引名称 + */ + const indexName = 'default'; + + /** + * 获取索引名称 + * @param string $index 索引名称 + * @param string|null $prefix 索引前缀 + * @param string|null $subfix 索引后缀 + * @return string + */ + public static function indexName($index = '', $prefix = '', $subfix = '') + { + $index = $index ?: static::indexName; + $prefix = $prefix ?: env('ES_INDEX_PREFIX', ''); + $subfix = $subfix ?: env('ES_INDEX_SUFFIX', ''); + if ($prefix) { + $index = rtrim($prefix, '_') . '_' . $index; + } + if ($subfix) { + $index = $index . '_' . ltrim($subfix, '_'); + } + return $index; + } } diff --git a/app/Module/ElasticSearch/ElasticSearchKeyValue.php b/app/Module/ElasticSearch/ElasticSearchKeyValue.php index b87e515a9..8dac827b8 100644 --- a/app/Module/ElasticSearch/ElasticSearchKeyValue.php +++ b/app/Module/ElasticSearch/ElasticSearchKeyValue.php @@ -13,6 +13,8 @@ use Illuminate\Support\Facades\Log; */ class ElasticSearchKeyValue extends ElasticSearchBase { + const indexName = 'key_value_store'; + /** * 构造函数 * @return ElasticSearchBase @@ -27,15 +29,6 @@ class ElasticSearchKeyValue extends ElasticSearchBase /** *********************************** 键值存储方法 ******************************************************** */ /** ******************************************************************************************************** */ - /** - * 键值存储索引名称 - * @return string - */ - public static function indexName() - { - return "key_value_store" . env("ES_INDEX_SUFFIX", ""); - } - /** * 创建键值存储索引 * @return array @@ -55,8 +48,8 @@ class ElasticSearchKeyValue extends ElasticSearchBase 'properties' => [ 'key' => ['type' => 'keyword'], 'value' => ['type' => 'text', 'fields' => ['keyword' => ['type' => 'keyword']]], - 'created_at' => ['type' => 'date'], - 'updated_at' => ['type' => 'date'] + 'created_at' => ['type' => 'date', 'format' => 'yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis'], + 'updated_at' => ['type' => 'date', 'format' => 'yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis'] ] ]; @@ -97,8 +90,8 @@ class ElasticSearchKeyValue extends ElasticSearchBase 'key' => $key, 'value' => is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value, 'namespace' => $namespace, - 'created_at' => date('Y-m-d H:i:s'), - 'updated_at' => date('Y-m-d H:i:s') + 'created_at' => date('Y-m-d\TH:i:s\Z', strtotime(date('Y-m-d H:i:s'))), + 'updated_at' => date('Y-m-d\TH:i:s\Z', strtotime(date('Y-m-d H:i:s'))) ]; // 索引文档 diff --git a/app/Module/ElasticSearch/ElasticSearchUserMsg.php b/app/Module/ElasticSearch/ElasticSearchUserMsg.php index 027612978..ed30d8949 100644 --- a/app/Module/ElasticSearch/ElasticSearchUserMsg.php +++ b/app/Module/ElasticSearch/ElasticSearchUserMsg.php @@ -14,6 +14,8 @@ use Illuminate\Support\Facades\Log; */ class ElasticSearchUserMsg extends ElasticSearchBase { + const indexName = 'dialog_user_msg'; + /** * 构造函数 * @return ElasticSearchBase @@ -28,15 +30,6 @@ class ElasticSearchUserMsg extends ElasticSearchBase /** *********************************************** 基础 ************************************************** */ /** ******************************************************************************************************** */ - /** - * 索引名称 - * @return string - */ - public static function indexName() - { - return "dialog_user_msg" . env("ES_INDEX_SUFFIX", ""); - } - /** * 创建聊天系统索引 - 使用父子关系 * @return array