
Metadata Filtering in Chroma
Learn how to use Chroma's powerful 'where' and 'where_document' filters to narrow down search results.
Metadata Filtering in Chroma
In a real RAG application, you rarely want to search the entire database. You might want to search only documents owned by a specific user, or those published within the last month. Chroma's metadata filtering makes this efficient.
The where Clause
The where clause filters based on the metadata dictionary associated with each vector.
# Simple filter
results = collection.query(
query_texts=["How do I reset my password?"],
where={"department": "Security"}
)
# Compound filter
results = collection.query(
query_texts=["How do I reset my password?"],
where={
"$and": [
{"department": "Security"},
{"access_level": "Public"}
]
}
)
Operators Supported
$eq: Equal to$ne: Not equal to$gt: Greater than$gte: Greater than or equal to$lt: Less than$lte: Less than or equal to
The where_document Clause
Unlike where (which checks the metadata dict), where_document checks for specific strings inside the actual document content using keyword matching (substring search).
# Find technical docs that specifically mention 'GPU'
results = collection.query(
query_texts=["performance issues"],
where_document={"$contains": "GPU"}
)
Why Filter Before Searching?
Chroma performs Pre-filtering. This means it identifies the subset of documents that match your filter first, and then performs the vector search only on that subset. This is much faster and more accurate than searching the whole database and then filtering the results manually.
Use Case: Multi-Tenant RAG
If you are building a tool for multiple companies, you must filter by company_id on every query to ensure Company A never sees Company B's data.
results = collection.query(
query_texts=[user_query],
where={"tenant_id": current_user.tenant_id}
)
Exercises
- Create a collection with metadata for "Price" and "Category".
- Perform a query where the Price is
$gt100. - Compare the results of a "Naive Search" vs. a "Filtered Search" for a specific category.