---
title: "Discussion on \"Building Search for BrainZen: From Embeddings to Hybrid Retrieval\" | Hashnode"
description: "Discussion on \"Building Search for BrainZen: From Embeddings to Hybrid Retrieval\". Learn how I designed BrainZen's search using embeddings, semantic search, lexical search, BM25, and hybrid retrieval with RRF."
url: "https://hashnode.com/posts/building-search-for-brainzen-from-embeddings-to-hybrid-retrieval/6ab19b401a06a536c5a4d52a"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Building Search for BrainZen: From Embeddings to Hybrid Retrieval](https://learningwithyahya.hashnode.dev/building-search-for-brainzen-from-embeddings-to-hybrid-retrieval?utm_source=hashnode&utm_medium=feed)

Learn how I designed BrainZen's search using embeddings, semantic search, lexical search, BM25, and hybrid retrieval with RRF.

Sep 21 · 5 min read · learningwithyahya.hashnode.dev  
Read the article

-   [#semantic-search](https://hashnode.com/tag/semantic-search)
-   [#search](https://hashnode.com/tag/search)
-   [#vector-search](https://hashnode.com/tag/vector-search)
-   [#information-retrieval](https://hashnode.com/tag/information-retrieval)
-   [#ai](https://hashnode.com/tag/ai)
-   [#embeddings](https://hashnode.com/tag/embeddings)
-   [#hybrid-search](https://hashnode.com/tag/hybrid-search)
-   [#nlp](https://hashnode.com/tag/nlp)
-   [#mongodb-atlas](https://hashnode.com/tag/mongodb-atlas)

## Written by

[Mohammed Yahya Ahmed](https://hashnode.com/@yahyaahmed)

@yahyaahmed

  
[View profile](https://hashnode.com/@yahyaahmed)

Full-stack developer | MERN • TypeScript • PostgreSQL • AI | Learning in public

Hi🤗, I'm a software developer focused on building full-stack applications and understanding how things work under the hood. Currently exploring full-stack development, search systems, AI, and backend architecture. I enjoy turning what I learn into real projects and writing about the concepts, problems, and lessons I encounter along the way. This space is where I document my learning journey, experiments, and things I wish I had understood earlier.

## Responses 5

Markdown supported · ⌘↵ to post

[Mihai\_LeanZero](https://hashnode.com/@mihaileanzero)

[Sep 21](https://hashnode.com/posts/building-search-for-brainzen-from-embeddings-to-hybrid-retrieval/6ab19b401a06a536c5a4d52a/comment/6ab1bad00981fcdb5794d273)

The embedding lifecycle point is the one people skip and it bites twice. What you hit is staleness on edit, but the other failure shows up once re-embedding runs async: two edits close together can produce two jobs that finish out of order, so a stale embedding can overwrite the fresh one unless each job is tagged with the note's version or last-modified time and anything arriving out of order gets dropped. On the RRF side, where the metadata or permission filter sits matters just as much - filtering after RRF has already picked the top-K means you can end up serving fewer than K results whenever the top-ranked items get filtered out, since fusion already discarded everything outside that window. Filtering each retrieval path before fusion, or over-fetching before the final cut, avoids that particular way of quietly returning less than you promised.

[Mohammed Yahya Ahmed](https://hashnode.com/@yahyaahmed)

[Sep 22](https://hashnode.com/posts/building-search-for-brainzen-from-embeddings-to-hybrid-retrieval/6ab19b401a06a536c5a4d52a/comment/6ab1bad00981fcdb5794d273#reply-6ab2bb845eb6c6beaf4ee641)

Wow...nice advice especially with tagging and last modified time. I think I understood what you are trying to say.

For the embedding race, am I understanding it correctly that the problem only appears when the re-embedding jobs are allowed to run "independently"? For example, I am generating embedding for content saved by the user. For instance, CV2(content version 2) queues a job for EV2(embedding version 2), then CV3 queues another job for EV3 before EV2 finishes. Since the CV2 job already captured the old content, it doesn't automatically know that the document has since become CV3. So if EV3 finishes first and then EV2 finishes later, EV2 could overwrite the newer embedding unless we associate each job with a version and reject stale results. Is that the idea?

And on filtering, I'm curious about the tradeoff. I completely agree that authorization filters like userId should happen before retrieval so we never search/return another user's data. But for ordinary relevance filters like tags/type, I'm not sure pre-filtering every retrieval path is always better.

For example, if I retrieve 100 candidates from a large corpus and then filter them down to 20, I may lose relevant documents that never made it into those 100 candidates. If I pre-filter first and then retrieve from the eligible 50k documents, it seems like retrieval gets a better candidate pool within the actual search space. On the other hand, over-fetching could also solve the post-filter problem.

So I'm wondering: would you distinguish authorization/data-isolation filters from ordinary metadata/relevance filters here? And in a hybrid system, when would you prefer pre-filtering each retrieval path versus over-fetching and filtering later?

I'm still learning the practical tradeoffs here, so I'd really like to understand how you'd think about it in a production system.

[Mihai\_LeanZero](https://hashnode.com/@mihaileanzero)

[Sep 23](https://hashnode.com/posts/building-search-for-brainzen-from-embeddings-to-hybrid-retrieval/6ab19b401a06a536c5a4d52a/comment/6ab1bad00981fcdb5794d273#reply-6ab44d0aeeddea3457c9b06d)

Yeah, that's exactly it. The failure only shows up because the two jobs are unordered relative to each other, EV2 captured CV2's content but has no way of knowing CV3 exists by the time it writes. Tagging each job with the content version it embedded and rejecting a write once a newer version has already landed closes it without needing the jobs to run in order.

On filtering, I'd draw the line at what happens if the filter is wrong rather than at what kind of filter it is. Getting authorization wrong leaks another user's data, so that has to happen before retrieval regardless of cost. Getting a relevance filter wrong just means a worse ranking, so it's fine to let it happen after retrieval when pre-filtering would shrink your candidate pool too much, which is exactly the top-K problem you described. I'd over-fetch and filter after for anything where the eligible set after the filter is still a meaningful fraction of the corpus, and only move the filter into the retrieval path itself once it's selective enough that post-filtering would regularly starve you below K. If you don't know that ratio yet, over-fetch by two or three times K and see how often the post-filter step still leaves you short, that tells you whether the filter needs to move earlier.

[Mohammed Yahya Ahmed](https://hashnode.com/@yahyaahmed)

[Sep 25](https://hashnode.com/posts/building-search-for-brainzen-from-embeddings-to-hybrid-retrieval/6ab19b401a06a536c5a4d52a/comment/6ab1bad00981fcdb5794d273#reply-6ab6ccac2fdac6a573514d34)

Thanks Man... I'll keep that in mind. BTW I implemented versioning....You can check it as its always good to have second opinion, Thanks

[Ahmet Özel](https://hashnode.com/@ahmetozel)

[Oct 11](https://hashnode.com/posts/building-search-for-brainzen-from-embeddings-to-hybrid-retrieval/6ab19b401a06a536c5a4d52a/comment/6acb5b3455480046f6b89911)

The asynchronous embedding lifecycle needs a rule for completion order as well as content edits. If a note changes twice while two embedding jobs are in flight, the older job may finish last and replace the newer vector even though every job succeeded.

I would attach the note revision to each job and accept its vector only if that revision still matches the current content. A test can deliberately finish the newer job first, then the older one, and assert that search remains tied to the latest revision. That turns stale-vector detection into a property of writes rather than a periodic cleanup hope.
