mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-12 19:35:50 +00:00
perf: 优化全文搜索
This commit is contained in:
parent
dbf42c51a4
commit
a2533ce7f9
@ -23,19 +23,6 @@ class ZincSearchBase
|
|||||||
$this->pass = env('DB_PASSWORD', '');
|
$this->pass = env('DB_PASSWORD', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取配置
|
|
||||||
*/
|
|
||||||
private function config(): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'host' => $this->host,
|
|
||||||
'port' => $this->port,
|
|
||||||
'user' => $this->user,
|
|
||||||
'pass' => $this->pass
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通用请求方法
|
* 通用请求方法
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -44,22 +44,10 @@ class ZincSearchKeyValue
|
|||||||
if (!ZincSearchBase::indexExists(self::$indexName)) {
|
if (!ZincSearchBase::indexExists(self::$indexName)) {
|
||||||
$mappings = [
|
$mappings = [
|
||||||
'properties' => [
|
'properties' => [
|
||||||
'key' => [
|
'key' => ['type' => 'keyword', 'index' => true],
|
||||||
'type' => 'keyword',
|
'value' => ['type' => 'text', 'index' => true],
|
||||||
'index' => true
|
'created_at' => ['type' => 'date', 'index' => true],
|
||||||
],
|
'updated_at' => ['type' => 'date', '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);
|
$result = ZincSearchBase::createIndex(self::$indexName, $mappings);
|
||||||
@ -94,10 +82,20 @@ class ZincSearchKeyValue
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否存在相同键的文档
|
// 检查是否存在相同键的文档 - 使用精确查询而不是普通搜索
|
||||||
$searchResult = ZincSearchBase::search(self::$indexName, $key, 0, 1);
|
$searchParams = [
|
||||||
$docs = $searchResult['data']['hits']['hits'] ?? [];
|
'search_type' => 'term',
|
||||||
$now = date('Y-m-d H:i:s');
|
'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)) {
|
if (!empty($docs)) {
|
||||||
$docId = $docs[0]['_id'] ?? null;
|
$docId = $docs[0]['_id'] ?? null;
|
||||||
@ -231,7 +229,7 @@ class ZincSearchKeyValue
|
|||||||
}
|
}
|
||||||
|
|
||||||
$docs = [];
|
$docs = [];
|
||||||
$now = date('Y-m-d H:i:s');
|
$now = date('c');
|
||||||
|
|
||||||
foreach ($keyValues as $key => $value) {
|
foreach ($keyValues as $key => $value) {
|
||||||
$docs[] = [
|
$docs[] = [
|
||||||
|
|||||||
@ -219,7 +219,7 @@ class ZincSearchUserMsg
|
|||||||
/**
|
/**
|
||||||
* 批量同步会话用户
|
* 批量同步会话用户
|
||||||
*
|
*
|
||||||
* @param array|iterable $dialogUsers WebSocketDialogUser对象集合
|
* @param WebSocketDialogUser[] $dialogUsers WebSocketDialogUser对象集合
|
||||||
* @return int 成功同步的用户数
|
* @return int 成功同步的用户数
|
||||||
*/
|
*/
|
||||||
public static function batchSyncUsers($dialogUsers): int
|
public static function batchSyncUsers($dialogUsers): int
|
||||||
@ -378,7 +378,7 @@ class ZincSearchUserMsg
|
|||||||
/**
|
/**
|
||||||
* 批量同步会话消息
|
* 批量同步会话消息
|
||||||
*
|
*
|
||||||
* @param array|iterable $dialogMsgs WebSocketDialogMsg对象集合
|
* @param WebSocketDialogMsg[] $dialogMsgs WebSocketDialogMsg对象集合
|
||||||
* @return int 成功同步的消息数
|
* @return int 成功同步的消息数
|
||||||
*/
|
*/
|
||||||
public static function batchSyncMsgs($dialogMsgs): int
|
public static function batchSyncMsgs($dialogMsgs): int
|
||||||
@ -390,8 +390,8 @@ class ZincSearchUserMsg
|
|||||||
|
|
||||||
// 预处理:收集所有涉及的对话ID
|
// 预处理:收集所有涉及的对话ID
|
||||||
$dialogIds = [];
|
$dialogIds = [];
|
||||||
foreach ($dialogMsgs as $message) {
|
foreach ($dialogMsgs as $dialogMsg) {
|
||||||
$dialogIds[] = $message->dialog_id;
|
$dialogIds[] = $dialogMsg->dialog_id;
|
||||||
}
|
}
|
||||||
$dialogIds = array_unique($dialogIds);
|
$dialogIds = array_unique($dialogIds);
|
||||||
|
|
||||||
@ -406,10 +406,10 @@ class ZincSearchUserMsg
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 为每条消息准备所有相关用户的文档
|
// 为每条消息准备所有相关用户的文档
|
||||||
foreach ($dialogMsgs as $message) {
|
foreach ($dialogMsgs as $dialogMsg) {
|
||||||
if (isset($userDialogs[$message->dialog_id])) {
|
if (isset($userDialogs[$dialogMsg->dialog_id])) {
|
||||||
foreach ($userDialogs[$message->dialog_id] as $dialogUser) {
|
foreach ($userDialogs[$dialogMsg->dialog_id] as $dialogUser) {
|
||||||
$docFormat = self::generateMsgFormat($message, $dialogUser->userid);
|
$docFormat = self::generateMsgFormat($dialogMsg, $dialogUser->userid);
|
||||||
$docs[] = $docFormat;
|
$docs[] = $docFormat;
|
||||||
$count++;
|
$count++;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user