LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。
TypeScript
fix: search ranks exact matches first — filename/phrase bonuses dominate
nash_suda5c8dff7f75
diff --git a/src/lib/search.ts b/src/lib/search.tsindex c44c6ca0..8a4ff7cc 100644--- a/src/lib/search.ts+++ b/src/lib/search.ts@@ -12,7 +12,31 @@ export interface SearchResult {const MAX_RESULTS = 20const SNIPPET_CONTEXT = 80-const TITLE_MATCH_BONUS = 10++// ── Scoring weights ────────────────────────────────────────────────────────+// Exact lexical matches dominate everything else. The rationale: when a+// user types "attention", the page literally named `attention.md` MUST+// rank first, regardless of how many other pages also mention the word.+//+// filename == query (e.g. `attention.md` for query "attention")+// → FILENAME_EXACT_BONUS — large enough that nothing short of an+// equally-exact match can outrank it.+//+// title or content contains the raw query as a substring+// → PHRASE_IN_TITLE_BONUS / PHRASE_IN_CONTENT_PER_OCC — phrase+// presence is worth far more than individual token presence, and+// in content it rewards repetition (with a cap to avoid runaway).+//+// per-token matches (existing behavior, but now smaller weight)+// → TITLE_TOKEN_WEIGHT / CONTENT_TOKEN_WEIGHT. These used to+// dominate via a flat +10 title bonus regardless of how many+// tokens matched; now each matched token counts individually.+const FILENAME_EXACT_BONUS = 200+const PHRASE_IN_TITLE_BONUS = 50+const PHRASE_IN_CONTENT_PER_OCC = 20+const MAX_PHRASE_OCC_COUNTED = 10 // cap to avoid runaway on huge logs+const TITLE_TOKEN_WEIGHT = 5+const CONTENT_TOKEN_WEIGHT = 1const STOP_WORDS = new Set(["的", "是", "了", "什么", "在", "有", "和", "与", "对", "从",@@ -70,6 +94,19 @@ function tokenMatchScore(text: string, tokens: readonly string[]): number {return score}+function countOccurrences(haystackLower: string, needleLower: string): number {+ if (!needleLower || needleLower.length === 0) return 0+ let count = 0+ let pos = 0+ while (true) {+ const idx = haystackLower.indexOf(needleLower, pos)+ if (idx === -1) break+ count+++ pos = idx + needleLower.length+ }+ return count+}+function flattenMdFiles(nodes: FileNode[]): FileNode[] {const files: FileNode[] = []for (const node of nodes) {@@ -221,6 +258,8 @@ async function searchFiles(query: string,results: SearchResult[],): Promise<void> {+ const queryPhrase = query.trim().toLowerCase()+for (const file of files) {let content = ""try {@@ -231,24 +270,54 @@ async function searchFiles(const title = extractTitle(content, file.name)const titleText = `${title} ${file.name}`+ const titleLower = titleText.toLowerCase()+ const contentLower = content.toLowerCase()+ const fileStem = file.name.replace(/\.md$/, "").toLowerCase()++ // Exact-match signals (strongest)+ const filenameExact = fileStem === queryPhrase+ const titleHasPhrase =+ queryPhrase.length > 0 && titleLower.includes(queryPhrase)+ const contentPhraseOcc = Math.min(+ countOccurrences(contentLower, queryPhrase),+ MAX_PHRASE_OCC_COUNTED,+ )++ // Token-level signals (fallback / density)+ const titleTokenScore = tokenMatchScore(titleText, tokens)+ const contentTokenScore = tokenMatchScore(content, tokens)++ // Must have at least one signal to be included+ if (+ !filenameExact &&+ !titleHasPhrase &&+ contentPhraseOcc === 0 &&+ titleTokenScore === 0 &&+ contentTokenScore === 0+ ) {+ continue+ }- const titleScore = tokenMatchScore(titleText, tokens)- const contentScore = tokenMatchScore(content, tokens)-- if (titleScore === 0 && contentScore === 0) continue+ const score =+ (filenameExact ? FILENAME_EXACT_BONUS : 0) ++ (titleHasPhrase ? PHRASE_IN_TITLE_BONUS : 0) ++ contentPhraseOcc * PHRASE_IN_CONTENT_PER_OCC ++ titleTokenScore * TITLE_TOKEN_WEIGHT ++ contentTokenScore * CONTENT_TOKEN_WEIGHT- const isTitleMatch = titleScore > 0- const score = contentScore + (isTitleMatch ? TITLE_MATCH_BONUS : 0)+ const isTitleMatch = titleTokenScore > 0 || titleHasPhrase- const firstMatchingToken = tokens.find((t) =>- content.toLowerCase().includes(t),- ) ?? query- const snippet = buildSnippet(content, firstMatchingToken)+ // Prefer snipping around the full phrase when it exists; otherwise+ // pick the first matching token; otherwise the raw query.+ const snippetAnchor =+ contentPhraseOcc > 0+ ? queryPhrase+ : tokens.find((t) => contentLower.includes(t)) ?? queryresults.push({path: file.path,title,- snippet,+ snippet: buildSnippet(content, snippetAnchor),titleMatch: isTitleMatch,score,})diff --git a/src/test-helpers/scenarios/search-scenarios.ts b/src/test-helpers/scenarios/search-scenarios.tsindex ceb15e68..2910b36c 100644--- a/src/test-helpers/scenarios/search-scenarios.ts+++ b/src/test-helpers/scenarios/search-scenarios.ts@@ -106,4 +106,69 @@ export const searchScenarios: SearchScenario[] = [// 'other' uses "the" a lot but should be filtered out and not rank top},},++ // 6. filename-exact-match-wins — the page whose filename stem EQUALS the+ // query must rank #1 even when longer, denser pages also mention the+ // word many times. Regression guard for the "精准匹配排到最后" bug.+ {+ name: "filename-exact-match-wins",+ description:+ "Query 'attention'. Many pages contain 'attention' in title AND body. " ++ "Only attention.md has the exact filename match — it must rank first.",+ initialWiki: {+ "wiki/attention.md": page("Attention", "Attention is a weighting mechanism."),+ "wiki/multi-head-attention.md": page(+ "Multi-Head Attention",+ "Multi-head attention extends attention across multiple heads. " ++ "Attention weights are computed per head and combined. " ++ "Each head attends to different patterns. Attention attention attention.",+ ),+ "wiki/self-attention-history.md": page(+ "Self-Attention: A History",+ "Self-attention appeared before multi-head attention. " ++ "Attention, attention, attention — the dominant mechanism in NLP.",+ ),+ "wiki/transformer-deep-dive.md": page(+ "Transformer Deep Dive",+ "The transformer uses attention everywhere. Attention in encoder, " ++ "attention in decoder, cross-attention between them. Attention rules.",+ ),+ },+ query: "attention",+ expected: {+ topResultPaths: ["wiki/attention.md"],+ titleMatchPaths: [+ "wiki/attention.md",+ "wiki/multi-head-attention.md",+ "wiki/self-attention-history.md",+ ],+ },+ },++ // 7. phrase-in-content-beats-scattered-tokens — a page that contains the+ // raw query phrase should outrank a page with the same tokens scattered.+ {+ name: "phrase-in-content-beats-scattered-tokens",+ description:+ "Query 'self-attention'. tokens split to ['self','attention']. " ++ "The page containing the literal phrase 'self-attention' should " ++ "outrank the page where 'self' and 'attention' appear separately.",+ initialWiki: {+ "wiki/phrase-page.md": page(+ "Mechanisms",+ "This discusses self-attention, the key building block of transformers. " ++ "Self-attention is what makes transformers powerful.",+ ),+ "wiki/scattered-page.md": page(+ "Scattered",+ "Self-improvement is a goal. Attention to detail is important. " ++ "Self-help books discuss attention spans. " ++ "This page talks about self and about attention separately.",+ ),+ },+ query: "self-attention",+ expected: {+ topResultPaths: ["wiki/phrase-page.md"],+ },+ },]