Case Study/AI & Agents/Design, engineering, maintenance

Cordmine Search Engine

Author
Aryan
Client
Open source
Published
2026
Length
7 min · 1438 words

Abstract

Zero-dependency library and CLI that archives Discord channels into SQLite and searches them in 0.2 ms.

Key outcomes

Query latency
0.2 ms
Dependencies
Zero
Index
SQLite FTS5

Discord remembers nothing on purpose

Discord is where a large share of technical communities do their work, and its search is built for scrolling rather than for recall. Results are shallow, jumping across channels is awkward, and anything past the immediate view is effectively gone. The consequence is that decisions, deployment commands and troubleshooting answers get rediscovered from scratch every few months, because the person who asked cannot find the thread where it was already answered.

That is a memory problem, not a chat problem. The messages already exist and the bot already sees them. What is missing is a durable index over what the bot has observed.

The reflex answer is a vector database, and it is the wrong one

The default response to “search the history” is embeddings in a vector store. That answer imports a service to run, an embedding provider dependency, a second store that has to stay in sync with the messages, and a per-message cost that scales with the size of the archive. It also makes the common query path slower than it needs to be, because a semantic lookup is the expensive way to find a literal string.

Most queries against a community archive are literal. A deployment command, a package name, a decision about a launch date, a link someone posted. Those are keyword queries, and an inverted index answers them without any model involvement. So the primary index is SQLite’s FTS5, and embeddings are an optional layer on top rather than the foundation.

FTS5 as the primary index

The vault is a single SQLite file in WAL mode, which lets the ingest path and the query path share it without blocking each other. Messages are written twice over: the raw gateway JSON is preserved for fidelity, and a normalized form is generated for indexing. Attachments, mentions and reply links are captured alongside, so a query can resolve a table that mentions a file, or follow a reply chain back to the message it answered.

The performance numbers are the reason the design is defensible rather than just simpler. On an archive of 120,781 messages, a warm FTS search resolves in roughly 0.2 ms. The cold first query takes 24 to 360 ms because SQLite is warming the page cache from disk. A raw LIKE scan over the same data takes about 17 seconds. The index is the difference between a search that feels instant and one that is unusable in a live bot handler.

Where embeddings actually fit

Semantic search is available but optional, and the two providers supported are OpenAI and a local Ollama instance, which keeps a fully offline deployment possible. Embedding is a backfill job rather than part of ingest: embed() drains a queue in batches, and the CLI exposes the same operation so a vault can be embedded after the fact or rebuilt.

The important detail is what happens when embeddings are not ready. hybridSearch and semanticSearch fall back to plain FTS rather than returning nothing. That was a deliberate choice against a failure mode where an operator adds semantic search, the embedding queue is behind, and queries start returning empty results with no obvious reason. Falling back means the worst case is a less clever answer, not a missing one.

Two front doors, one vault

The project ships as a library and a CLI, which is one implementation surface with two entry points. The library mirrors the discord.js API shape, Client, Events, ChannelType and Collection, so it drops into an existing bot without the handler being rewritten around a new mental model. The CLI is the agent-facing side, with --json on every command. Both read and write the same SQLite file, which is what makes the split safe: a cron job, a script and a running bot cannot disagree about the data, because there is only one copy of it.

Exit codes are stable because scripts depend on them. Zero means success, and the remaining codes distinguish usage errors, authentication problems and database failures, so a scheduler can react without parsing output. Configuration is environment-first, CORDMINE_DB and CORDMINE_GUILDS or the matching flags, which keeps the CLI usable from a shell without a config file.

The runtime dependency count is zero. Every capability comes from Bun and SQLite itself, which means the whole thing installs into an environment with no package resolution step and no supply chain beyond the standard library. It runs on Windows, macOS and Linux, and the archive is a file that can be copied, backed up or handed to a colleague.

There is no telemetry and no cloud component. The data stays on the machine that produced it, because the archive is a private record of a community and sending it somewhere else would change what the tool is.

Keeping the vault current

There are two ingest paths, and they exist because bots differ.

The first is cooperative. The bot already receives messageCreate, so it forwards the event with listenMessage, and updates and deletes are handled the same way. This is one line inside a handler the bot already has, and it means the archive is as current as the bot’s own event stream.

The second is autonomous. live() lets the archive hold the gateway connection itself, archiving messages, channels, threads and members as they change, reopening the connection when it drops and running a periodic repair sync to close gaps left by downtime. Both paths write through the same code, so mixing them does not produce duplicate rows. Duplicate handling is keyed on message ids, which are stable, and a redelivered event is a write that changes nothing.

Staying inside Discord’s rules

An archive of this kind is only legitimate if it uses the official surface. cordmine connects with a normal bot token, never a self-bot user account, and it only archives what the bot is already permitted to see in servers it has been invited to. Backfill uses ordinary pagination and respects the API’s rate limits rather than hammering endpoints for speed.

The honest position is that this is a tool, not a legal opinion. A large or unusual deployment should be reviewed against Discord’s developer terms, and the members of an archived server should be told what is being stored. Building the archive quietly would undermine the reason for having it.

The hard parts, honestly

Cold queries are the counter-intuitive cost. An index is fast once it is in memory, and the first query after a restart can take two orders of magnitude longer than the warm case. That is page-cache warmup, and the practical response is to accept it, keep one copy of the index and let the operating system’s page cache absorb the cost of the first cold query.

Embedding is a backlog, not a step. A vault that has just been synced has no embeddings at all, and the queue drains at whatever rate the provider allows. Treating embedding as a background process with visible progress, instead of a synchronous part of ingest, is what keeps syncing a large server from appearing to hang.

Rich embeds carry a lot of the useful text. Bot and webhook messages were originally excluded by default, which turned out to be wrong: pull request cards, feed bots and game trackers post their content in embed fields rather than in the message body. Those fields, title, description, author, footer, URL and custom fields, are now part of the indexed content, along with poll questions and answers and attachment filenames.

Schema evolution lands on the reader. Adding a searchable field means older rows are missing it until a rebuild. Migrations run when the vault is opened, and the README is explicit that a semantic index built before a content change needs to be regenerated, because the silent failure is subtle: results arrive, they are just worse.

What I would do differently

The normalized row shape should have been fixed earlier. It grew as new searchable content was discovered, and each addition meant deciding what the older rows should look like. Freezing a normalized representation up front, with raw JSON kept alongside, would have made those additions mechanical.

The sql command, a read-only escape hatch that lets an operator run an arbitrary query against the vault, was added after the fact and is now one of the more useful parts of the CLI. It should have been a first-class interface from the start, because there will always be a question the typed API does not express, and the alternative to a read-only escape hatch is exporting the file and opening it somewhere else.

References

  1. [Source repository]github.com/Uo1428/cordmine
  2. [Author]Aryan (Uo1428)

Built with

BunTypeScriptSQLite FTS5Embeddings

Related production systems from the same category.

All case studies →

Want this kind of system on your side of the table?