Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 36 ยท Retrieval Strategies

Metadata Filtering and Query Transformation

Learn pre-filter vs post-filter search, how to extract filters from a question (self-query), and how query expansion and multi-query retrieval raise recall.

Similarity alone is not enough in real systems. A user asking "what is the refund policy for the EU plan?" does not want the US policy, even if its text is almost identical. A user on version 3 of your product should not see version 2 instructions. These constraints are metadata: structured fields stored beside each chunk, such as product, language, version, region, published_at or tenant_id.

This lesson covers two tools that sit around the vector search itself: filtering on metadata, and query transformation, which improves the query before it ever reaches the index.

Why filter

  • Correctness: the right answer depends on region, version or audience.
  • Security: one customer must never retrieve another customer's chunks (access control).
  • Efficiency: searching only the relevant slice is faster and has less noise.
  • Freshness: prefer or require recent documents for time-sensitive topics.

Pre-filtering versus post-filtering

There are two ways to combine a filter with nearest-neighbour search.

  • Post-filter: search the whole index for the top k, then drop results that fail the filter. Simple, but you may end up with fewer than k (even zero) results.
  • Pre-filter (filtered search): restrict the candidate set first, then find the top k inside it. You always get k results if k matching chunks exist. Modern vector databases do this inside the index, with different strategies for very selective filters.
Python Why post-filtering can return nothing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The three US refund chunks crowd out the EU chunk in the top 3, so the post-filter returns an empty list. The pre-filter finds the EU chunk. The same effect happens at scale with approximate indexes, which is why checking how your database implements filtering matters (the indexing chapter of this course explains recall in approximate search).

Index the fields you filter on (payload or scalar indexes). An unindexed filter over millions of chunks turns a millisecond query into a scan.

Self-query: getting filters from the question

Users do not type region=eu. They write "refunds for European customers on version 3". Self-query retrieval extracts a structured filter plus a clean semantic query from the natural-language question. In production an LLM does this with a JSON schema; a simpler rule-based extractor works for small, well-known vocabularies and makes a good first version.

Python Rule-based self-query

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With an LLM the same idea becomes: "Given this schema of fields and this question, return JSON with query and filter." Always validate the model's output against an allow-list of field names and types before passing it to the database, otherwise a hallucinated field raises errors or, worse, a crafted question changes which data is searched.

Query transformation

Short or vague queries retrieve poorly. Several transformations help.

  • Normalization: fix typos, expand abbreviations and acronyms ("2FA" to "two-factor authentication"), strip filler ("please tell me how to").
  • Expansion: add synonyms or related terms; useful for the keyword side.
  • Multi-query: generate several paraphrases, search with each, and fuse the lists with RRF. It improves recall for ambiguous questions.
  • Decomposition: split a compound question ("compare plan A and plan B prices and limits") into sub-questions, retrieve for each and merge.
  • HyDE and step-back: covered in Advanced RAG Patterns.
Python Multi-query retrieval with a synonym table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The single query "get my money back" shares no words with the refund chunk and returns nothing; the expanded variant "get my refund" finds it. An LLM can generate such paraphrases for any domain, at the cost of one extra model call per question (see caching, latency and cost).

Pitfalls

  • Over-filtering: a wrongly extracted filter returns nothing. Fall back to an unfiltered search when a filtered one is empty, and log it.
  • Stale metadata: if you update a document's version but not its chunks' metadata, filters hide the right content.
  • Too many variants: each variant costs a search; three to five is usually enough.
  • Filtering on fuzzy fields: keep filter fields structured (enums, ids, dates), not free text.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main risk of post-filtering?

    Filtering after taking the top k throws away results, possibly leaving fewer than k.
  2. 2. What does self-query retrieval do?

    It turns a free-text question into a semantic query plus metadata filter.
  3. 3. Why validate an LLM-generated filter before running it?

    Treat model output as untrusted input; check field names and values against an allow-list.
  4. 4. How are multi-query results usually combined?

    RRF merges ranked lists from several paraphrases into one.

Summary

  • Metadata filters enforce correctness, security and freshness; index the fields you filter on.
  • Prefer pre-filtering (filtered search) so you still get k results.
  • Self-query extracts a filter and clean query from natural language; validate the output.
  • Query normalization, expansion, multi-query and decomposition raise recall.
  • Fall back gracefully when a filter returns nothing.

Next: Re-ranking and Maximal Marginal Relevance