MiniSearch full-text index with field boosting and JSON serialization: working versions (tested 2026-10-15)
Build a MiniSearch index over title and body with title boosting, prefix and fuzzy search, stored-field filters, auto-suggest, discard, and JSON save/load.
passed6 passed · 0 failed2026-10-15 20:09 UTC- Status
- passed
- Last passed
- 2026-10-15 20:09 UTC
- Tests
- 6 / 6
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
minisearch@7.2.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("minisearch-fields-boost-serialize") (install), or see them on GitHub.
package.json
{
"name": "minisearch-fields-boost-serialize",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"minisearch": "7.2.0"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/index.ts
import MiniSearch from "minisearch";
export type Doc = { id: number; title: string; body: string; category: string };
export const options = {
fields: ["title", "body"],
storeFields: ["title", "category"],
searchOptions: { boost: { title: 3 }, prefix: true, fuzzy: 0.2 },
};
export function createIndex(docs: Doc[]) {
const ms = new MiniSearch<Doc>(options);
ms.addAll(docs);
return ms;
}
export function find(ms: MiniSearch<Doc>, q: string, category?: string) {
return ms
.search(q, category ? { filter: (r) => r.category === category } : undefined)
.map((r) => ({ id: r.id as number, title: r.title as string, terms: r.terms }));
}
export const save = (ms: MiniSearch<Doc>) => JSON.stringify(ms);
export const load = (json: string) => MiniSearch.loadJSON<Doc>(json, options);
test/index.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { createIndex, find, save, load, type Doc } from "../src/index.ts";
const docs: Doc[] = [
{ id: 1, title: "Confined space entry", body: "Atmospheric testing before entry and a rescue plan.", category: "safety" },
{ id: 2, title: "Rigging basics", body: "Confined loads, slings and hitches.", category: "lifting" },
{ id: 3, title: "Forklift operation", body: "Pre-shift inspection and load charts.", category: "lifting" },
];
test("title boost ranks title match above body match", () => {
const r = find(createIndex(docs), "confined");
assert.deepEqual(r.map((x) => x.id), [1, 2]);
});
test("prefix and fuzzy matching", () => {
const ms = createIndex(docs);
assert.equal(find(ms, "fork")[0].id, 3);
assert.equal(find(ms, "riging")[0].id, 2);
});
test("filter by stored field", () => {
assert.deepEqual(find(createIndex(docs), "load", "lifting").map((x) => x.id).sort(), [2, 3]);
assert.deepEqual(find(createIndex(docs), "confined", "safety").map((x) => x.id), [1]);
});
test("discard removes a document", () => {
const ms = createIndex(docs);
ms.discard(1);
assert.deepEqual(find(ms, "confined").map((x) => x.id), [2]);
});
test("autoSuggest", () => {
const s = createIndex(docs).autoSuggest("insp");
assert.equal(s[0].suggestion, "inspection");
});
test("JSON serialization round-trips", () => {
const ms = createIndex(docs);
const restored = load(save(ms));
assert.deepEqual(find(restored, "rescue plan"), find(ms, "rescue plan"));
assert.equal(restored.documentCount, 3);
});
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.