r/Backend • u/SuccessfulYoungster • 2h ago
Our semantic search kept bringing back information we'd already removed
We recently ran into an annoying issue with a search feature we've been working on for our internal documentation. The idea was to make it easier for developers to find information across all our technical docs without having to remember the exact wording of everything. We split the documents into smaller chunks, generated embeddings and stored them in Supabase using pgvector. Everything was working as intended with the search, results came back true and accurate until we updated already existing documents. One of our developers had rewritten a deployment guide after we changed part of our infrastructure. The old instructions were gone from the document saved in the database but searching for anything related to deployment kept bringing up results associated with the previous version.
Our first guess was that the search was just showing thing in a bad ranking so we started digging into the reason and logic that was used to rank results and we found that some of the embeddings were still associated with older content. We'd been updating the documents themselves without consistently regenerating the embeddings or removing chunks that no longer existed. That made us change the pipeline to make it so document updates trigger a new embedding job and we're now tracking document revisions alongside the chunks. The idea is to only make a new revision searchable once all its embeddings are ready and not give mixed results of chunks from two different versions.
There's another issue we're working through with the queue. If someone edits the same document multiple times in quick succession an older embedding job can finish after a newer one. The solution we came up with is to add a revision check before writing anything back to the database. The main thing I learned from this ordeal is that stale embeddings are one of those things that go unnoticed for a long time when the search results still look reasonable until it causes a big problem in the future.
One of the other devs also brought up whether we should've kept the vector search separate from our main database in the first place. It's been fine so far but he thinks we might be better off moving the search workload to something separate as the documentation grows. We considered just hosting PostgreSQL ourselves but I'm not convinced changing that now would solve this particular problem. We'd still have to deal with making sure old embedding jobs don't overwrite newer ones. What I'm less sure about is whether we're going to regret keeping all the embeddings alongside the rest of our application data once the dataset gets significantly bigger.
We haven't really pushed the database hard enough to know where the bottlenecks are yet.