FlexSearch Document index add and search: working versions (tested 2026-10-15)
Build a FlexSearch Document index over title/content fields and search with enrichment offline.
passed2 passed · 0 failed2026-10-15 20:57 UTC- Status
- passed
- Last passed
- 2026-10-15 20:57 UTC
- Tests
- 2 / 2
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions4
flexsearch@0.8.212@types/flexsearch@0.7.42@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 38805962781
The code
Exactly the files that passed. Your agent can pull them with get_recipe("flexsearch-document-index") (install), or see them on GitHub.
package.json
{
"name": "flexsearch-document-index",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"flexsearch": "0.8.212"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15",
"@types/flexsearch": "0.7.42"
}
}
src/index-docs.ts
import { Document } from "flexsearch";
export type Article = { id: number; title: string; content: string };
export function createArticleIndex() {
return new Document<Article>({
document: {
id: "id",
index: ["title", "content"],
store: true,
},
});
}
export function addArticles(index: Document<Article>, articles: Article[]) {
for (const a of articles) index.add(a);
}
export function searchArticles(index: Document<Article>, query: string) {
const raw = index.search(query, { enrich: true, limit: 10 }) as Array<{
field: string;
result: Array<{ id: number; doc: Article }>;
}>;
const ids = new Set<number>();
const docs: Article[] = [];
for (const field of raw) {
for (const hit of field.result ?? []) {
if (!ids.has(hit.id)) {
ids.add(hit.id);
docs.push(hit.doc);
}
}
}
return docs;
}
test/index-docs.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { addArticles, createArticleIndex, searchArticles } from "../src/index-docs.ts";
test("indexes documents and returns enriched hits", () => {
const index = createArticleIndex();
addArticles(index, [
{ id: 1, title: "TypeScript tips", content: "Use strict null checks" },
{ id: 2, title: "Rust ownership", content: "Borrow checker basics" },
]);
const hits = searchArticles(index, "TypeScript");
assert.ok(hits.some((h) => h.id === 1));
assert.ok(hits.every((h) => typeof h.title === "string"));
});
test("content field is searchable", () => {
const index = createArticleIndex();
addArticles(index, [
{ id: 10, title: "Guide", content: "borrow checker basics for beginners" },
]);
const hits = searchArticles(index, "borrow");
assert.equal(hits.length, 1);
assert.equal(hits[0].id, 10);
});
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.