# prajexec/searchcraft architecture

> Architecture diagram of the GitHub repository prajexec/searchcraft, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/prajexec/searchcraft
- Repository: https://github.com/prajexec/searchcraft

## Overview

SearchCraft is a local-first TypeScript search service: clients create named indexes, submit documents or bounded NDJSON batches, and query ranked results. Writes persist source documents and durable jobs in SQLite; a worker applies changes to custom indexes and immutable segments, while search uses BM25, filters, phrase matching, and cursor pagination. The documented product also includes reindexing, compaction, snapshots, job inspection, and operational endpoints. Edges reflect README statements or shown source references/calls; some maintenance wiring is documented but not shown in sampled implementation.

Main entry points: [`src/app.ts`](https://github.com/prajexec/searchcraft/blob/main/src/app.ts) (HTTP routes), [`src/indexes/index-manager.ts`](https://github.com/prajexec/searchcraft/blob/main/src/indexes/index-manager.ts) (Index manager), [`src/workers/index-worker.ts`](https://github.com/prajexec/searchcraft/blob/main/src/workers/index-worker.ts) (Index worker), [`src/indexing/inverted-index.ts`](https://github.com/prajexec/searchcraft/blob/main/src/indexing/inverted-index.ts) (Inverted index)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["HTTP API"]
  node_http["HTTP routes<br/>[app.ts]"]
  node_bulk["Bulk parser<br/>[bulk.ts]"]
  node_api_spec["OpenAPI contract<br/>[openapi.ts]"]
end

subgraph group_persist["Durable State"]
  node_source_docs[("Source documents<br/>[documents.ts]")]
  node_jobs[("Indexing jobs<br/>[jobs.ts]")]
  node_sqlite[("SQLite database<br/>[sqlite.ts]")]
end

subgraph group_engine["Search Engine"]
  node_manager["Index manager<br/>[index-manager.ts]"]
  node_index_config["Index configuration<br/>[index-config.ts]"]
  node_worker["Index worker<br/>[index-worker.ts]"]
  node_inverted["Inverted index<br/>[inverted-index.ts]"]
  node_tokenizer["Tokenizer<br/>[tokenizer.ts]"]
  node_bm25["BM25 ranking<br/>[bm25.ts]"]
  node_filters["Search filters<br/>[filters.ts]"]
  node_segments[("Immutable segments<br/>[segment.ts]")]
  node_compression["Position compression<br/>[compression.ts]"]
  node_dictionary["Term dictionary<br/>[term-dictionary.ts]"]
end

subgraph group_ops["Operations"]
  node_config["Runtime config<br/>[config.ts]"]
  node_snapshots["Snapshot archives<br/>[snapshots.ts]"]
  node_logger["Structured logging<br/>[logger.ts]"]
  node_metrics["Metrics<br/>[metrics.ts]"]
end

node_client(("Application client"))
node_external_disk[("Local disk")]

node_client -->|"sends requests"| node_http
node_http -->|"parses batches"| node_bulk
node_http -->|"routes operations"| node_manager
node_http -->|"parses filters"| node_filters
node_http -.->|"serves contract"| node_api_spec
node_http -->|"uses limits"| node_config
node_manager -->|"validates config"| node_index_config
node_manager -->|"mutates documents"| node_source_docs
node_manager -->|"manages jobs"| node_jobs
node_source_docs -.->|"persists source"| node_sqlite
node_jobs -.->|"persists queue"| node_sqlite
node_worker -->|"claims jobs"| node_jobs
node_worker -->|"loads documents"| node_source_docs
node_worker -->|"publishes updates"| node_manager
node_manager -->|"searches index"| node_inverted
node_inverted -.->|"tokenizes text"| node_tokenizer
node_inverted -->|"scores candidates"| node_bm25
node_inverted -->|"filters matches"| node_filters
node_inverted -->|"encodes positions"| node_compression
node_segments -->|"decodes positions"| node_compression
node_segments -->|"validates blocks"| node_dictionary
node_manager -->|"loads generations"| node_segments
node_manager -->|"stores index data"| node_external_disk
node_snapshots -->|"archives index data"| node_segments
node_snapshots -.->|"stores archives"| node_external_disk
node_http -.->|"reports metrics"| node_metrics
node_worker -->|"records outcomes"| node_metrics
node_worker -->|"logs events"| node_logger
node_http -.->|"logs requests"| node_logger
node_http -->|"returns results"| node_client

click node_http "https://github.com/prajexec/searchcraft/blob/main/src/app.ts"
click node_bulk "https://github.com/prajexec/searchcraft/blob/main/src/http/bulk.ts"
click node_api_spec "https://github.com/prajexec/searchcraft/blob/main/src/http/openapi.ts"
click node_config "https://github.com/prajexec/searchcraft/blob/main/src/config.ts"
click node_manager "https://github.com/prajexec/searchcraft/blob/main/src/indexes/index-manager.ts"
click node_index_config "https://github.com/prajexec/searchcraft/blob/main/src/indexes/index-config.ts"
click node_source_docs "https://github.com/prajexec/searchcraft/blob/main/src/storage/documents.ts"
click node_jobs "https://github.com/prajexec/searchcraft/blob/main/src/storage/jobs.ts"
click node_sqlite "https://github.com/prajexec/searchcraft/blob/main/src/storage/sqlite.ts"
click node_worker "https://github.com/prajexec/searchcraft/blob/main/src/workers/index-worker.ts"
click node_inverted "https://github.com/prajexec/searchcraft/blob/main/src/indexing/inverted-index.ts"
click node_tokenizer "https://github.com/prajexec/searchcraft/blob/main/src/indexing/tokenizer.ts"
click node_bm25 "https://github.com/prajexec/searchcraft/blob/main/src/query/bm25.ts"
click node_filters "https://github.com/prajexec/searchcraft/blob/main/src/query/filters.ts"
click node_segments "https://github.com/prajexec/searchcraft/blob/main/src/indexing/segment.ts"
click node_compression "https://github.com/prajexec/searchcraft/blob/main/src/indexing/compression.ts"
click node_dictionary "https://github.com/prajexec/searchcraft/blob/main/src/indexing/term-dictionary.ts"
click node_snapshots "https://github.com/prajexec/searchcraft/blob/main/src/storage/snapshots.ts"
click node_logger "https://github.com/prajexec/searchcraft/blob/main/src/observability/logger.ts"
click node_metrics "https://github.com/prajexec/searchcraft/blob/main/src/observability/metrics.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_http,node_bulk,node_api_spec,node_client toneBlue
class node_source_docs,node_jobs,node_sqlite,node_external_disk toneAmber
class node_manager,node_index_config,node_worker,node_inverted,node_tokenizer,node_bm25,node_filters,node_segments,node_compression,node_dictionary toneMint
class node_config,node_snapshots,node_logger,node_metrics toneRose
```

## Components

### HTTP API

- [HTTP routes](https://github.com/prajexec/searchcraft/blob/main/src/app.ts): `src/app.ts`
- [Bulk parser](https://github.com/prajexec/searchcraft/blob/main/src/http/bulk.ts): `src/http/bulk.ts`
- [OpenAPI contract](https://github.com/prajexec/searchcraft/blob/main/src/http/openapi.ts): `src/http/openapi.ts`

### Durable State

- [Source documents](https://github.com/prajexec/searchcraft/blob/main/src/storage/documents.ts): `src/storage/documents.ts`
- [Indexing jobs](https://github.com/prajexec/searchcraft/blob/main/src/storage/jobs.ts): `src/storage/jobs.ts`
- [SQLite database](https://github.com/prajexec/searchcraft/blob/main/src/storage/sqlite.ts): `src/storage/sqlite.ts`

### Search Engine

- [Index manager](https://github.com/prajexec/searchcraft/blob/main/src/indexes/index-manager.ts): `src/indexes/index-manager.ts`
- [Index configuration](https://github.com/prajexec/searchcraft/blob/main/src/indexes/index-config.ts): `src/indexes/index-config.ts`
- [Index worker](https://github.com/prajexec/searchcraft/blob/main/src/workers/index-worker.ts): `src/workers/index-worker.ts`
- [Inverted index](https://github.com/prajexec/searchcraft/blob/main/src/indexing/inverted-index.ts): `src/indexing/inverted-index.ts`
- [Tokenizer](https://github.com/prajexec/searchcraft/blob/main/src/indexing/tokenizer.ts): `src/indexing/tokenizer.ts`
- [BM25 ranking](https://github.com/prajexec/searchcraft/blob/main/src/query/bm25.ts): `src/query/bm25.ts`
- [Search filters](https://github.com/prajexec/searchcraft/blob/main/src/query/filters.ts): `src/query/filters.ts`
- [Immutable segments](https://github.com/prajexec/searchcraft/blob/main/src/indexing/segment.ts): `src/indexing/segment.ts`
- [Position compression](https://github.com/prajexec/searchcraft/blob/main/src/indexing/compression.ts): `src/indexing/compression.ts`
- [Term dictionary](https://github.com/prajexec/searchcraft/blob/main/src/indexing/term-dictionary.ts): `src/indexing/term-dictionary.ts`

### Operations

- [Runtime config](https://github.com/prajexec/searchcraft/blob/main/src/config.ts): `src/config.ts`
- [Snapshot archives](https://github.com/prajexec/searchcraft/blob/main/src/storage/snapshots.ts): `src/storage/snapshots.ts`
- [Structured logging](https://github.com/prajexec/searchcraft/blob/main/src/observability/logger.ts): `src/observability/logger.ts`
- [Metrics](https://github.com/prajexec/searchcraft/blob/main/src/observability/metrics.ts): `src/observability/metrics.ts`

### Other components

- **Application client**
- **Local disk**

## Connections

- Application client → HTTP routes: sends requests (evidence: `README.md`)
- HTTP routes → Bulk parser: parses batches (evidence: `src/app.ts`)
- HTTP routes → Index manager: routes operations (evidence: `src/app.ts`)
- HTTP routes → Search filters: parses filters (evidence: `src/app.ts`)
- HTTP routes → OpenAPI contract: serves contract (evidence: `src/app.ts`)
- HTTP routes → Runtime config: uses limits (evidence: `src/app.ts`)
- Index manager → Index configuration: validates config (evidence: `src/indexes/index-manager.ts`)
- Index manager → Source documents: mutates documents (evidence: `src/indexes/index-manager.ts`)
- Index manager → Indexing jobs: manages jobs (evidence: `src/indexes/index-manager.ts`)
- Source documents → SQLite database: persists source
- Indexing jobs → SQLite database: persists queue
- Index worker → Indexing jobs: claims jobs (evidence: `src/workers/index-worker.ts`)
- Index worker → Source documents: loads documents (evidence: `src/workers/index-worker.ts`)
- Index worker → Index manager: publishes updates (evidence: `src/workers/index-worker.ts`)
- Index manager → Inverted index: searches index (evidence: `src/indexes/index-manager.ts`)
- Inverted index → Tokenizer: tokenizes text (evidence: `src/indexing/inverted-index.ts`)
- Inverted index → BM25 ranking: scores candidates (evidence: `src/indexing/inverted-index.ts`)
- Inverted index → Search filters: filters matches (evidence: `src/indexing/inverted-index.ts`)
- Inverted index → Position compression: encodes positions (evidence: `src/indexing/inverted-index.ts`)
- Immutable segments → Position compression: decodes positions (evidence: `src/indexing/segment.ts`)
- Immutable segments → Term dictionary: validates blocks (evidence: `src/indexing/segment.ts`)
- Index manager → Immutable segments: loads generations (evidence: `src/indexes/index-manager.ts`)
- Index manager → Local disk: stores index data (evidence: `README.md`)
- Snapshot archives → Immutable segments: archives index data (evidence: `src/storage/snapshots.ts`)
- Snapshot archives → Local disk: stores archives
- HTTP routes → Metrics: reports metrics (evidence: `src/app.ts`)
- Index worker → Metrics: records outcomes (evidence: `src/workers/index-worker.ts`)
- Index worker → Structured logging: logs events (evidence: `src/workers/index-worker.ts`)
- HTTP routes → Structured logging: logs requests (evidence: `src/app.ts`)
- HTTP routes → Application client: returns results (evidence: `README.md`)
