From e176adfb9e27dd7abc7d269ff5b9f2c5ab73e177 Mon Sep 17 00:00:00 2001 From: Bartosz Prusinowski Date: Fri, 14 Apr 2023 17:05:13 +0200 Subject: [PATCH] fix: Search test --- app/rdf/query-search-score-utils.ts | 8 +++++--- app/rdf/query-search.spec.ts | 26 +++++++++++++++++++------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app/rdf/query-search-score-utils.ts b/app/rdf/query-search-score-utils.ts index 32ad0452b..c1ae97865 100644 --- a/app/rdf/query-search-score-utils.ts +++ b/app/rdf/query-search-score-utils.ts @@ -7,13 +7,15 @@ export const parseFloatZeroed = (s: string) => { } }; -export const weights: Record = { +export const weights = { name: 5, description: 2, themeName: 1, publisher: 1, creatorLabel: 1, }; +export const langMultiplier = 1.5; +export const exactMatchPoints = weights["name"] * 2; const isStopword = (d: string) => { return d.length < 3 && d.toLowerCase() === d; @@ -53,14 +55,14 @@ export const computeScores = ( // Bonus points for exact match. if (val.includes(query.toLowerCase())) { - score += weight * 2; + score += exactMatchPoints; } } // Cubes with properties in the current language get a bonus, // as generally we expect the user to be interested in those. if (scoreRow["lang"] === lang) { - score *= 1.5; + score *= langMultiplier; } if ( diff --git a/app/rdf/query-search.spec.ts b/app/rdf/query-search.spec.ts index 3b30fc513..3e38cbcd6 100644 --- a/app/rdf/query-search.spec.ts +++ b/app/rdf/query-search.spec.ts @@ -1,4 +1,9 @@ -import { computeScores, weights } from "./query-search-score-utils"; +import { + computeScores, + exactMatchPoints, + langMultiplier, + weights, +} from "./query-search-score-utils"; // jest.mock("rdf-ext", () => ({})); // jest.mock("@rdf-esm/data-model", () => ({})); @@ -11,20 +16,27 @@ jest.mock("@tpluscode/sparql-builder", () => ({})); describe("compute scores", () => { const scores = [ - { cube: "a", name: "national" }, - { cube: "b", name: "national", description: "economy" }, - { cube: "c", creatorLabel: "national" }, - { cube: "d", creatorLabel: "" }, + { lang: "en", cube: "a", name: "national" }, + { lang: "en", cube: "b", name: "national", description: "economy" }, + { lang: "de", cube: "c", creatorLabel: "national" }, + { lang: "de", cube: "d", creatorLabel: "" }, + { lang: "en", cube: "e", name: "National Economy of Switzerland" }, ]; it("should compute weighted score per cube from score rows", () => { const reduced = computeScores(scores, { query: "national economy", identifierName: "cube", + lang: "en", }); - expect(reduced["a"].score).toEqual(weights.name); - expect(reduced["b"].score).toEqual(weights.name + weights.description); + expect(reduced["a"].score).toEqual(weights.name * langMultiplier); + expect(reduced["b"].score).toEqual( + (weights.name + weights.description) * langMultiplier + ); expect(reduced["c"].score).toEqual(weights.creatorLabel); expect(reduced["d"]).toBeUndefined(); + expect(reduced["e"].score).toEqual( + (weights.name * 2 + exactMatchPoints) * langMultiplier + ); }); });