diff --git a/app/Module/ZincSearch/ZincSearchBase.php b/app/Module/ZincSearch/ZincSearchBase.php index 93811033d..3c9621ad0 100644 --- a/app/Module/ZincSearch/ZincSearchBase.php +++ b/app/Module/ZincSearch/ZincSearchBase.php @@ -23,19 +23,6 @@ class ZincSearchBase $this->pass = env('DB_PASSWORD', ''); } - /** - * 获取配置 - */ - private function config(): array - { - return [ - 'host' => $this->host, - 'port' => $this->port, - 'user' => $this->user, - 'pass' => $this->pass - ]; - } - /** * 通用请求方法 */ diff --git a/app/Module/ZincSearch/ZincSearchKeyValue.php b/app/Module/ZincSearch/ZincSearchKeyValue.php index 2a56b3d00..6b862ea16 100644 --- a/app/Module/ZincSearch/ZincSearchKeyValue.php +++ b/app/Module/ZincSearch/ZincSearchKeyValue.php @@ -44,22 +44,10 @@ class ZincSearchKeyValue if (!ZincSearchBase::indexExists(self::$indexName)) { $mappings = [ 'properties' => [ - 'key' => [ - 'type' => 'keyword', - 'index' => true - ], - 'value' => [ - 'type' => 'text', - 'index' => true - ], - 'created_at' => [ - 'type' => 'date', - 'index' => true - ], - 'updated_at' => [ - 'type' => 'date', - 'index' => true - ] + 'key' => ['type' => 'keyword', 'index' => true], + 'value' => ['type' => 'text', 'index' => true], + 'created_at' => ['type' => 'date', 'index' => true], + 'updated_at' => ['type' => 'date', 'index' => true] ] ]; $result = ZincSearchBase::createIndex(self::$indexName, $mappings); @@ -94,10 +82,20 @@ class ZincSearchKeyValue } } - // 检查是否存在相同键的文档 - $searchResult = ZincSearchBase::search(self::$indexName, $key, 0, 1); - $docs = $searchResult['data']['hits']['hits'] ?? []; - $now = date('Y-m-d H:i:s'); + // 检查是否存在相同键的文档 - 使用精确查询而不是普通搜索 + $searchParams = [ + 'search_type' => 'term', + 'query' => [ + 'field' => 'key', + 'term' => $key + ], + 'from' => 0, + 'max_results' => 1 + ]; + + $result = ZincSearchBase::advancedSearch(self::$indexName, $searchParams); + $docs = $result['data']['hits']['hits'] ?? []; + $now = date('c'); if (!empty($docs)) { $docId = $docs[0]['_id'] ?? null; @@ -231,7 +229,7 @@ class ZincSearchKeyValue } $docs = []; - $now = date('Y-m-d H:i:s'); + $now = date('c'); foreach ($keyValues as $key => $value) { $docs[] = [ diff --git a/app/Module/ZincSearch/ZincSearchUserMsg.php b/app/Module/ZincSearch/ZincSearchUserMsg.php index 67f959de2..0d7f2cfe4 100644 --- a/app/Module/ZincSearch/ZincSearchUserMsg.php +++ b/app/Module/ZincSearch/ZincSearchUserMsg.php @@ -219,7 +219,7 @@ class ZincSearchUserMsg /** * 批量同步会话用户 * - * @param array|iterable $dialogUsers WebSocketDialogUser对象集合 + * @param WebSocketDialogUser[] $dialogUsers WebSocketDialogUser对象集合 * @return int 成功同步的用户数 */ public static function batchSyncUsers($dialogUsers): int @@ -378,7 +378,7 @@ class ZincSearchUserMsg /** * 批量同步会话消息 * - * @param array|iterable $dialogMsgs WebSocketDialogMsg对象集合 + * @param WebSocketDialogMsg[] $dialogMsgs WebSocketDialogMsg对象集合 * @return int 成功同步的消息数 */ public static function batchSyncMsgs($dialogMsgs): int @@ -390,8 +390,8 @@ class ZincSearchUserMsg // 预处理:收集所有涉及的对话ID $dialogIds = []; - foreach ($dialogMsgs as $message) { - $dialogIds[] = $message->dialog_id; + foreach ($dialogMsgs as $dialogMsg) { + $dialogIds[] = $dialogMsg->dialog_id; } $dialogIds = array_unique($dialogIds); @@ -406,10 +406,10 @@ class ZincSearchUserMsg } // 为每条消息准备所有相关用户的文档 - foreach ($dialogMsgs as $message) { - if (isset($userDialogs[$message->dialog_id])) { - foreach ($userDialogs[$message->dialog_id] as $dialogUser) { - $docFormat = self::generateMsgFormat($message, $dialogUser->userid); + foreach ($dialogMsgs as $dialogMsg) { + if (isset($userDialogs[$dialogMsg->dialog_id])) { + foreach ($userDialogs[$dialogMsg->dialog_id] as $dialogUser) { + $docFormat = self::generateMsgFormat($dialogMsg, $dialogUser->userid); $docs[] = $docFormat; $count++; }