Bench Notes

Adding semantic search to Paperless-ngx with a read-only sidecar

Full-text search is the Paperless feature I use most, but it has the usual keyword-search limit: you have to remember words that are actually in the document. I wanted to search by description — “the letter about the water heater warranty” — and get the right document even if none of those words appear in it. The constraints were the same as for the archive itself: fully local, no document content leaves the house, and no chatbot. Retrieval only.

The design

A small sidecar service runs next to Paperless:

Gotcha 1: a view-only account sees nothing

Paperless permissions are two-layered. A fresh account holding every global “view” permission still sees zero documents, because a document that has an owner is visible only to the owner and to whoever has been granted access to that document. The fix is a group: grant the group view access to the documents themselves, and put the read-only account in it. Nothing in the UI hints at this — an empty document list looks exactly like a broken token, and I spent a while debugging the wrong thing.

Gotcha 2: words are not tokens

The first full sync died asking the runtime for a 24 GB attention buffer. The chunker split text into 450-word pieces, which sounds safe until a document’s text contains almost no spaces: to a word counter it is a few enormous “words”; to the model it is thousands of tokens, and attention memory grows with the square of the token count. The chunker now works on a hard character budget — pieces of at most 900 characters, chunks of at most 1,800 — the embedder runs small batches, and a truncation guard at the embedder door caps every input regardless of what the chunker does.

Related: every chunk is embedded with the document’s title in front of it. A scan whose OCR produced no text at all still gets one chunk — an empty one — so its title is embedded and the document stays findable. That edge case was not hypothetical; several of my scans are exactly that.

Gotcha 3: the model is now a power switch

The embedding runtime’s allocator holds on to its peak memory, so after a full indexing pass the service sat on gigabytes it no longer needed. But I search this archive a few times a month at most. Keeping a 500 MB model resident for that made no sense, so the model became a manual switch in the UI: Load when I want semantic search or a sync, Unload when done. While it is off, syncs still reconcile — documents that would need embedding are counted as “pending” and left for a later, loaded pass — and the exact and regex modes, being pure SQL, keep working. A malloc_trim after unloading hands part of the pool back (measured 3.5 GB down to 2.7 GB after a full re-index); the rest only goes away when the process restarts, which is fine.

Result

It lives on the same mini PC as everything else, idles at about 80 MB with the model off, and answers instantly at this scale — the whole archive is a few dozen documents. The unexpected effect is on filing: I tag even less than before, because I trust search to find things by description. The system’s job was always retrieval; now it is better at it.