Area 02 · RAG · Concept 07 of 20

Metadata Filtering

Narrow the search by fields like date, user or document type.

01–10 · The basePhase 2 · Weeks 7–10≈ 45 min

The idea

Not every chunk is a valid answer for every question. A user should only see their own company’s documents; a question about "the 2026 policy" should not get the 2023 version. Metadata filtering restricts the search to chunks whose fields match.

Filters can apply before the vector search (pre-filtering, exact but can be slow on very selective filters with some indexes) or after (post-filtering, fast but may return fewer than k results). In pgvector it is a WHERE clause; check your plan and consider partial indexes for hot filters.

You can also let the LLM extract filters from the question: "refund policy for Germany from last year" becomes country = DE and year = 2025, plus a semantic query "refund policy".

Why it matters

Filtering is both a relevance tool and a security control. Tenant filters are not optional.

Words to know

  • Pre-filter / post-filterApplying conditions before or after the nearest-neighbour search.

Try it without code

List your filters

  1. For your documents, list every field a user might filter on (date, product, region, author, type).
  2. Mark which ones are security filters.

Use it in code

Semantic search with a tenant and date filter in pgvector.

python
def search(conn, qvec, tenant_id, since, k=5):
    return conn.execute("""
        SELECT body, doc, embedding <=> %(q)s AS dist
        FROM chunks
        WHERE tenant_id = %(t)s AND published_at >= %(since)s
        ORDER BY dist
        LIMIT %(k)s""", {"q": qvec, "t": tenant_id, "since": since, "k": k}).fetchall()
PRACTICE · TICK IT AFTER THIS

Add doc_type and year columns to your chunks table and let an LLM turn questions into a filter object before searching.

Common mistakes

  • Applying tenant filters in Python after retrieval, leaking ranking signals.
  • Post-filtering and silently getting zero results.

Go deeper