update code level

This commit is contained in:
lychang
2026-03-25 14:06:49 +08:00
parent 448eef2319
commit e05cf48063
7 changed files with 254 additions and 192 deletions

View File

@@ -12,7 +12,7 @@ pub struct Album {
pub updated_at: String,
}
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Image {
pub id: Uuid,
pub album_id: Uuid,
@@ -147,11 +147,16 @@ impl AlbumsCache {
}
pub fn search(&self, query: &str) -> Vec<AlbumInfo> {
let query_lower = query.to_lowercase();
let query_lower = query.to_lowercase().trim().to_string();
if query_lower.is_empty() {
return Vec::new();
}
let mut results = Vec::new();
let mut seen_ids = std::collections::HashSet::new();
// 搜索完整匹配
// 1. 搜索完整匹配(最高优先级)
if let Some(matches) = self.search_index.get(&query_lower) {
for album in matches {
if seen_ids.insert(album.id) {
@@ -160,10 +165,45 @@ impl AlbumsCache {
}
}
// 搜索单词匹配
// 2. 搜索单词匹配
for word in query_lower.split_whitespace() {
if let Some(matches) = self.search_index.get(word) {
for album in matches {
if word.len() >= 2 { // 只搜索长度>=2的单词
if let Some(matches) = self.search_index.get(word) {
for album in matches {
if seen_ids.insert(album.id) {
results.push(album.clone());
}
}
}
}
}
// 3. 模糊搜索:在相册标题和描述中搜索包含查询词的结果
if results.is_empty() {
for album in &self.albums {
let title_lower = album.title.to_lowercase();
let description_lower = album.description.as_ref()
.map(|d| d.to_lowercase())
.unwrap_or_default();
// 检查标题或描述是否包含搜索词
if title_lower.contains(&query_lower) || description_lower.contains(&query_lower) {
if seen_ids.insert(album.id) {
results.push(album.clone());
}
}
}
}
// 4. 如果还没有结果,尝试部分匹配(更宽松的搜索)
if results.is_empty() && query_lower.len() >= 2 {
for album in &self.albums {
let title_lower = album.title.to_lowercase();
// 检查标题是否包含搜索词的任何部分
if query_lower.split_whitespace().any(|word|
word.len() >= 2 && title_lower.contains(word)
) {
if seen_ids.insert(album.id) {
results.push(album.clone());
}