feat(query): exact, lexical, fuzzy, graph and slice query surface

- exact channel resolves canonical ids, aliases and unique exact names only;
  multiple exact names are returned as candidates, never silently chosen
- lexical channel uses weighted FTS5 BM25 over CJK-bigram segmented fields and
  reports the matching field; fuzzy channel recalls via the gram index with
  bounded edit distance, CJK single-character and prefix fallbacks for typos
  whose bigrams do not overlap
- weighted RRF fuses per-node ranks (exact 1.5, lexical/fuzzy/semantic 1.0) and
  bounded graph expansion follows references both ways, contains children,
  calls/imports in their direction with --relation/--reverse, depth decay and
  explicit truncation; --kind filters after expansion
- document/execution/hybrid slices share one plan+materialise traversal, skip
  ancestor duplication, include module context for execution imports, report
  external/dynamic gaps, enforce the 32 KiB budget on UTF-8 boundaries and keep
  exact source text with path/span provenance
- stale databases are refused unless --snapshot is explicit; query never
  compiles implicitly and emits the single result envelope with honest
  channels/truncated/diagnostics
- add end-to-end query tests for CJK recall, typo recall, exact/ambiguous ids,
  kind filtering, graph neighbours, slices, staleness, missing database,
  no-result honesty and explicit semantic-config failure
This commit is contained in:
marqdo-knowledge committed 2026-11-26 01:46:17 +08:00
1 parent 66f3e168c4
commit 514d9fbd4c
10 files changed
+2414 -16

No files matched your search

+29
View File
@@ -235,6 +235,35 @@ pub fn similarity(left: &str, right: &str) -> f64 {
}
}
/// Best similarity between the query and any character window of `text` whose
/// length is close to the query length. Bounded to short strings; used only for
/// fuzzy ranking. This tolerates a title suffix ("归一化动机" vs "归依化") without
/// scanning the corpus.
pub fn best_window_similarity(query: &str, text: &str) -> f64 {
let normalized_query = normalize(query);
let normalized_text = normalize(text);
let query_chars: Vec<char> = normalized_query.chars().collect();
let text_chars: Vec<char> = normalized_text.chars().collect();
if query_chars.is_empty() || text_chars.is_empty() {
return 0.0;
}
if text_chars.len() <= query_chars.len() {
return similarity(&normalized_query, &normalized_text);
}
let width = query_chars.len();
let mut best = 0.0f64;
let max_start = text_chars.len().saturating_sub(width).min(32);
for start in 0..=max_start {
let end = (start + width).min(text_chars.len());
let window: String = text_chars[start..end].iter().collect();
best = best.max(similarity(&normalized_query, &window));
if best >= 0.999 {
break;
}
}
best
}
/// Escape one token for an FTS5 query: the token is emitted as a quoted string
/// literal, so no FTS operator can be injected. Empty tokens are dropped.
pub fn fts_quote(token: &str) -> Option<String> {