mirror of
https://github.com/DrizzleTime/Foxel.git
synced 2026-09-09 01:27:24 +08:00
fix(PictureService, SearchDialog): enhance vector search with error handling and tab management #9
This commit is contained in:
@@ -49,20 +49,33 @@ public class PictureService(
|
||||
|
||||
// 决定是使用向量搜索还是普通搜索
|
||||
if (useVectorSearch && !string.IsNullOrWhiteSpace(searchQuery))
|
||||
{
|
||||
try
|
||||
{
|
||||
return await PerformVectorSearchAsync(
|
||||
dbContext, page, pageSize, searchQuery, tags,
|
||||
startDate, endDate, userId, onlyWithGps, similarityThreshold,
|
||||
excludeAlbumId, albumId, onlyFavorites, ownerId, includeAllPublic);
|
||||
}
|
||||
else
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 如果向量搜索失败,记录错误并回退到标准搜索
|
||||
Console.WriteLine($"向量搜索失败,回退到标准搜索: {ex.Message}");
|
||||
|
||||
// 如果是明确的配置错误,则向上抛出异常
|
||||
if (ex.Message.Contains("请检查嵌入模型配置"))
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 执行标准搜索(作为默认方法或向量搜索的回退选项)
|
||||
return await PerformStandardSearchAsync(
|
||||
dbContext, page, pageSize, searchQuery, tags,
|
||||
startDate, endDate, userId, sortBy, onlyWithGps,
|
||||
excludeAlbumId, albumId, onlyFavorites, ownerId, includeAllPublic);
|
||||
}
|
||||
}
|
||||
|
||||
// 执行向量搜索
|
||||
private async Task<PaginatedResult<PictureResponse>> PerformVectorSearchAsync(
|
||||
@@ -82,7 +95,24 @@ public class PictureService(
|
||||
int? ownerId,
|
||||
bool includeAllPublic)
|
||||
{
|
||||
var queryEmbedding = await embeddingService.GetEmbeddingAsync(searchQuery);
|
||||
try
|
||||
{
|
||||
float[]? queryEmbedding = null;
|
||||
try
|
||||
{
|
||||
queryEmbedding = await embeddingService.GetEmbeddingAsync(searchQuery);
|
||||
|
||||
// 检查嵌入向量是否有效
|
||||
if (queryEmbedding == null || queryEmbedding.Length == 0)
|
||||
{
|
||||
throw new InvalidOperationException("嵌入模型返回了空向量");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException($"向量搜索失败,请检查嵌入模型配置: {ex.Message}", ex);
|
||||
}
|
||||
|
||||
var queryVector = new Vector(queryEmbedding);
|
||||
|
||||
// 构建基础查询
|
||||
@@ -132,6 +162,12 @@ public class PictureService(
|
||||
TotalCount = totalCount
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"向量搜索失败: {ex.Message}");
|
||||
throw new InvalidOperationException($"向量搜索失败: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
// 执行标准搜索
|
||||
private async Task<PaginatedResult<PictureResponse>> PerformStandardSearchAsync(
|
||||
|
||||
@@ -126,6 +126,17 @@ const SearchDialog: React.FC<SearchDialogProps> = ({
|
||||
}
|
||||
};
|
||||
|
||||
// 处理标签页切换,同时更新 useVectorSearch 状态
|
||||
const handleTabChange = (key: string) => {
|
||||
setActiveTabKey(key);
|
||||
// 根据标签页自动设置向量搜索状态
|
||||
if (key === 'vector') {
|
||||
setUseVectorSearch(true);
|
||||
} else {
|
||||
setUseVectorSearch(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Modal
|
||||
title={
|
||||
@@ -143,7 +154,7 @@ const SearchDialog: React.FC<SearchDialogProps> = ({
|
||||
>
|
||||
<Tabs
|
||||
activeKey={activeTabKey}
|
||||
onChange={setActiveTabKey}
|
||||
onChange={handleTabChange}
|
||||
className="search-tabs"
|
||||
items={[
|
||||
{
|
||||
@@ -163,9 +174,6 @@ const SearchDialog: React.FC<SearchDialogProps> = ({
|
||||
autoFocus={activeTabKey === 'text'}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Divider orientation="left" plain>筛选选项</Divider>
|
||||
|
||||
<div className="search-option-group">
|
||||
<Text strong className="option-label">标签筛选:</Text>
|
||||
<Select
|
||||
@@ -212,9 +220,6 @@ const SearchDialog: React.FC<SearchDialogProps> = ({
|
||||
autoFocus={activeTabKey === 'vector'}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Divider orientation="left" plain>高级选项</Divider>
|
||||
|
||||
<div className="search-option-group vector-options">
|
||||
<div className="vector-switch-container">
|
||||
<Switch
|
||||
|
||||
Reference in New Issue
Block a user