From ff968f62a89dd9203adfadf83f502a404323fa1b Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Mon, 20 Jan 2020 23:03:31 +0100 Subject: [PATCH] Changing search engine --- package-lock.json | 16 +++++----------- package.json | 3 +-- src/stores.ts | 32 ++++++++++++++------------------ 3 files changed, 20 insertions(+), 31 deletions(-) diff --git a/package-lock.json b/package-lock.json index 409afc4..b912bb4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 36fdfcf..cc7b809 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/stores.ts b/src/stores.ts index 7c36f5f..0b1c306 100644 --- a/src/stores.ts +++ b/src/stores.ts @@ -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; 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; }