perf: 优化全文搜索

This commit is contained in:
kuaifan 2025-04-17 13:02:58 +08:00
parent dbf42c51a4
commit a2533ce7f9
3 changed files with 27 additions and 42 deletions

View File

@ -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
];
}
/**
* 通用请求方法
*/

View File

@ -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[] = [

View File

@ -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++;
}