Changing search engine

This commit is contained in:
Fabian Stamm 2020-01-20 23:03:31 +01:00
parent 94b27f9ee4
commit ff968f62a8
3 changed files with 20 additions and 31 deletions

16
package-lock.json generated
View File

@ -1038,12 +1038,6 @@
"physical-cpu-count": "^2.0.0" "physical-cpu-count": "^2.0.0"
} }
}, },
"@types/elasticlunr": {
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/@types/elasticlunr/-/elasticlunr-0.9.0.tgz",
"integrity": "sha512-7xUGaa0HqDmfawyFd8ANaoTHt5KF/BEdvKfqz4NxGEhbloIXGGtH065MOvC+YYQRZPAA/b5Vt/Xe5AmXoKTmhQ==",
"dev": true
},
"@types/navigo": { "@types/navigo": {
"version": "7.0.1", "version": "7.0.1",
"resolved": "https://registry.npmjs.org/@types/navigo/-/navigo-7.0.1.tgz", "resolved": "https://registry.npmjs.org/@types/navigo/-/navigo-7.0.1.tgz",
@ -2738,11 +2732,6 @@
"integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=", "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=",
"dev": true "dev": true
}, },
"elasticlunr": {
"version": "0.9.5",
"resolved": "https://registry.npmjs.org/elasticlunr/-/elasticlunr-0.9.5.tgz",
"integrity": "sha1-ZVQbswnd3Qz5Ty0ciGGyvmUbsNU="
},
"electron-to-chromium": { "electron-to-chromium": {
"version": "1.3.314", "version": "1.3.314",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.314.tgz", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.314.tgz",
@ -3775,6 +3764,11 @@
"integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==",
"dev": true "dev": true
}, },
"fuse.js": {
"version": "3.4.6",
"resolved": "https://registry.npmjs.org/fuse.js/-/fuse.js-3.4.6.tgz",
"integrity": "sha512-H6aJY4UpLFwxj1+5nAvufom5b2BT2v45P1MkPvdGIK8fWjQx/7o6tTT1+ALV0yawQvbmvCF0ufl2et8eJ7v7Cg=="
},
"gauge": { "gauge": {
"version": "2.7.4", "version": "2.7.4",
"resolved": "https://registry.npmjs.org/gauge/-/gauge-2.7.4.tgz", "resolved": "https://registry.npmjs.org/gauge/-/gauge-2.7.4.tgz",

View File

@ -11,8 +11,8 @@
"dependencies": { "dependencies": {
"@hibas123/theme": "^1.2.14", "@hibas123/theme": "^1.2.14",
"@hibas123/utils": "^2.2.3", "@hibas123/utils": "^2.2.3",
"elasticlunr": "^0.9.5",
"feather-icons": "^4.24.1", "feather-icons": "^4.24.1",
"fuse.js": "^3.4.6",
"navigo": "^7.1.2", "navigo": "^7.1.2",
"svelte": "^3.16.0", "svelte": "^3.16.0",
"svelte-key": "^1.0.0", "svelte-key": "^1.0.0",
@ -20,7 +20,6 @@
"uuid": "^3.3.3" "uuid": "^3.3.3"
}, },
"devDependencies": { "devDependencies": {
"@types/elasticlunr": "^0.9.0",
"@types/navigo": "^7.0.1", "@types/navigo": "^7.0.1",
"@types/node": "^12.12.14", "@types/node": "^12.12.14",
"@types/uuid": "^3.4.6", "@types/uuid": "^3.4.6",

View File

@ -26,9 +26,7 @@ console.log("Running exam:", dataVersion);
const runsShould = 3; const runsShould = 3;
import elasticlunr from "elasticlunr"; import Fuse from "fuse.js";
console.log(elasticlunr);
class QuestionManager { class QuestionManager {
version: string = dataVersion; version: string = dataVersion;
@ -47,7 +45,7 @@ class QuestionManager {
level: number level: number
}[]; }[];
index: elasticlunr.Index<{ id: string, title: string, answers: string }>; index: Fuse<any, any>;
private getAnswerString(question: Question) { private getAnswerString(question: Question) {
switch (question.type) { switch (question.type) {
@ -73,30 +71,28 @@ class QuestionManager {
this.getNewActive(); this.getNewActive();
this.index = elasticlunr(function () { const searchData = Data.questions
this.addField("title")
this.addField("answers");
this.setRef("id")
})
Data.questions
.map(e => ({ .map(e => ({
id: e.id, id: e.id,
title: e.title, title: e.title,
answers: this.getAnswerString(e) answers: this.getAnswerString(e)
})) }))
.forEach(e => this.index.addDoc(e));
this.index = new Fuse(searchData, {
keys: [{
name: "title",
weight: 0.8
}, {
name: "answers",
weight: 0.2
}]
})
} }
search(term: string) { search(term: string) {
if (term === "") if (term === "")
return Data.questions; return Data.questions;
const match = this.index.search(term, { const match = (this.index.search(term) as any[]).map(e => QuestionMap.get(e.id));
fields: {
title: { boost: 2 },
answers: { boost: 1 }
}
}).map(e => QuestionMap.get(e.ref));
return match; return match;
} }