# What Is Metadata Filtering?

> How metadata filtering narrows a search over your documents, with tags like country, date and team, its traps and access rules, explained simply for your AI interview.

**In short:** Metadata filtering keeps only the chunks whose labels match, such as country, date or team, usually before the search ranks them. It helps an AI search avoid quoting old, wrong or off-limits text, even when that text matches the question's words well, if the tags are right.

AI and LLM interview question Q129, Retrieval-augmented generation (RAG). Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-metadata-filtering

## Say it in 30 seconds

Metadata filtering means I keep only the chunks whose labels match, usually before the search ranks them. Say a bank's support bot is asked about card fees for Ireland. I filter to Ireland and current schedules, so a 2019 UK sheet never reaches the model. My code sets those values, not the model. I also check that the filter doesn't hide the right answer, because a missing tag hides it silently.

## Say it in 2 minutes

Metadata filtering means I keep only the chunks whose labels match, usually before the search ranks them. Each chunk in our index carries tags like country, date, status and team. A customer in Ireland asking about card fees should never reach a 2019 UK schedule, even when its wording matches better.

The order matters. A pre-filter removes the non-matching chunks first, then ranks what is left, so the top five are all eligible. A post-filter ranks everything first, then drops the chunks that don't match, which can leave fewer results than I asked for. I check which one the tool does, because the answer can change.

The values come from my code, never from the model. The app reads the customer's country from their account and sets it in the filter. For access rules I pass the user's own permissions, so the search only ranks documents that person may read, as long as the access tags are complete and correct. I also count the results after filtering, since a missing tag can hide the right answer without any error.

Managed tools offer this too. OpenAI's file search, Google's Agent Search and AWS Bedrock Knowledge Bases all support filters on metadata. The skill is designing the tags up front and testing what each filter removes.

## What is metadata filtering, in plain words?

In a RAG system (retrieval-augmented generation), the app searches your own documents and gives the best parts to the model. Each chunk, a short piece of a document cut so it fits in a prompt, can carry labels called metadata. Examples are the country, the date, whether it is current, or which team may read it.

Metadata filtering keeps only the chunks whose labels match, usually before the search ranks them by meaning. The search then looks only at what is left.

Think of a new claims handler at an insurance firm who needs the Ohio motor rules. They don't read every policy binder in the building. They walk to the Ohio shelf, pick the current edition, and search inside it. The shelf is the filter, and the reading is the search. In an AI app, the shelf is a metadata filter: a customer in Ireland only reaches the Irish, current fee schedules.

- **Metadata:** Labels on a chunk, such as country, date, status or team.
- **Chunk:** A piece of a document, cut small enough to search and to fit in a prompt.
- **Filter:** A rule that keeps only the chunks whose labels match.
- **Top-k:** How many best matches the search returns, such as five.

## How does a filtered search work?

Say a mid-size bank runs a support assistant over nine years of fee schedules, about 120,000 chunks. A customer in Ireland asks what a card costs. With no filter, the search ranks all 120,000 chunks. An old UK schedule from 2019 may score higher, because its wording is close, so the assistant quotes the wrong fee.

- **Without a filter: Everything is in the race.** The search ranks all 120,000 chunks, so a 2019 UK schedule can win on wording alone.
- **With a filter: Only the right set is ranked.** Only chunks tagged Ireland and current are ranked, about 6,200 of them, so the answer is about today's fees.

*Diagram, How a filtered search flows: Your code sets the filter from the customer's account, so the model can't change it. Only chunks whose tags match go into the search, and the question runs against that smaller set. The model answers from the top five results.*
1. Your code reads the customer's country from their account.
2. Each chunk's tags are checked against that filter.
3. Only the matching chunks go on to the search.
4. The customer's question goes to the same search.
5. The model reads the top five results and answers from them.

The filter values come from your code, not the model. The app knows the customer's country from their account, so it sets the filter itself. The model only reads what the search returns.

Managed tools offer this too. OpenAI's file search, Google's Agent Search and AWS Bedrock Knowledge Bases all let you filter on metadata.

## Does the filter run before or after the search?

This is where candidates slip. Vector search ranks chunks by meaning, not by exact words. A pre-filter removes the non-matching chunks first, then ranks what is left. A post-filter ranks everything first, takes the top results, then drops the ones that don't match. The second can leave you with fewer results than you asked for.

> "Query results will only include vectors that match the filter criteria, meaning that filter is applied first, and the topK results are taken from the filtered set." (Cloudflare Vectorize)
>
> "A filter provides value-based criteria for including or excluding content before query execution for keyword search, or before or after query execution for vector search." (Microsoft Azure AI Search)

*Diagram, Filter before or after the ranking: The same filter can give different answers depending on order. Filter first ranks only the matching chunks, so five full results are possible. Filter after can leave fewer, and which one you get depends on how your tool works.*
1. The matching chunks are kept first, down from 120,000 to 6,200.
2. The top five then come from those 6,200 only.
3. Here the ranking runs on all 120,000 chunks first.
4. Then the filter drops any that don't match, so fewer than five can remain.

