Skip to content

Commit d92821a

Browse files
committed
feat: 对加粗部分增加权重
1 parent 9e2ba6f commit d92821a

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

api/main.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ app.get('/', function(req, res) {
7474
}
7575
}
7676
},
77+
{
78+
match: {
79+
bold: {
80+
query: keyword,
81+
minimum_should_match: "75%",
82+
boost: 4
83+
}
84+
}
85+
},
7786
{
7887
match: {
7988
standard_content: {

build.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ curl -H'Content-Type: application/json' -XPUT "http://localhost:9200/oiwiki" -d'
4242
"url": {
4343
"type": "text"
4444
},
45+
"bold": {
46+
"type": "text",
47+
"analyzer": "pinyin_analyzer",
48+
"search_analyzer": "pinyin_search_analyzer"
49+
},
4550
"standard_content": {
4651
"type": "text"
4752
}

webhook/index.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,31 +52,38 @@ function getContent(filename, data) {
5252
return ['', '', ''];
5353
}
5454

55-
const h1reg = /^# .+$/gm, h2reg = /^## .+$/gm, authorreg = /author:[^\n]*/gm;
55+
const h1reg = /^# .+$/gm, h2reg = /^## .+$/gm, authorreg = /author:[^\n]*/gm, boldreg = /\*\*(.*?)\*\*|__(.*?)__/g;
5656
const lines = file.split('\n').filter(e => !e.match(authorreg));
57-
let others = lines.filter((e) => !e.match(h1reg) && !e.match(h2reg));
57+
let content = lines.filter((e) => !e.match(h1reg) && !e.match(h2reg));
5858
let title = lines[0] && lines[0].match(h1reg) ?
5959
lines[0].replace('# ', '') : '';
6060
traversalArticle(data['nav'], (key, value) => {
6161
if (value == filename) title = key;
6262
});
6363
const h2 = lines.filter(e => e.match(h2reg)).map(e => e.replace(/^## /, ''));
6464

65-
others = others.map(e => e.replace(/^##+ /, ''));
65+
let bold = "";
66+
let match;
67+
while ((match = boldreg.exec(file)) !== null) {
68+
// match[1] is the content in **bold**, match[2] is the content in __bold__
69+
bold += match[1] || match[2];
70+
}
71+
72+
content = content.map(e => e.replace(/^##+ /, ''));
6673

67-
remark.process(others.join('\n'), (err, file) => {
74+
remark.process(content.join('\n'), (err, file) => {
6875
if (err) {
6976
console.error('Remark processing error:', err);
7077
return;
7178
}
72-
others = String(file)
79+
content = String(file)
7380
.replace('"', "")
7481
.replace("\\n\\n", "\\n");
7582
});
7683

77-
others.replace()
84+
content.replace()
7885

79-
return [title, others, h2.join('\n')];
86+
return [title, content, h2.join('\n'), bold];
8087
}
8188

8289
/**
@@ -91,12 +98,13 @@ function updateContent(modified, removed) {
9198
let ops = [];
9299
modified.forEach((filename) => {
93100
ops.push({ index: { _index: 'oiwiki', _id: filename } });
94-
let [title, article, h2] = getContent(filename, data);
101+
let [title, article, h2, bold] = getContent(filename, data);
95102
ops.push({
96103
title: title,
97104
content: article,
98105
url: '/' + filename.replace('/index.md', '/').replace('.md', '/'),
99106
h2: h2,
107+
bold: bold,
100108
standard_content: article,
101109
});
102110
});

webhook/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ const response = client.search({
4848
}
4949
}
5050
},
51+
{
52+
match: {
53+
bold: {
54+
query: keyword,
55+
minimum_should_match: "75%",
56+
boost: 3
57+
}
58+
}
59+
},
5160
{
5261
match: {
5362
standard_content: {

0 commit comments

Comments
 (0)