方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

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:

  1. Stores documents with typed metadata — strings, numbers, booleans.
  2. Supports pre-filtering via a filter expression: {"field": {"op": value}}. Supported operators: eq, ne, gt, lt, gte, lte, in, contains.
  3. Supports AND logic via {"$and": [filter1, filter2]}.
  4. Returns results with metadata intact for display alongside answers.
  5. Raises FilterError for 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

Practice stage

General drill

Hint

Keep the solution explicit and reviewable.

Next drill

This is the end of the current mini-sequence.

Success criteria

Make the solution explicit, debuggable, and easy to explain.

Review checklist

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.