AI Engineer Portal
Your personal operating system for career transition.
Exercise
Implement retrieval with metadata filtering
Implement Retrieval with Metadata Filtering
Pure semantic search returns results regardless of when documents were created, who authored them, or what type they are. In production, users need scoped retrieval: "search only the 2024 handbook," "return only verified content," "find contract clauses from after 2023."
Metadata filtering adds these constraints. Pre-filtering narrows the candidate set before semantic scoring; it requires understanding the tradeoffs of strict vs. permissive filtering.
What you are building
Create a FilterableIndex that:
- Stores documents with typed metadata — strings, numbers, booleans.
- Supports pre-filtering via a filter expression:
{"field": {"op": value}}. Supported operators:eq,ne,gt,lt,gte,lte,in,contains. - Supports AND logic via
{"$and": [filter1, filter2]}. - Returns results with metadata intact for display alongside answers.
- Raises
FilterErrorfor unsupported operators.
Constraints
- Filter matching in pure Python — no external libraries.
- All metadata values stored as Python native types (str, int, float, bool).
search(query, top_k, filters=None) -> list[SearchResult].
Rag Systems / medium / Step 6 of 6
General drill
Keep the solution explicit and reviewable.
This is the end of the current mini-sequence.
Make the solution explicit, debuggable, and easy to explain.
Review where the boundary is, what gets validated, and what would be hard to debug later.
Practice
Generate a variation
Generate a new exercise variation to deepen understanding or practice a related concept.
Attempt history
Recent submissions
Before you submit, decide what a strong answer should make obvious to the reviewer.
No attempts yet.