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"
}
},
"@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": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/@types/navigo/-/navigo-7.0.1.tgz",
@ -2738,11 +2732,6 @@
"integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=",
"dev": true
},
"elasticlunr": {
"version": "0.9.5",
"resolved": "https://registry.npmjs.org/elasticlunr/-/elasticlunr-0.9.5.tgz",
"integrity": "sha1-ZVQbswnd3Qz5Ty0ciGGyvmUbsNU="
},
"electron-to-chromium": {
"version": "1.3.314",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.314.tgz",
@ -3775,6 +3764,11 @@
"integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==",
"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": {
"version": "2.7.4",
"resolved": "https://registry.npmjs.org/gauge/-/gauge-2.7.4.tgz",

View File

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

View File

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