Skip to content

Commit 6067bc5

Browse files
committed
feat: 对加粗部分增加权重
1 parent b09bec9 commit 6067bc5

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
@@ -78,6 +78,15 @@ app.get("/", function (req, res) {
7878
},
7979
},
8080
},
81+
{
82+
match: {
83+
bold: {
84+
query: keyword,
85+
minimum_should_match: "75%",
86+
boost: 4,
87+
},
88+
},
89+
},
8190
{
8291
match: {
8392
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
@@ -57,9 +57,10 @@ function getContent(filename, data) {
5757

5858
const h1reg = /^# .+$/gm,
5959
h2reg = /^## .+$/gm,
60-
authorreg = /author:[^\n]*/gm;
60+
authorreg = /author:[^\n]*/gm,
61+
boldreg = /\*\*(.*?)\*\*|__(.*?)__/g;
6162
const lines = file.split("\n").filter((e) => !e.match(authorreg));
62-
let others = lines.filter((e) => !e.match(h1reg) && !e.match(h2reg));
63+
let content = lines.filter((e) => !e.match(h1reg) && !e.match(h2reg));
6364
let title =
6465
lines[0] && lines[0].match(h1reg) ? lines[0].replace("# ", "") : "";
6566
traversalArticle(data["nav"], (key, value) => {
@@ -68,20 +69,26 @@ function getContent(filename, data) {
6869
const h2 = lines
6970
.filter((e) => e.match(h2reg))
7071
.map((e) => e.replace(/^## /, ""));
72+
let bold = "";
73+
let match;
74+
while ((match = boldreg.exec(file)) !== null) {
75+
// match[1] is the content in **bold**, match[2] is the content in __bold__
76+
bold += match[1] || match[2];
77+
}
7178

72-
others = others.map((e) => e.replace(/^##+ /, ""));
79+
content = content.map((e) => e.replace(/^##+ /, ""));
7380

74-
remark.process(others.join("\n"), (err, file) => {
81+
remark.process(content.join("\n"), (err, file) => {
7582
if (err) {
7683
console.error("Remark processing error:", err);
7784
return;
7885
}
79-
others = String(file).replace('"', "").replace("\\n\\n", "\\n");
86+
content = String(file).replace('"', "").replace("\\n\\n", "\\n");
8087
});
8188

82-
others.replace();
89+
content.replace();
8390

84-
return [title, others, h2.join("\n")];
91+
return [title, content, h2.join("\n"), bold];
8592
}
8693

8794
/**
@@ -96,12 +103,13 @@ function updateContent(modified, removed) {
96103
let ops = [];
97104
modified.forEach((filename) => {
98105
ops.push({ index: { _index: "oiwiki", _id: filename } });
99-
let [title, article, h2] = getContent(filename, data);
106+
let [title, article, h2, bold] = getContent(filename, data);
100107
ops.push({
101108
title: title,
102109
content: article,
103110
url: "/" + filename.replace("/index.md", "/").replace(".md", "/"),
104111
h2: h2,
112+
bold: bold,
105113
standard_content: article,
106114
});
107115
});

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)