feat(milvus_kb): 优化文件删除逻辑,增加文件存在性检查
This commit is contained in:
parent
b403d8dd90
commit
04f6cb5a79
@ -311,8 +311,8 @@ class MilvusKB(KnowledgeBase):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# 设置查询参数 - Milvus 知识库特有的参数
|
# 设置查询参数 - Milvus 知识库特有的参数
|
||||||
top_k = kwargs.get("top_k", 10)
|
top_k = kwargs.get("top_k", 30)
|
||||||
similarity_threshold = kwargs.get("similarity_threshold", 0.0) # 相似度阈值
|
similarity_threshold = kwargs.get("similarity_threshold", 0.2) # 相似度阈值
|
||||||
include_distances = kwargs.get("include_distances", True) # 是否包含距离信息
|
include_distances = kwargs.get("include_distances", True) # 是否包含距离信息
|
||||||
metric_type = kwargs.get("metric_type", "COSINE") # 距离度量类型
|
metric_type = kwargs.get("metric_type", "COSINE") # 距离度量类型
|
||||||
|
|
||||||
@ -367,19 +367,31 @@ class MilvusKB(KnowledgeBase):
|
|||||||
"""删除文件"""
|
"""删除文件"""
|
||||||
collection = await self._get_milvus_collection(db_id)
|
collection = await self._get_milvus_collection(db_id)
|
||||||
|
|
||||||
def _delete_from_milvus():
|
if collection:
|
||||||
"""同步执行 Milvus 删除操作的辅助函数"""
|
# 先查询文件是否存在,避免不必要的删除操作
|
||||||
try:
|
try:
|
||||||
expr = f'file_id == "{file_id}"'
|
expr = f'file_id == "{file_id}"'
|
||||||
collection.delete(expr)
|
results = collection.query(
|
||||||
collection.flush()
|
expr=expr,
|
||||||
logger.info(f"Deleted chunks for file {file_id} from Milvus")
|
output_fields=["id"],
|
||||||
|
limit=1
|
||||||
|
)
|
||||||
|
|
||||||
|
if not results:
|
||||||
|
logger.info(f"File {file_id} not found in Milvus, skipping delete operation")
|
||||||
|
else:
|
||||||
|
# 只有在文件确实存在时才执行删除
|
||||||
|
def _delete_from_milvus():
|
||||||
|
try:
|
||||||
|
collection.delete(expr)
|
||||||
|
collection.flush()
|
||||||
|
logger.info(f"Deleted chunks for file {file_id} from Milvus")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error deleting file {file_id} from Milvus: {e}")
|
||||||
|
|
||||||
|
await asyncio.to_thread(_delete_from_milvus)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error deleting file {file_id} from Milvus: {e}")
|
logger.error(f"Error checking file existence in Milvus: {e}")
|
||||||
|
|
||||||
if collection:
|
|
||||||
await asyncio.to_thread(_delete_from_milvus)
|
|
||||||
|
|
||||||
# 使用锁确保元数据操作的原子性
|
# 使用锁确保元数据操作的原子性
|
||||||
async with self._metadata_lock:
|
async with self._metadata_lock:
|
||||||
if file_id in self.files_meta:
|
if file_id in self.files_meta:
|
||||||
|
|||||||
@ -271,7 +271,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="info-item">
|
<div class="info-item">
|
||||||
<label>上传时间:</label>
|
<label>上传时间:</label>
|
||||||
<span>{{ formatRelativeTime(Math.round(selectedFile.created_at*1000)) }}</span>
|
<span>{{ formatStandardTime(Math.round(selectedFile.created_at*1000)) }}</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="info-item">
|
<div class="info-item">
|
||||||
<label>处理状态:</label>
|
<label>处理状态:</label>
|
||||||
@ -1033,9 +1033,10 @@ const openFileDetail = (record) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Format relative time with more granularity: days ago, weeks ago, months ago
|
||||||
const formatRelativeTime = (timestamp, offset = 0) => {
|
const formatRelativeTime = (timestamp, offset = 0) => {
|
||||||
// 如果调整为东八区时间(UTC+8),则offset为8,否则为0
|
// If you want to adjust to UTC+8, set offset to 8, otherwise 0
|
||||||
const timezoneOffset = offset * 60 * 60 * 1000; // 东八区偏移量(毫秒)
|
const timezoneOffset = offset * 60 * 60 * 1000; // offset in milliseconds
|
||||||
const adjustedTimestamp = timestamp + timezoneOffset;
|
const adjustedTimestamp = timestamp + timezoneOffset;
|
||||||
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
@ -1047,7 +1048,17 @@ const formatRelativeTime = (timestamp, offset = 0) => {
|
|||||||
return Math.round(secondsPast / 60) + ' 分钟前';
|
return Math.round(secondsPast / 60) + ' 分钟前';
|
||||||
} else if (secondsPast < 86400) {
|
} else if (secondsPast < 86400) {
|
||||||
return Math.round(secondsPast / 3600) + ' 小时前';
|
return Math.round(secondsPast / 3600) + ' 小时前';
|
||||||
|
} else if (secondsPast < 86400 * 7) {
|
||||||
|
// Less than 7 days
|
||||||
|
return Math.round(secondsPast / 86400) + ' 天前';
|
||||||
|
} else if (secondsPast < 86400 * 30) {
|
||||||
|
// Less than 30 days, show in weeks
|
||||||
|
return Math.round(secondsPast / (86400 * 7)) + ' 周前';
|
||||||
|
} else if (secondsPast < 86400 * 365) {
|
||||||
|
// Less than 1 year, show in months
|
||||||
|
return Math.round(secondsPast / (86400 * 30)) + ' 月前';
|
||||||
} else {
|
} else {
|
||||||
|
// More than 1 year, show full date
|
||||||
const date = new Date(adjustedTimestamp);
|
const date = new Date(adjustedTimestamp);
|
||||||
const year = date.getFullYear();
|
const year = date.getFullYear();
|
||||||
const month = date.getMonth() + 1;
|
const month = date.getMonth() + 1;
|
||||||
@ -1056,6 +1067,17 @@ const formatRelativeTime = (timestamp, offset = 0) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const formatStandardTime = (timestamp) => {
|
||||||
|
const date = new Date(timestamp);
|
||||||
|
const year = date.getFullYear();
|
||||||
|
const month = date.getMonth() + 1;
|
||||||
|
const day = date.getDate();
|
||||||
|
const hour = date.getHours();
|
||||||
|
const minute = date.getMinutes();
|
||||||
|
const second = date.getSeconds();
|
||||||
|
return `${year} 年 ${month} 月 ${day} 日 ${hour}:${minute}:${second}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const getDatabaseInfo = () => {
|
const getDatabaseInfo = () => {
|
||||||
const db_id = databaseId.value
|
const db_id = databaseId.value
|
||||||
@ -1654,6 +1676,15 @@ const columnsCompact = [
|
|||||||
sorter: (a, b) => (a.filename || '').localeCompare(b.filename || ''),
|
sorter: (a, b) => (a.filename || '').localeCompare(b.filename || ''),
|
||||||
sortDirections: ['ascend', 'descend']
|
sortDirections: ['ascend', 'descend']
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title: '时间',
|
||||||
|
dataIndex: 'created_at',
|
||||||
|
key: 'created_at',
|
||||||
|
width: 120,
|
||||||
|
align: 'right',
|
||||||
|
sorter: (a, b) => (a.created_at || 0) - (b.created_at || 0),
|
||||||
|
sortDirections: ['ascend', 'descend']
|
||||||
|
},
|
||||||
{
|
{
|
||||||
title: '状态',
|
title: '状态',
|
||||||
dataIndex: 'status',
|
dataIndex: 'status',
|
||||||
@ -1666,15 +1697,6 @@ const columnsCompact = [
|
|||||||
},
|
},
|
||||||
sortDirections: ['ascend', 'descend']
|
sortDirections: ['ascend', 'descend']
|
||||||
},
|
},
|
||||||
{
|
|
||||||
title: '时间',
|
|
||||||
dataIndex: 'created_at',
|
|
||||||
key: 'created_at',
|
|
||||||
width: 80,
|
|
||||||
align: 'right',
|
|
||||||
sorter: (a, b) => (a.created_at || 0) - (b.created_at || 0),
|
|
||||||
sortDirections: ['ascend', 'descend']
|
|
||||||
},
|
|
||||||
{ title: '', key: 'action', dataIndex: 'file_id', width: 40, align: 'center' }
|
{ title: '', key: 'action', dataIndex: 'file_id', width: 40, align: 'center' }
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user