Check which order your tool uses before you promise an answer. Cloudflare's Vectorize filters first. Azure's vector search lets you pick the order with its vectorFilterMode setting: prefiltering or postfiltering. The page says the mode affects latency, recall and throughput.

## What goes wrong with metadata filters?

- **A chunk has no tag:** A chunk without the tag never shows up, even when it holds the only right answer. Count the results after filtering, so a gap shows up as a number, not as a wrong answer.
- **The field can't be filtered:** Some tools index a field for search but don't allow it as a filter. Google's guide says an indexable field may still not support filtering, so check the field setting first.
- **Tags must come before the data:** Cloudflare's Vectorize docs (last updated April 2026) say metadata indexes must be created before vectors are inserted. Vectors added earlier won't carry those tags, so the filter misses them.
- **The model should not pick access:** Google lists having natural-language understanding create filters from users' queries as one option. It is convenient, but a wrong guess hides the right chunk. Access rules should come from the logged-in user, in code.

Access is the serious case. Azure's search docs describe security filters, where the user's security ID is passed as a filter. A match stands in for access rights, so the filter only protects documents when every document carries the right security tags.

> **Tip:** Asked how you'd make search safe? Say the filter runs in code, set from the logged-in user, and that you count results after filtering to catch gaps.

### How would you filter a legal search over 40,000 contracts?

A legal team needs clauses from 40,000 contracts, but a junior lawyer must never see a matter they aren't staffed on. Each chunk is tagged with its matter ID, client and expiry date when it is loaded.

1. **Set access in code:** The matter list comes from the lawyer's login. The question never sets it, so a clever prompt can't widen it.
2. **Filter, then rank:** The search runs only on the matter IDs that lawyer may see, and only on contracts that have not expired.
3. **Count what is left:** Log how many chunks survive the filter. A near-zero count usually means a tag is missing, not that the answer doesn't exist.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: keep only the chunks whose labels match, usually before the ranking.
- [ ] Pre-filter versus post-filter, and why the order changes results.
- [ ] Tags are set when data is loaded, and filter values come from code.
- [ ] Missing or unfilterable tags hide good chunks, silently.
- [ ] Access rules are filters too, set from the logged-in user.
- [ ] Counting results after filtering to catch a bad filter.

**Common traps**

- Trap: "Just tell the prompt to only use current documents." Better: A prompt is a request. A filter is a rule the search applies before the model sees a chunk.
- Trap: "Filters always make answers more accurate." Better: Only if the tags are right and complete. A wrong or missing tag hides the answer.
- Trap: "The model can pick the filter from the question." Better: It can suggest one, but code should set access rules from the logged-in user, not from the model's guess.

## Can you spot the problem?

**Spot the problem:** A claims assistant searches policy chunks for many adjusters. In the code, vector is the question turned into numbers (its meaning), and top_k=5 is how many results to return. Which search call is safe to ship?

A. search(vector, top_k=5)  # every team's policies are ranked, and the reply hides the rest
B. search(vector, top_k=5, filter={"team": ask_model_for_team(question)})
C. search(vector, top_k=5, filter={"team": user.team_id, "status": "current"})
D. search(vector, top_k=5, filter={"status": "current"})  # team checked later, in the reply

**Answer:** C. The team comes from the logged-in user, in code, so the model can't change it. The status tag keeps old policies out. The other three let the model pick the team, search everything, or show another team's chunks before any check.

**Quick check:** A RAG bot keeps quoting 2019 fee rules to customers who ask about today's fees. What do you check first?

A. The model's size
B. Whether old chunks have a status tag, and the filter asks for current
C. The randomness setting
D. Whether the bot's reply uses bullet points

**Answer:** B. A filter can only drop old chunks if they carry the tag and the query asks for it. Model size, the randomness setting and bullet points don't decide which chunks get searched.

## Can you answer these from memory?

**Flash cards**

- **What is metadata filtering?** Keeping only the chunks whose labels match, usually before the search ranks them.
- **Pre-filter or post-filter?** Pre-filter narrows first. Post-filter ranks first, so you can get fewer results.
- **Where do filter values come from?** Your code, from the logged-in user and the request. Not from the model's guess.
- **What fails silently?** A chunk with a missing tag never shows up, even when it is the right answer.
- **Is a prompt rule enough?** No. A prompt is a request. A filter runs in the search before the model sees a chunk.
- **Why do access filters matter?** They stop a search returning documents the user may not read.

## Likely follow-up questions

- Q123 What is hybrid search (BM25 + vectors)? https://designervenkat.online/interview/ai/what-is-hybrid-search
- Q126 Rerankers: cross-encoder vs bi-encoder? https://designervenkat.online/interview/ai/cross-encoder-vs-bi-encoder-rerankers
- Q139 How do you keep the index fresh? https://designervenkat.online/interview/ai/how-do-you-keep-the-index-fresh
- Q140 How do you enforce access control in RAG? https://designervenkat.online/interview/ai/how-do-you-enforce-access-control-in-rag
- Q146 How do you pick top-k? https://designervenkat.online/interview/ai/how-do-you-pick-top-k

## Learn it in depth

- [RAG & Dynamic Filters](https://designervenkat.online/learn/ai-engineering/rag-and-dynamic-filters)
