Fuse.js fuzzy search with weighted keys and a prebuilt index: working versions (tested 2026-10-15)
Fuzzy search a product list with Fuse.js using weighted title/brand/tag keys, match metadata, and a serialized prebuilt index, fully offline.
passed5 passed · 0 failed2026-10-15 20:08 UTC- Status
- passed
- Last passed
- 2026-10-15 20:08 UTC
- Tests
- 5 / 5
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
fuse.js@7.5.0@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38799498972
The code
Exactly the files that passed. Your agent can pull them with get_recipe("fuse-js-weighted-search") (install), or see them on GitHub.
package.json
{
"name": "fuse-js-weighted-search",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"fuse.js": "7.5.0"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/search.ts
import Fuse, { type IFuseOptions } from "fuse.js";
export type Product = { id: string; title: string; brand: string; tags: string[] };
const options: IFuseOptions<Product> = {
keys: [
{ name: "title", weight: 0.7 },
{ name: "brand", weight: 0.2 },
{ name: "tags", weight: 0.1 },
],
includeScore: true,
includeMatches: true,
threshold: 0.4,
ignoreLocation: true,
};
export function buildSearch(items: Product[]) {
// Pre-built index so the same index can be cached or shipped to the client.
const index = Fuse.createIndex(options.keys!, items);
return new Fuse(items, options, index);
}
export function search(fuse: Fuse<Product>, q: string, limit = 5) {
return fuse.search(q, { limit }).map((r) => ({ id: r.item.id, score: r.score!, keys: r.matches?.map((m) => m.key) ?? [] }));
}
export function serializeIndex(fuse: Fuse<Product>) {
return JSON.stringify(fuse.getIndex().toJSON());
}
export function loadSearch(items: Product[], json: string) {
return new Fuse(items, options, Fuse.parseIndex<Product>(JSON.parse(json)));
}
test/search.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { buildSearch, search, serializeIndex, loadSearch, type Product } from "../src/search.ts";
const items: Product[] = [
{ id: "1", title: "Steel toe work boots", brand: "Carhartt", tags: ["safety", "footwear"] },
{ id: "2", title: "Hard hat with visor", brand: "MSA", tags: ["safety", "head"] },
{ id: "3", title: "Insulated gloves", brand: "Steelcase", tags: ["hands", "winter"] },
{ id: "4", title: "Safety glasses", brand: "Uvex", tags: ["eyes"] },
];
test("tolerates typos", () => {
const fuse = buildSearch(items);
const r = search(fuse, "hardhat visr");
assert.equal(r[0].id, "2");
});
test("title weight beats brand and tag weight", () => {
const fuse = buildSearch(items);
// "steel" appears in a title (boots) and a brand (Steelcase); weighted title should rank first
const r = search(fuse, "steel");
assert.equal(r[0].id, "1");
assert.ok(r.some((x) => x.id === "3"));
assert.ok(r[0].score <= r[1].score);
assert.ok(r[0].keys.includes("title"));
});
test("matches array tags", () => {
const fuse = buildSearch(items);
const r = search(fuse, "winter");
assert.equal(r[0].id, "3");
assert.deepEqual(r[0].keys, ["tags"]);
});
test("serialized index round-trips with same results", () => {
const fuse = buildSearch(items);
const restored = loadSearch(items, serializeIndex(fuse));
assert.deepEqual(search(restored, "glasses"), search(fuse, "glasses"));
});
test("unrelated query returns nothing", () => {
assert.deepEqual(search(buildSearch(items), "xylophone"), []);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.