Build a Text File Search Index and Query CLI
A tool that walks a directory, builds an inverted index of words to documents, persists the index to disk, and answers ranked keyword queries from the command line.
Project goal
Build a CLI that indexes the words of every text file under a directory into an inverted index, persists it, and answers queries with ranked document matches using term frequency.
Final features
- Tokenise text into lowercase words and build an inverted index.
- Persist the index to disk and reload it for later queries.
- Rank documents by term frequency and multi-term overlap.
- Report the best matching paths with a score per query.
- Skip binary files and common stop words during indexing.
Architecture
A tokenizer normalises text; an indexer maps terms to document postings with counts; a scorer ranks documents for a query; a persistence module serialises the index; and a CLI ties index and query subcommands together. Tokenisation and scoring are pure and heavily tested.
Prerequisites
- Node.js 20 LTS or later
- Familiarity with maps and basic information-retrieval ideas
- Comfort reading files in text mode
Dependencies
- node >=20 — File traversal, persistence, and the CLI entry point
- vitest ^2.1.0 — Tokenizer, indexer, and scorer tests
Directory structure
search-index/
├── src/
│ ├── tokenize.js
│ ├── indexer.js
│ ├── scorer.js
│ ├── persist.js
│ └── cli.js
├── test/
│ ├── indexer.test.js
│ └── scorer.test.js
└── package.json
Phases
1. Tokenizer
Split text into lowercase word tokens, dropping punctuation, pure numbers, and stop words.
Files: src/tokenize.js
const STOP = new Set(['the', 'a', 'an', 'and', 'or', 'of', 'to', 'in', 'is', 'it']);
export function tokenize(text) {
return text
.toLowerCase()
.split(/[^a-z0-9]+/)
.filter((w) => w.length > 1 && !STOP.has(w) && !/^\d+$/.test(w));
}Expected behavior: Punctuation and casing are normalised, stop words are removed, and short tokens and pure numbers are dropped.
2. Inverted index builder
Walk files, tokenise each, and record term to document postings with counts.
Files: src/indexer.js
import { readdir, readFile } from 'node:fs/promises';
import { join, relative } from 'node:path';
import { tokenize } from './tokenize.js';
export async function buildIndex(root) {
const index = new Map();
async function walk(dir) {
for (const entry of await readdir(dir, { withFileTypes: true })) {
const full = join(dir, entry.name);
if (entry.isDirectory()) await walk(full);
else if (/\.(txt|md|js|ts)$/.test(entry.name)) {
const text = await readFile(full, 'utf8');
const rel = relative(root, full).replace(/\\/g, '/');
const counts = new Map();
for (const w of tokenize(text)) counts.set(w, (counts.get(w) ?? 0) + 1);
for (const [w, n] of counts) {
if (!index.has(w)) index.set(w, new Map());
index.get(w).set(rel, n);
}
}
}
}
await walk(root);
return index;
}Expected behavior: A word appearing twice in one file records a count of two, and a word in two files has two postings with correct counts.
3. Query scorer
Score documents by summing term frequencies across query terms and sort by score then path.
Files: src/scorer.js
export function query(index, terms) {
const scores = new Map();
for (const term of terms) {
const postings = index.get(term);
if (!postings) continue;
for (const [doc, count] of postings) {
scores.set(doc, (scores.get(doc) ?? 0) + count);
}
}
return [...scores.entries()]
.map(([path, score]) => ({ path, score }))
.sort((a, b) => b.score - a.score || a.path.localeCompare(b.path));
}Expected behavior: A document matching two terms outranks one matching a single term, and a query with no matching terms returns an empty list.
4. Index persistence
Serialise the nested maps to JSON and reload them, reconstructing maps on read.
Files: src/persist.js
import { readFile, writeFile } from 'node:fs/promises';
export function serialise(index) {
const out = {};
for (const [term, postings] of index) out[term] = Object.fromEntries(postings);
return JSON.stringify(out);
}
export async function loadIndex(path) {
const raw = JSON.parse(await readFile(path, 'utf8'));
const index = new Map();
for (const [term, postings] of Object.entries(raw)) index.set(term, new Map(Object.entries(postings)));
return index;
}
export async function saveIndex(path, index) {
await writeFile(path, serialise(index));
}Expected behavior: Saving then loading an index yields an equal structure, and querying the loaded index returns the same ranking as the in-memory one.
5. CLI subcommands
Provide index and query subcommands, default the index path, and print ranked results.
Files: src/cli.js
import { buildIndex } from './indexer.js';
import { query } from './scorer.js';
import { loadIndex, saveIndex } from './persist.js';
import { tokenize } from './tokenize.js';
const [sub, target, ...rest] = process.argv.slice(2);
const indexPath = process.env.INDEX_FILE ?? './index.json';
if (sub === 'index') {
const index = await buildIndex(target ?? '.');
await saveIndex(indexPath, index);
console.log('indexed ' + index.size + ' terms');
} else if (sub === 'query') {
const index = await loadIndex(indexPath);
for (const hit of query(index, tokenize((target ?? '') + ' ' + rest.join(' ')))) {
console.log(hit.score + ' ' + hit.path);
}
} else {
console.error('usage: search-index <index|query> <dir|terms>');
process.exit(1);
}Expected behavior: Index prints a term count and writes the file, and query prints one score-prefixed path per hit in descending order.
6. Tests and hardening
Cover tokenising, indexing, scoring, and persistence round-trips, then fix any failure.
Files: test/scorer.test.js
import { query } from '../src/scorer.js';
import { indexDocument } from '../src/indexer.js';
function makeIndex() {
const index = new Map();
indexDocument(index, 'a.txt', 'apple banana banana');
indexDocument(index, 'b.txt', 'banana cherry');
return index;
}
test('two-term overlap outranks one-term match', () => {
const results = query(makeIndex(), ['apple', 'banana']);
expect(results[0].path).toBe('a.txt');
expect(results.map((r) => r.path)).toContain('b.txt');
});Expected behavior: The scorer suite passes, an empty query returns no results, and a persisted index answers identically to the in-memory build.
Testing
Run npx vitest. Tokenizer tests cover punctuation, casing, numbers, and stop words. Indexer tests assert posting counts for repeated words and across multiple documents. Scorer tests assert that a document containing two query terms outranks one containing a single term, and that an absent term yields no results.
Troubleshooting
The index file grows huge for a large corpus
Store posting counts as pairs and consider dropping terms that appear in a single very large document; a plain JSON index is fine for small corpora but should be gzipped or moved to a keyed store as it grows.
Searching is case-sensitive
Lowercase both the indexed text and the query terms in one tokenizer used by both paths; a shared tokenizer is what keeps indexing and querying consistent.
Reloaded index loses document counts
Reconstruct nested maps on load rather than leaving plain objects; the scorer calls map methods, so objects with the same shape are not interchangeable.
Deployment
Run the index step in CI to refresh a committed index, then serve queries from a static host or a small search endpoint. The persisted JSON index can be shipped with the site for client-side search.
Known limitations
- Ranking uses raw term frequency and ignores document length and rarity, so a long document can outrank a focused one.
- There is no stemming, so searching for a plural does not match the singular form.
Project overview
Search is a small problem with a clear right answer: build an index once, then answer queries by intersecting postings rather than rescanning every file. This project builds that index and explains the ranking.
The index maps each word to the documents containing it with a count, persists as JSON, and answers queries by scoring overlapping terms. Stop words are dropped so common words do not dominate the results.
The indexer is a single pass that records, for each term, which documents contain it and how often:
export function indexDocument(index, path, text) {
const counts = new Map();
for (const word of tokenize(text)) counts.set(word, (counts.get(word) ?? 0) + 1);
for (const [word, count] of counts) {
if (!index.has(word)) index.set(word, new Map());
index.get(word).set(path, count);
}
return index;
}
Because the posting map stores a per-document count, ranking never needs the original text again, which is what makes the persisted index sufficient to answer queries.
Setup
Zero runtime dependencies keeps the retrieval logic the only interesting thing in the codebase.
| Dependency | Version | Why it is here |
|---|---|---|
| node | >=20 | File traversal, persistence, and the CLI entry point |
| vitest | ^2.1.0 | Tokenizer, indexer, and scorer tests |
Create the project and install:
mkdir search-index && cd search-index
npm init -y
npm install -D vitest@^2.1.0
Configure the index and query entry points plus the tests:
{
"name": "search-index",
"type": "module",
"scripts": {
"index": "node src/cli.js index",
"query": "node src/cli.js query",
"test": "vitest run"
}
}
Keep a fixtures directory of three or four small text files so indexer and scorer tests run against realistic content offline.
Phase 1: Tokenizer
Split text into lowercase word tokens, dropping punctuation, pure numbers, and stop words. Each phase produces something demonstrable, so the tutorial never asks the reader to hold an unfinished design in their head.
const STOP = new Set(['the', 'a', 'an', 'and', 'or', 'of', 'to', 'in', 'is', 'it']);
export function tokenize(text) {
return text
.toLowerCase()
.split(/[^a-z0-9]+/)
.filter((w) => w.length > 1 && !STOP.has(w) && !/^\d+$/.test(w));
}
Acceptance for this phase: Punctuation and casing are normalised, stop words are removed, and short tokens and pure numbers are dropped.
Phase 2: Inverted index builder
Walk files, tokenise each, and record term to document postings with counts. Each phase produces something demonstrable, so the tutorial never asks the reader to hold an unfinished design in their head.
import { readdir, readFile } from 'node:fs/promises';
import { join, relative } from 'node:path';
import { tokenize } from './tokenize.js';
export async function buildIndex(root) {
const index = new Map();
async function walk(dir) {
for (const entry of await readdir(dir, { withFileTypes: true })) {
const full = join(dir, entry.name);
if (entry.isDirectory()) await walk(full);
else if (/\.(txt|md|js|ts)$/.test(entry.name)) {
const text = await readFile(full, 'utf8');
const rel = relative(root, full).replace(/\\/g, '/');
const counts = new Map();
for (const w of tokenize(text)) counts.set(w, (counts.get(w) ?? 0) + 1);
for (const [w, n] of counts) {
if (!index.has(w)) index.set(w, new Map());
index.get(w).set(rel, n);
}
}
}
}
await walk(root);
return index;
}
Acceptance for this phase: A word appearing twice in one file records a count of two, and a word in two files has two postings with correct counts.
Phase 3: Query scorer
Score documents by summing term frequencies across query terms and sort by score then path. Each phase produces something demonstrable, so the tutorial never asks the reader to hold an unfinished design in their head.
export function query(index, terms) {
const scores = new Map();
for (const term of terms) {
const postings = index.get(term);
if (!postings) continue;
for (const [doc, count] of postings) {
scores.set(doc, (scores.get(doc) ?? 0) + count);
}
}
return [...scores.entries()]
.map(([path, score]) => ({ path, score }))
.sort((a, b) => b.score - a.score || a.path.localeCompare(b.path));
}
Acceptance for this phase: A document matching two terms outranks one matching a single term, and a query with no matching terms returns an empty list.
Phase 4: Index persistence
Serialise the nested maps to JSON and reload them, reconstructing maps on read. Each phase produces something demonstrable, so the tutorial never asks the reader to hold an unfinished design in their head.
import { readFile, writeFile } from 'node:fs/promises';
export function serialise(index) {
const out = {};
for (const [term, postings] of index) out[term] = Object.fromEntries(postings);
return JSON.stringify(out);
}
export async function loadIndex(path) {
const raw = JSON.parse(await readFile(path, 'utf8'));
const index = new Map();
for (const [term, postings] of Object.entries(raw)) index.set(term, new Map(Object.entries(postings)));
return index;
}
export async function saveIndex(path, index) {
await writeFile(path, serialise(index));
}
Acceptance for this phase: Saving then loading an index yields an equal structure, and querying the loaded index returns the same ranking as the in-memory one.
Phase 5: CLI subcommands
Provide index and query subcommands, default the index path, and print ranked results. Each phase produces something demonstrable, so the tutorial never asks the reader to hold an unfinished design in their head.
import { buildIndex } from './indexer.js';
import { query } from './scorer.js';
import { loadIndex, saveIndex } from './persist.js';
import { tokenize } from './tokenize.js';
const [sub, target, ...rest] = process.argv.slice(2);
const indexPath = process.env.INDEX_FILE ?? './index.json';
if (sub === 'index') {
const index = await buildIndex(target ?? '.');
await saveIndex(indexPath, index);
console.log('indexed ' + index.size + ' terms');
} else if (sub === 'query') {
const index = await loadIndex(indexPath);
for (const hit of query(index, tokenize((target ?? '') + ' ' + rest.join(' ')))) {
console.log(hit.score + ' ' + hit.path);
}
} else {
console.error('usage: search-index <index|query> <dir|terms>');
process.exit(1);
}
Acceptance for this phase: Index prints a term count and writes the file, and query prints one score-prefixed path per hit in descending order.
Phase 6: Tests and hardening
Cover tokenising, indexing, scoring, and persistence round-trips, then fix any failure. Each phase produces something demonstrable, so the tutorial never asks the reader to hold an unfinished design in their head.
import { query } from '../src/scorer.js';
import { indexDocument } from '../src/indexer.js';
function makeIndex() {
const index = new Map();
indexDocument(index, 'a.txt', 'apple banana banana');
indexDocument(index, 'b.txt', 'banana cherry');
return index;
}
test('two-term overlap outranks one-term match', () => {
const results = query(makeIndex(), ['apple', 'banana']);
expect(results[0].path).toBe('a.txt');
expect(results.map((r) => r.path)).toContain('b.txt');
});
Acceptance for this phase: The scorer suite passes, an empty query returns no results, and a persisted index answers identically to the in-memory build.
Testing strategy
Run npx vitest. Tokenizer tests cover punctuation, casing, numbers, and stop words. Indexer tests assert posting counts for repeated words and across multiple documents. Scorer tests assert that a document containing two query terms outranks one containing a single term, and that an absent term yields no results.
The ranking test is the meaningful one: it pins the ordering that makes search feel useful rather than merely correct, and it fails loudly if the scoring formula drifts.
Extending the project
Stemming, phrase queries, and a persisted term-document frequency table for TF-IDF are each an addition to one module. The index format already carries counts, so richer ranking is a scorer change rather than an indexer change.