Merge pull request #1541 from CyC2018/site-rank2

Update search.js
This commit is contained in:
CyC2018
2026-11-06 05:40:25 +08:00
committed by GitHub
2 changed files with 34 additions and 3 deletions
+20
View File
@@ -48,6 +48,26 @@ assert.equal(search(phrasing, '和为 S').length, 1, '没有正文的开头部
assert.equal(search(phrasing, '什么是').length, 0, '只有问法时没有结果');
assert.equal(highlight('a<b 的 TCP 与 tcp', 'tcp'), 'a&lt;b 的 <mark>TCP</mark> 与 <mark>tcp</mark>', '搜索词高亮不区分大小写,其余文字转义');
assert.equal(highlight('&amp; (x)', 'amp (x)'), '&amp;<mark>amp</mark>; <mark>(x)</mark>', '高亮先切分再转义,正则字符按字面匹配');
const generic = { documents: [
{ title: '学习路线 - 示例', path: '/notes/学习路线 - 示例', sections: [{ title: '第 3 天:锁', id: 'day', text: '阅读 synchronized 的原理' }] },
{ title: 'Spring', path: '/notes/Spring', sections: [
{ title: '七、Spring 中的设计模式', id: 'pattern', text: 'AOP 用到了代理模式' },
{ title: '二、AOP', id: 'aop', text: 'Spring AOP 在运行时为 Bean 生成代理' },
] },
{ title: '并发', path: '/notes/并发', sections: [{ title: 'synchronized', id: 'sync', text: 'JVM 用监视器锁实现' }] },
{ title: '大模型', path: '/notes/大模型', sections: [
{ title: '长上下文与 Agentic RAG', id: 'agentic', text: 'RAG 的变体' },
{ title: '三、检索增强生成', id: 'rag', text: '先检索再生成' },
] },
{ title: '进程管理', path: '/notes/进程管理', sections: [
{ title: '进程与线程', id: 'both', text: '进程是资源分配的单位,线程是调度的单位' },
{ title: '2. 线程', id: 'thread', text: '线程共享进程的资源' },
] },
] };
assert.equal(search(generic, 'synchronized 原理')[0].id, 'sync', '“原理”这类泛泛的词正文中没有时,去掉后再搜');
assert.equal(search(generic, 'spring aop')[0].id, 'aop', '文章名中的词出现在小节标题里少加分');
assert.equal(search(generic, 'rag 是什么')[0].id, 'rag', 'RAG 与检索增强生成互通,去掉编号后正好是搜索词的小节在前');
assert.equal(search(generic, '进程和线程的区别')[0].id, 'both', '多个词时,只匹配其中一个词的小节不算正好匹配');
const sample = { documents: [{ title: '数据库', path: '/notes/示例', sections: [
{ title: '事务', id: '事务', text: '一致性 '.repeat(20) },
{ title: '一致性', id: '一致性', text: 'SQL 与数据库' },
+14 -3
View File
@@ -20,7 +20,7 @@
// 学习路线只是阅读导读,附录是实验过程,排在讲解正文之后
var penalty = (/^学习路线/.test(doc.title) ? 45 : 0) + (/^a\d+ /.test(heading) ? 40 : 0);
prepared.push({ page: page, heading: heading, body: body, text: heading + ' ' + page + ' ' + body,
lead: body.slice(0, 120), flatHeading: heading.replace(/ /g, ''), flatBody: body.replace(/ /g, ''), penalty: penalty, pageTitle: doc.title, path: doc.path, section: section });
pageParts: page.split(' - ').map(function (x) { return x.trim(); }), bare: heading.replace(/^(?:[一二三四五六七八九十]+、|[a-z]?\d+(?:\.\d+)*\.?\s)\s*/, ''), lead: body.slice(0, 120), flatHeading: heading.replace(/ /g, ''), flatBody: body.replace(/ /g, ''), penalty: penalty, pageTitle: doc.title, path: doc.path, section: section });
});
});
preparedIndexes.set(index, prepared);
@@ -58,7 +58,7 @@
var SYNONYMS = [
['垃圾收集', '垃圾回收'], ['i/o', 'io'], ['哈希', '散列', 'hash'], ['新生代', '年轻代'], ['重排序', '指令重排'],
['首部', '头部'], ['幻读', '幻影读'], ['脏读', '读脏数据'], ['b+tree', 'b+树', 'b+ 树'], ['多路复用', 'i/o 复用', '复用'],
['多版本并发控制', 'mvcc'], ['java 虚拟机', 'jvm']
['多版本并发控制', 'mvcc'], ['java 虚拟机', 'jvm'], ['检索增强生成', 'rag'], ['集群', 'cluster']
];
// 中英文混写的术语(“b+ 树”“i/o 复用”)在切词前换成一个整体,否则会被切成“b+”和“树”两个词分别匹配
var MIXED = [];
@@ -101,7 +101,10 @@
if (!term) return null;
// 小节标题正好是搜索词时最相关,例如“死锁的必要条件”中的“必要条件”;标题用的是另一种说法时也算(搜 MVCC,标题是“多版本并发控制”)
var head = firstHit(entry.heading, groups[i]) || term;
total += (entry.heading === head ? 30 : 0) + (entry.heading.includes(head) ? (wholeWord(entry.heading, head) ? 40 : 25) : 0) +
// 多个词时,就是文章名(或“ - ”分开的一段)的词只说明是哪篇文章,出现在小节标题中少加分(“spring aop”中的 spring)
var headBonus = groups.length > 1 && entry.pageParts.indexOf(head) !== -1 ? 10 : wholeWord(entry.heading, head) ? 40 : 25;
// 小节标题正好是搜索词时最相关;只有一个词时去掉编号(“三、”“2.”)再比
total += (entry.heading === head || (groups.length === 1 && entry.bare === head) ? 30 : 0) + (entry.heading.includes(head) ? headBonus : 0) +
(entry.page.includes(term) ? 15 : 0) + (entry.lead.includes(term) ? 10 : 0) + occurrences(entry.body, term);
}
var phrase = groups.length > 1 && normalizedQuery ? normalizedQuery.replace(/ /g, '') : '';
@@ -116,6 +119,11 @@
var terms = splitTerms(normalize(query.replace(QUESTION, ' '))).filter(function (term) { return term.length > 1; });
return terms.length ? terms.join(' ') : '';
}
var GENERIC = /(底层)?(原理|机制|实现方式|实现|作用|流程|过程|优缺点|应用场景|使用场景|场景|特点|详解|介绍|用法)/g;
function stripGeneric(query) {
var terms = splitTerms(query.replace(GENERIC, ' ')).filter(function (term) { return term.length > 1; });
return terms.length ? terms.join(' ') : '';
}
// 仍然没有结果时按相邻两个字的词组匹配,至少命中七成;原文中连续出现的长片段(“四个条件”)额外加分
function bigrams(terms) {
var pairs = [];
@@ -169,6 +177,9 @@
// 去掉问法后的结果合并进来:“死锁的必要条件”原文只出现在学习路线中,正文的小节标题是“必要条件”
var stripped = stripQuestion(normalizedQuery);
if (stripped && stripped !== normalizedQuery) exact(stripped);
// “原理”“机制”这类泛泛的词正文中不一定出现(“synchronized 原理”),去掉后再搜一次;结果比带着这些词命中的少一个词的分
var core = stripGeneric(stripped || normalizedQuery);
if (core && core !== normalizedQuery && core !== stripped) exact(core);
if (!results.length) {
var terms = splitTerms(stripped || normalizedQuery);
var pairs = bigrams(terms.map(function (term) { return alternatives(term)[0]; }));