Merge pull request #1561 from CyC2018/search-parent

Update search ranking with parent headings
This commit is contained in:
CyC2018
2026-11-07 06:36:01 +08:00
committed by GitHub
4 changed files with 20 additions and 4 deletions
+1 -1
View File
@@ -122,7 +122,7 @@ def search_document(path, content):
if heading:
finish()
raw_title = heading[2]
current = {'title': plain_text(raw_title), 'id': heading_slug(raw_title, counts), 'text': ''}
current = {'title': plain_text(raw_title), 'id': heading_slug(raw_title, counts), 'level': len(heading[1]), 'text': ''}
elif not re.match(r'^\s*[-*+]\s+\[[^]]+\]\(#[^)]*\)\s*(?:<span class="hot[^"]*"></span>)?\s*$', line):
lines.append(line)
finish()
+11
View File
@@ -122,6 +122,17 @@ const camel = { documents: [
] };
assert.equal(search(camel, 'SpringCloudAlibaba')[0].id, 'sc', '驼峰连写按拆开的写法也能搜到');
assert.equal(search(camel, 'LRUCache')[0].id, 'lru', '连续大写后接单词时在单词前拆开');
const parent = { documents: [
{ title: 'Java 虚拟机', path: '/notes/Java 虚拟机', sections: [
{ title: '双亲委派模型', id: 'pdm', level: 3, text: '应用程序是由三种类加载器互相配合从而实现类加载' },
{ title: '1. 工作过程', id: 'work', level: 4, text: '一个类加载器首先将类加载请求转发到父类加载器' },
{ title: '2. 好处', id: 'good', level: 4, text: '使得 Java 类随着它的类加载器一起具有一种带有优先级的层次关系,从而使得基础类得到统一' },
{ title: '三、类加载器分类', id: 'kind', level: 3, text: '启动类加载器、扩展类加载器、应用程序类加载器' },
{ title: '4. 好处与代价', id: 'cost', level: 4, text: '自定义类加载器的好处是可以隔离不同版本的类' },
] },
] };
const parentIds = search(parent, '双亲委派机制的好处').map(r => r.id);
assert.ok(parentIds.indexOf('good') !== -1 && parentIds.indexOf('good') < parentIds.indexOf('cost'), '小节标题只写“好处”时,按上级标题也能搜到');
const guide = { documents: [
{ title: '学习路线 - 示例', path: '/notes/学习路线 - 示例', sections: [{ title: '第 25 天:B+ 树与索引的类型', id: 'day', text: '自测:MySQL 的 B+Tree 为什么适合范围查询?' }] },
{ title: 'MySQL', path: '/notes/MySQL', sections: [
+1
View File
@@ -42,6 +42,7 @@ class SearchIndexTests(unittest.TestCase):
sections = [s for s in doc['sections'] if s['id'] == 'tcp-拥塞控制']
self.assertEqual(len(sections), 1)
self.assertEqual(sections[0]['text'], '慢开始')
self.assertEqual(sections[0]['level'], 2)
def test_comments_and_example_asset_urls_are_not_published(self):
content = '<!-- <img src="/secret.png"> -->\n```html\n<img src="/example.png">\n```\n<img src="/notes/pics/actual.png">\n[正文](/notes/示例.md)\n'
+7 -3
View File
@@ -12,8 +12,12 @@
if (prepared) return prepared;
prepared = [];
index.documents.forEach(function (doc) {
var page = normalize(doc.title);
var page = normalize(doc.title), parents = [];
doc.sections.forEach(function (section, i) {
// 上级小节的标题:“双亲委派模型”下的“2. 好处”,搜“双亲委派的好处”时要能找到
while (parents.length && parents[parents.length - 1].level >= section.level) parents.pop();
var parent = parents.length ? parents[parents.length - 1].heading : '';
if (section.level > 1) parents.push({ level: section.level, heading: normalize(section.title) });
// 文章开头没有正文的部分与一级标题重复,不单独作为结果
if (i === 0 && !section.text && doc.sections.length > 1 && normalize(doc.sections[1].title) === page) return;
var heading = normalize(section.title);
@@ -21,7 +25,7 @@
// 学习路线只是阅读导读,附录是实验过程,排在讲解正文之后
var penalty = (/^学习路线/.test(doc.title) ? 45 : 0) + (/^a\d+ /.test(heading) ? 40 : 0) + (/ - 进阶$/.test(doc.title) ? 8 : 0) + (heading === '参考资料' ? 30 : 0);
var bare = heading.replace(/^(?:[一二三四五六七八九十]+、|[a-z]?\d+(?:\.\d+)*\.?\s)\s*/, '');
prepared.push({ page: page, heading: heading, body: body, text: heading + ' ' + page + ' ' + body,
prepared.push({ page: page, heading: heading, parent: parent, body: body, text: heading + ' ' + parent + ' ' + page + ' ' + body,
pageParts: page.split(' - ').map(function (x) { return x.trim(); }), bare: bare, core: bare.replace(/(模型|原理|机制)$/, '').trim(), lead: body.slice(0, 120), flatHeading: heading.replace(/ /g, ''), flatBody: body.replace(/ /g, ''), penalty: penalty, pageTitle: doc.title, path: doc.path, section: section });
});
});
@@ -207,7 +211,7 @@
got += weights[i];
var head = entry.heading.includes(hit);
if (head) inHead++;
value += weights[i] * ((head ? 3 : 0) + (entry.page.includes(hit) ? 1 : 0) + Math.min(occurrences(entry.body, hit), 3) * .4);
value += weights[i] * ((head ? 3 : entry.parent.includes(hit) ? 1.5 : 0) + (entry.page.includes(hit) ? 1 : 0) + Math.min(occurrences(entry.body, hit), 3) * .4);
});
// 门槛按面经问题的开发集选定:命中的信息量不到 15% 的不算,标题中一个都没有命中的要到 40%
if (got < total * .15 || (!inHead && got < total * .4)) return;