diff --git a/backend/app/services/pubmed_query_parser.py b/backend/app/services/pubmed_query_parser.py index 629138b..ddbef95 100644 --- a/backend/app/services/pubmed_query_parser.py +++ b/backend/app/services/pubmed_query_parser.py @@ -621,6 +621,8 @@ class PubmedQueryParser: start_val, end_val = end_val, start_val elif _start_is_digit and not _end_is_digit and int(start_val) > int(end_val[:4]): start_val, end_val = end_val, start_val + elif not _start_is_digit and _end_is_digit and int(start_val[:4]) > int(end_val): + start_val, end_val = end_val, start_val # 确定两端是否是 4 位年份 _start_is_year = start_val.isdigit() and len(start_val) == 4 _end_is_year = end_val.isdigit() and len(end_val) == 4 @@ -717,10 +719,15 @@ def parse_pubmed_query(query: str) -> ParsedPubmedQuery: parser = PubmedQueryParser(tokens) return parser.parse() except (ParseError, IndexError, ValueError): - # P0-1: 降级时返回原始查询作为 plain_terms,不丢失用户输入 + # P0-1: 降级时清理查询中的 [field] 标签、布尔符、引号和括号 degraded = ParsedPubmedQuery() - for t in query.strip().split(): - degraded.plain_terms.append(Term(text=t)) + import re as _degrade_re + _clean = _degrade_re.sub(r'\[[\w/: -]+\]', '', query) + _clean = _degrade_re.sub(r'\b(AND|OR|NOT)\b', '', _clean) + _clean = _clean.replace('"', '').replace('(', '').replace(')', '') + for t in _clean.split(): + if t.strip(): + degraded.plain_terms.append(Term(text=t.strip())) return degraded diff --git a/backend/app/services/query_expansion.py b/backend/app/services/query_expansion.py index 080c7f5..44f3891 100644 --- a/backend/app/services/query_expansion.py +++ b/backend/app/services/query_expansion.py @@ -99,7 +99,7 @@ async def _find_mesh_tags(db: AsyncSession, query: str) -> list[UUID]: stmt = select(GlobalTag.id).where( GlobalTag.source.in_(["mesh", "manual"]), GlobalTag.name_zh.ilike(like_pattern), - ) + ).limit(100) rows = await db.execute(stmt) for (tid,) in rows: if tid not in seen: diff --git a/backend/app/services/search_engine.py b/backend/app/services/search_engine.py index ff1292c..cac5768 100644 --- a/backend/app/services/search_engine.py +++ b/backend/app/services/search_engine.py @@ -219,7 +219,7 @@ class AdvancedSearchEngine: or _pubmed_parsed.year_from or _pubmed_parsed.year_to) ]): # 解析失败但检测到 PubMed 语法 — 擦除 [field] 标签、布尔符、引号 - query = re.sub(r'\[[\w/-]+\]', '', query) # P5: [\w/-] 覆盖 [Title/Abstract] + query = re.sub(r'\[[\w/: -]+\]', '', query) # P5: [\w/: -] 覆盖 [Title/Abstract] 和 [MH:noexp] query = re.sub(r'\b(AND|OR|NOT)\b', '', query) query = query.replace('"', '').replace('(', '').replace(')', '') query = ' '.join(query.split()) @@ -245,8 +245,8 @@ class AdvancedSearchEngine: terms = [p for p in _phrases if p.strip()] + [t for t in _rest if t not in _phrases] # 单数字词:优先 PMID 精确匹配(unique index 5ms 返回) # 不是 PMID 时才回退到 ILIKE 兜底(DOI 片段等),不做 tsquery 避免 seq scan - numeric_terms = [t for t in terms if t.isdecimal() and len(t) <= 15] - text_terms = [t for t in terms if not (t.isdecimal() and len(t) <= 15)] + numeric_terms = [t for t in terms if re.match(r'^\d{1,15}$', t)] + text_terms = [t for t in terms if not re.match(r'^\d{1,15}$', t)] if numeric_terms: num_conds = [] if exact_phrase: @@ -287,6 +287,7 @@ class AdvancedSearchEngine: try: _atm_cond = await _expand_atm(db, _atm_query) except Exception: + logger.exception("ATM expansion failed (flat text): %s", _atm_query[:100]) _atm_cond = None _cond_before = len(conditions) @@ -335,8 +336,9 @@ class AdvancedSearchEngine: subq = select(GlobalJournal.issn).where(GlobalJournal.tier.in_(journal_tiers)) result = await db.execute(subq) issns = [r for (r,) in result.all()] - if issns: - conditions.append(GlobalLiterature.journal_issn.in_(issns)) + if not issns: + logger.warning("journal_tiers filter matched zero journals: %s", journal_tiers) + conditions.append(GlobalLiterature.journal_issn.in_(issns)) # 标签筛选(含子标签递归) if tag_ids: @@ -386,8 +388,9 @@ class AdvancedSearchEngine: subq = select(GlobalJournal.issn).where(GlobalJournal.nlm_subsets.overlap(nlm_subsets)) result = await db.execute(subq) issns = [r for (r,) in result.all()] - if issns: - conditions.append(GlobalLiterature.journal_issn.in_(issns)) + if not issns: + logger.warning("nlm_subsets filter matched zero journals: %s", nlm_subsets) + conditions.append(GlobalLiterature.journal_issn.in_(issns)) # ── PubMed 筛选器 ── @@ -494,7 +497,7 @@ class AdvancedSearchEngine: # 排序(PubMed 查询时跳过 ts_rank,避免语法标签噪音) _relevance_query = query - if _pubmed_parsed and sort == "relevance": + if _pubmed_parsed and sort in ("relevance", "best_match"): # 用纯文本词做相关性排序,去掉 [field] 标签 plain_parts = [t.text for t in _pubmed_parsed.plain_terms] plain_parts += [t.text for t in _pubmed_parsed.title_terms] @@ -609,6 +612,7 @@ class AdvancedSearchEngine: term_conditions: list = [] # 1. 字段级搜索 [TI] [AB] [TIAB] [AU] [TA] [LA] [VI] [IP] [PG] [LID] + field_combine = or_ if pp.boolean_operator == "or" else and_ field_map = { "title": pp.title_terms, "abstract": pp.abstract_terms, @@ -631,7 +635,7 @@ class AdvancedSearchEngine: if term.is_not: cond = not_(cond) field_conds.append(cond) - term_conditions.append(and_(*field_conds) if len(field_conds) > 1 else field_conds[0]) + term_conditions.append(field_combine(*field_conds) if len(field_conds) > 1 else field_conds[0]) # 2. 纯文本词(无字段标签)— P0-2: 对无标签词补充 ATM MeSH 展开 if pp.plain_terms: @@ -648,14 +652,15 @@ class AdvancedSearchEngine: try: atm_cond = await _expand_atm_inline(db, combined) except Exception: + logger.exception("ATM expansion failed (pubmed plain_terms): %s", combined[:100]) atm_cond = None if atm_cond is not None: - text_cond = and_(*plain_conds) if len(plain_conds) > 1 else plain_conds[0] + text_cond = field_combine(*plain_conds) if len(plain_conds) > 1 else plain_conds[0] term_conditions.append(or_(atm_cond, text_cond)) else: - term_conditions.append(and_(*plain_conds) if len(plain_conds) > 1 else plain_conds[0]) + term_conditions.append(field_combine(*plain_conds) if len(plain_conds) > 1 else plain_conds[0]) else: - term_conditions.append(and_(*plain_conds) if len(plain_conds) > 1 else plain_conds[0]) + term_conditions.append(field_combine(*plain_conds) if len(plain_conds) > 1 else plain_conds[0]) # 3. [MH] → tree_number 展开,支持 is_not 和 _noexp if pp.mesh_terms: @@ -964,9 +969,9 @@ class AdvancedSearchEngine: # 6. [DP] → 年份/日期范围 dp_negated = "DP" in getattr(pp, 'negated_date_ranges', set()) dp_conds = [] - if pp.year_from: + if pp.year_from is not None: dp_conds.append(GlobalLiterature.pub_year >= pp.year_from) - if pp.year_to: + if pp.year_to is not None: dp_conds.append(GlobalLiterature.pub_year <= pp.year_to) if pp.date_from: from datetime import date as _dt_date @@ -1095,10 +1100,18 @@ class AdvancedSearchEngine: if field == "TT": return GlobalLiterature.vernacular_title.ilike(f"%{_escape_ilike(term.text)}%") if field == "SB": - subq = select(GlobalJournal.issn).where( - GlobalJournal.nlm_subsets.overlap([term.text.upper()]) - ) - return GlobalLiterature.journal_issn.in_(subq) + val = term.text.upper() + if val == "PUBMED": + return text("TRUE") # no-op: 所有记录都是 PubMed + elif val == "MEDLINE": + return GlobalLiterature.citation_status == "medline" + elif len(val) == 1 and val.isalpha(): + subq = select(GlobalJournal.issn).where( + GlobalJournal.nlm_subsets.overlap([val]) + ) + return GlobalLiterature.journal_issn.in_(subq) + else: + return GlobalLiterature.citation_status == val.lower() if field == "STAT": return GlobalLiterature.citation_status == term.text.lower() if field == "UID": @@ -1239,7 +1252,7 @@ class AdvancedSearchEngine: for (tid,) in rows: mesh_tag_ids.add(tid) except Exception: - pass + logger.exception("MeSH tag lookup failed for mesh_names=%s", mesh_names[:5]) if not mesh_tag_ids: return None @@ -1263,7 +1276,7 @@ class AdvancedSearchEngine: )).scalars().all() mesh_tag_ids.update(children) except Exception: - pass + logger.exception("Tree number expansion failed for mesh_names=%s", mesh_names[:5]) uids = list(mesh_tag_ids) if major_only: diff --git a/docs/13-搜索修复全记录.md b/docs/13-搜索修复全记录.md index 10ea69d..d6c317b 100644 --- a/docs/13-搜索修复全记录.md +++ b/docs/13-搜索修复全记录.md @@ -2,7 +2,7 @@ > 本文档按修复轮次详细记录所有搜索功能合规性修复的背景、根因分析和修改内容。 > -> **累计**:6 轮,75 项修复,50+ 字段标签注册,248 项测试覆盖 +> **累计**:7 轮,95 项修复,50+ 字段标签注册,276 项测试覆盖 > **时间跨度**:2026-07-24 ~ 2026-07-27 > **核心文件**:`pubmed_query_parser.py`(~730 行)→ `search_engine.py`(~1320 行) @@ -481,6 +481,146 @@ --- +## 第七轮:第 7 轮深度审计修复(20 项) + +**日期**:2026-07-27 +**数量**:20 项(4 Agent 第 2 轮并行审计) +**触发**:用户"再次全面、深入地检查、分析,消除漏洞" +**测试**:276 通过(新增 ~28 项),13 项预存失败 + +### P7-1: `isdecimal()` 非 ASCII 数字崩溃 PMID 检测(HIGH) + +- **文件**:`search_engine.py` `search()` L245-246 +- **问题**:`str.isdecimal()` 对阿拉伯数字 U+0660 等返回 True,但 `int()` 不接受非 ASCII 数字 → `ValueError`,搜索返回 500 +- **根因**:Python 的 isdecimal() 包含 Unicode 数字字符,int() 只认 ASCII +- **修复**:`t.isdecimal()` → `re.match(r'^\d{1,15}$', t)` + +### P7-2: 降级 regex `[\w/: -]` 兼容冒号 + +- **文件**:`search_engine.py` `search()` L219 +- **问题**:`[MH:noexp]` 标签中的冒号不在 `[\w/-]` 内,降级后冒号残留 +- **根因**:regex 缺少 `:` 和空格 +- **修复**:`[\w/-]` → `[\w/: -]` + +### P7-3: Parse 异常处理器清除字段标签/布尔符/引号 + +- **文件**:`pubmed_query_parser.py` `parse_pubmed_query()` L719-726 +- **问题**:降级时 `query.strip().split()` 产生含 `"`、`[`、`]` 的碎片词,传递给 ATM 和 ILIKE 产生无意义匹配 +- **根因**:降级路径未做任何清理 +- **修复**:先用 regex 去掉 `[field]` 标签、AND/OR/NOT、引号和括号,再 split + +### P7-4: `_parse_range` date:year 反向交换(第 4 分支) + +- **文件**:`pubmed_query_parser.py` `_parse_range()` L621 +- **问题**:`2026-06-01:2024[DP]` 开始日期、结束年份时未交换 +- **根因**:缺少 `not _start_is_digit and _end_is_digit` 分支 +- **修复**:增加第 4 分支处理 date:year 反向 + +### P7-5: `_pubmed_conditions` boolean_operator 应用到字段分组(HIGH) + +- **文件**:`search_engine.py` `_pubmed_conditions()` L612, L635 +- **问题**:字段分组(title、abstract 等)内全部用 `and_()` 组合,无视 `boolean_operator="or"` +- **根因**:字段分组硬编码 `and_()` +- **修复**:定义 `field_combine = or_ if boolean_operator=="or" else and_` + +### P7-6: `_pubmed_conditions` boolean_operator 应用到无标签词(HIGH) + +- **文件**:`search_engine.py` `_pubmed_conditions()` L652-659 +- **问题**:纯文本词固定 AND,分开写的 `cancer OR tumor` 实际变 AND +- **根因**:plain_conds 硬编码 `and_()` +- **修复**:全部改用 `field_combine` + +### P7-7: best_match 排序剥离字段标签 + +- **文件**:`search_engine.py` `search()` L497 +- **问题**:sort=="best_match" 时未剥离标签词,`[TI]` 参与 ts_rank 产生噪音 +- **根因**:条件只检查 `sort == "relevance"` +- **修复**:改为 `sort in ("relevance", "best_match")` + +### P7-8: year_from/year_to 精确空值检测 + +- **文件**:`search_engine.py` `_pubmed_conditions()` L969-972 +- **问题**:`if pp.year_from:` 当 year_from=0 时 falsy → 条件跳过 +- **根因**:falsy 检测不适用于年份 0 +- **修复**:`if pp.year_from is not None` + +### P7-9: `_single_term_condition` SB 字段全量分发 + +- **文件**:`search_engine.py` `_single_term_condition()` L1100-1117 +- **问题**:组内 SB 只有 nlm_subset 路径,PUBMED/MEDLINE/其他未处理 +- **根因**:括号分组内调 `_single_term_condition`,与顶层分发不一致 +- **修复**:复制顶层 SB 全量分发逻辑 + +### P7-10: journal_tiers/nlm_subsets 空条件预警 + +- **文件**:`search_engine.py` `search()` L336-341, L388-393 +- **问题**:筛选项匹配 0 个期刊时条件被跳过,用户收到全量结果而非 0 结果 +- **根因**:`if issns:` 保护,空→跳过 +- **修复**:始终添加条件,空 ISSNS 时 0 结果 + `logger.warning` + +### P7-11: ATM 展开异常日志化 + +- **文件**:`search_engine.py` `search()` L287, `_pubmed_conditions()` L655 +- **问题**:flat text 和 pubmed 路径 ATM 异常都用裸 `except Exception: pass` +- **修复**:改为 `logger.exception()` + +### P7-12: `_expand_mesh_tag_ids` 异常日志化 + +- **文件**:`search_engine.py` `_expand_mesh_tag_ids()` L1239, L1276 +- **问题**:MeSH tag 查找和树展开的 `except Exception: pass` +- **修复**:改为 `logger.exception()` + +### P7-13: 中文 name_zh ILIKE 加 LIMIT 100 + +- **文件**:`query_expansion.py` `_find_mesh_tags()` L99 +- **问题**:中文 name_zh ILIKE 无 LIMIT,常见词匹配数千标签 +- **根因**:英文 name_en ILIKE 已有 LIMIT 100,中文忘记加 +- **修复**:`.limit(100)` + +### P7-14: Cursor 分页使用 pub_date 优先(HIGH) + +- **文件**:`SearchView.vue` L358 +- **问题**:sort 是 pub_date 降序,cursor 却用 article_date → 数据错位/丢失 +- **修复**:改为 `pub_date || article_date` + +### P7-15: Null cursor 日期安全守卫 + +- **文件**:`SearchView.vue` L361-363 +- **问题**:两个日期都为空时 cursor_date="" → `fromisoformat("")` ValueError → 静默回退 offset 分页 +- **修复**:`delete keysetCursors.value[p+1]` 当两日期都为空 + +### P7-16: syncSearchToUrl 移到 finally 块 + +- **文件**:`SearchView.vue` L365, L373-376 +- **问题**:搜索失败时 URL 状态不更新,下次搜索使用过时参数 +- **修复**:移到 `finally` 块 + +### P7-17: 年份滑块清除 URL 日期 + +- **文件**:`SearchView.vue` `onYearSliderChange()` L89 +- **问题**:拖动年份滑块后 URL 残留 `date_from`/`date_to` 与滑块设置冲突 +- **修复**:添加 `urlDateFrom.value=''; urlDateTo.value=''` + +### P7-18: resetAllFilters 清除 URL 日期 + +- **文件**:`SearchView.vue` `resetAllFilters()` L528 +- **问题**:重置筛选后 urlDateFrom/urlDateTo 依然存在 +- **修复**:添加 `urlDateFrom.value=''; urlDateTo.value=''` + +### P7-19: MAX_TERMS 保护 + +- **文件**:`pubmed_query_parser.py` `tokenise()` +- **问题**:超长查询(>200 token)产生过多字段条件,数据库超时 +- **修复**:扫描到 `MAX_TERMS=200` 后截断并记录 warning + +### P7-20: `_FIELD_TAG_MAP` 补充 `TITLE/ABSTRACT` 大写键 + +- **文件**:`pubmed_query_parser.py` `_FIELD_TAG_MAP` +- **问题**:解析器 `.upper()` 产生 `"TITLE/ABSTRACT"` 但 map 只有 `"Title/Abstract"` +- **修复**:增加大写键 + +--- + ## 遗留限制 截至 2026-07-27,剩余 7 项已知限制: @@ -504,4 +644,7 @@ | `test_pubmed_query_parser.py` | ~40 | tokeniser、解析器、语法正确性 | | `test_pubmed_search_integration.py` | ~60 | 字段映射、API 集成、前端格式 | | `test_comprehensive_verify.py` | ~27 | 字段完整、NOT 语义、括号、日期 | -| **合计** | **214** | 全部通过 | +| `test_comprehensive_verify.py` | ~55 | 第 7 轮新增覆盖(full dispatch、boolean_operator、cursor 等) | +| **合计** | **276** | 全部通过 | + +> **预存失败(13 项)**:9 项 `feed_engine` `StopAsyncIteration`(测试数据缺失) + 4 项 `pubmed_api` `_tag_article` import(函数已移入 pipeline) diff --git a/frontend/src/views/app/SearchView.vue b/frontend/src/views/app/SearchView.vue index 1c78d16..116ce66 100644 --- a/frontend/src/views/app/SearchView.vue +++ b/frontend/src/views/app/SearchView.vue @@ -89,6 +89,7 @@ function onYearSliderChange(val: any) { yearFromStr.value = String(val[0]) yearToStr.value = String(val[1]) datePreset.value = null + urlDateFrom.value = ''; urlDateTo.value = '' // P6: clear stale URL dates if (_sliderTimer) clearTimeout(_sliderTimer) _sliderTimer = setTimeout(() => goToPage(1), 250) } @@ -352,20 +353,26 @@ const { page, total, goToPage } = usePagination({ if (items.length > 0) { const last = items[items.length - 1] keysetCursors.value[p + 1] = { - cursor_date: last.article_date || last.pub_date || '', + cursor_date: last.pub_date || last.article_date || '', cursor_id: last.id, } + // 当两个日期都为空时,不设游标(避免空串 fromisoformat 失败) + if (!last.pub_date && !last.article_date) { + delete keysetCursors.value[p + 1] + } } } else { total.value = data.total || 0 } yearCounts.value = data.year_counts || [] - syncSearchToUrl() } catch (e: any) { if (e?.name === 'CanceledError' || e?.code === 'ERR_CANCELED') return toast.apiError(e, '搜索失败,请重试') } - finally { if (gen === searchGeneration.value) loading.value = false } + finally { + syncSearchToUrl() // P6: sync URL even on error (avoid URL/state desync) + if (gen === searchGeneration.value) loading.value = false + } }, pageSize: pageSize.value, }) @@ -518,6 +525,7 @@ function syncSearchToUrl() { function resetAllFilters() { yearFromStr.value = ''; yearToStr.value = '' + urlDateFrom.value = ''; urlDateTo.value = '' // P6: clear stale URL dates datePreset.value = null selectedTiers.value = [] selectedTags.value = []