Skip to content

Commit 96dfe1e

Browse files
authored
Merge pull request #1561 from CyC2018/search-parent
Update search ranking with parent headings
2 parents 541cf15 + 42b44ed commit 96dfe1e

4 files changed

Lines changed: 20 additions & 4 deletions

File tree

‎website/builder/site_assets.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def finish():
122122
if heading:
123123
finish()
124124
raw_title = heading[2]
125-
current = {'title': plain_text(raw_title), 'id': heading_slug(raw_title, counts), 'text': ''}
125+
current = {'title': plain_text(raw_title), 'id': heading_slug(raw_title, counts), 'level': len(heading[1]), 'text': ''}
126126
elif not re.match(r'^\s*[-*+]\s+\[[^]]+\]\(#[^)]*\)\s*(?:<span class="hot[^"]*"></span>)?\s*$', line):
127127
lines.append(line)
128128
finish()

‎website/builder/test_search.cjs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ const camel = { documents: [
122122
] };
123123
assert.equal(search(camel, 'SpringCloudAlibaba')[0].id, 'sc', '驼峰连写按拆开的写法也能搜到');
124124
assert.equal(search(camel, 'LRUCache')[0].id, 'lru', '连续大写后接单词时在单词前拆开');
125+
const parent = { documents: [
126+
{ title: 'Java 虚拟机', path: '/notes/Java 虚拟机', sections: [
127+
{ title: '双亲委派模型', id: 'pdm', level: 3, text: '应用程序是由三种类加载器互相配合从而实现类加载' },
128+
{ title: '1. 工作过程', id: 'work', level: 4, text: '一个类加载器首先将类加载请求转发到父类加载器' },
129+
{ title: '2. 好处', id: 'good', level: 4, text: '使得 Java 类随着它的类加载器一起具有一种带有优先级的层次关系,从而使得基础类得到统一' },
130+
{ title: '三、类加载器分类', id: 'kind', level: 3, text: '启动类加载器、扩展类加载器、应用程序类加载器' },
131+
{ title: '4. 好处与代价', id: 'cost', level: 4, text: '自定义类加载器的好处是可以隔离不同版本的类' },
132+
] },
133+
] };
134+
const parentIds = search(parent, '双亲委派机制的好处').map(r => r.id);
135+
assert.ok(parentIds.indexOf('good') !== -1 && parentIds.indexOf('good') < parentIds.indexOf('cost'), '小节标题只写“好处”时,按上级标题也能搜到');
125136
const guide = { documents: [
126137
{ title: '学习路线 - 示例', path: '/notes/学习路线 - 示例', sections: [{ title: '第 25 天:B+ 树与索引的类型', id: 'day', text: '自测:MySQL 的 B+Tree 为什么适合范围查询?' }] },
127138
{ title: 'MySQL', path: '/notes/MySQL', sections: [

‎website/builder/test_site_assets.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def test_headings_in_code_do_not_change_real_anchors(self):
4242
sections = [s for s in doc['sections'] if s['id'] == 'tcp-拥塞控制']
4343
self.assertEqual(len(sections), 1)
4444
self.assertEqual(sections[0]['text'], '慢开始')
45+
self.assertEqual(sections[0]['level'], 2)
4546

4647
def test_comments_and_example_asset_urls_are_not_published(self):
4748
content = '<!-- <img src="/secret.png"> -->\n```html\n<img src="/example.png">\n```\n<img src="/notes/pics/actual.png">\n[正文](/notes/示例.md)\n'

‎website/templates/search.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212
if (prepared) return prepared;
1313
prepared = [];
1414
index.documents.forEach(function (doc) {
15-
var page = normalize(doc.title);
15+
var page = normalize(doc.title), parents = [];
1616
doc.sections.forEach(function (section, i) {
17+
// 上级小节的标题:“双亲委派模型”下的“2. 好处”,搜“双亲委派的好处”时要能找到
18+
while (parents.length && parents[parents.length - 1].level >= section.level) parents.pop();
19+
var parent = parents.length ? parents[parents.length - 1].heading : '';
20+
if (section.level > 1) parents.push({ level: section.level, heading: normalize(section.title) });
1721
// 文章开头没有正文的部分与一级标题重复,不单独作为结果
1822
if (i === 0 && !section.text && doc.sections.length > 1 && normalize(doc.sections[1].title) === page) return;
1923
var heading = normalize(section.title);
2024
var body = normalize(section.text);
2125
// 学习路线只是阅读导读,附录是实验过程,排在讲解正文之后
2226
var penalty = (/^学习路线/.test(doc.title) ? 45 : 0) + (/^a\d+ /.test(heading) ? 40 : 0) + (/ - 进阶$/.test(doc.title) ? 8 : 0) + (heading === '参考资料' ? 30 : 0);
2327
var bare = heading.replace(/^(?:[一二三四五六七八九十]+、|[a-z]?\d+(?:\.\d+)*\.?\s)\s*/, '');
24-
prepared.push({ page: page, heading: heading, body: body, text: heading + ' ' + page + ' ' + body,
28+
prepared.push({ page: page, heading: heading, parent: parent, body: body, text: heading + ' ' + parent + ' ' + page + ' ' + body,
2529
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 });
2630
});
2731
});
@@ -207,7 +211,7 @@
207211
got += weights[i];
208212
var head = entry.heading.includes(hit);
209213
if (head) inHead++;
210-
value += weights[i] * ((head ? 3 : 0) + (entry.page.includes(hit) ? 1 : 0) + Math.min(occurrences(entry.body, hit), 3) * .4);
214+
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);
211215
});
212216
// 门槛按面经问题的开发集选定:命中的信息量不到 15% 的不算,标题中一个都没有命中的要到 40%
213217
if (got < total * .15 || (!inHead && got < total * .4)) return;

0 commit comments

Comments
 (0)