Skip to content
AgenticAIWiki
  1. Learn
  2. Part 4
  3. 22. Vector database internals
  4. 22.4

Lesson 22.4 · 4 of 8 · 4 min read · Reviewed 24 Nov 2026

Filtering, hybrid search and metadata

By the end, you can

  • Combine vector search with metadata filters.
  • Explain hybrid search inside a vector store.

Metadata on every chunk

{"id": "pol-4.2", "tenant": "acme", "doc_type": "policy", "lang": "en",
 "valid_from": "2026-01-01", "valid_to": null, "text": "...", "vector": [...]}

Pre-, in- and post-filtering

Approach How Risk
Post-filter Find top k by similarity, then drop non-matching Can leave few or no results
Pre-filter Restrict candidates first, then search Can be slow if the filter is very broad
In-search filter Index skips non-matching nodes while searching Best of both where supported

For access control (tenant, user), the filter must always apply. Never rely on the model to ignore other tenants’ text.

Vectors capture meaning; keywords capture exact terms like product codes and names. Many vector storesVector database: A database built to store embeddings and find the nearest ones to a query quickly. can combine a BM25BM25: A classic keyword ranking function that scores documents by term frequency and rarity. keyword score with vector similarity:

score = a x vector_similarity + (1 - a) x keyword_score

or merge two ranked lists with reciprocal rank fusion (23.2).

Freshness

Add validity dates or versions to chunksChunking: Splitting documents into smaller passages before embedding and indexing them. and filter on them, so retired policies don’t come back. Re-index changed documents promptly.

Key points

  • Real queries need filters: only this tenant's documents, only current policies, only English.
  • Filtering before or during search keeps results correct; filtering after can leave too few results.
  • Many stores also support hybrid search: keyword (BM25) scores combined with vector similarity.
Hands-on labRuns in the Playground

Compare post-filtering with pre-filtering

See why filtering after top-k can return nothing, and fix it by filtering before the search.

You will have: A script that runs the same tenant query with post-filtering and pre-filtering and prints both result lists.

Steps

  1. Read the chunks: most of the refund-related ones belong to other tenants.
  2. Write post_filter(): take the top 3 by similarity, then drop chunks from other tenants.
  3. Write pre_filter(): keep only the tenant's chunks first, then take the top 3.
  4. Run both for tenant acme and compare.

Done when

  • Post-filtering returns 0 results for acme.
  • Pre-filtering returns 3 acme results, best first.

Open in the Playground No API key needed.

Starter code (Python)
CHUNKS = [  # (id, tenant, similarity to the query "refund rules"), illustrative scores
    ("g-1", "globex", 0.91), ("g-2", "globex", 0.89), ("i-1", "initech", 0.88),
    ("a-1", "acme", 0.74), ("g-3", "globex", 0.72), ("a-2", "acme", 0.70), ("a-3", "acme", 0.55), ("i-2", "initech", 0.52),
]

def post_filter(tenant, k=3):
    # TODO: top k by similarity across all chunks, then keep only this tenant's
    return []

def pre_filter(tenant, k=3):
    # TODO: keep only this tenant's chunks, then take the top k by similarity
    return []

print("post-filter:", post_filter("acme"))
print("pre-filter:", pre_filter("acme"))
Show a solution
CHUNKS = [  # (id, tenant, similarity to the query "refund rules"), illustrative scores
    ("g-1", "globex", 0.91), ("g-2", "globex", 0.89), ("i-1", "initech", 0.88),
    ("a-1", "acme", 0.74), ("g-3", "globex", 0.72), ("a-2", "acme", 0.70), ("a-3", "acme", 0.55), ("i-2", "initech", 0.52),
]

def top(chunks, k):
    return sorted(chunks, key=lambda c: -c[2])[:k]

def post_filter(tenant, k=3):
    return [c[0] for c in top(CHUNKS, k) if c[1] == tenant]

def pre_filter(tenant, k=3):
    return [c[0] for c in top([c for c in CHUNKS if c[1] == tenant], k)]

post, pre = post_filter("acme"), pre_filter("acme")
print(f"post-filter: {len(post)} results {post}")
print(f"pre-filter: {len(pre)} results {pre}")
Quick checkA query filters to one tenant, but post-filtering the top 10 vector hits leaves zero results. What's the fix?
How sure are you?
Pick an answer and how sure you are