Each test is 5 questions with varying difficulty.
AI Prep covers AI Agents, Generative AI, ML Fundamentals, NLP & LLMs and a lot more, with adaptive tests and daily challenges. Fully offline on Android. Free to try, one-time unlock for lifetime access.
Metadata filtering in vector search is the process of applying scalar constraints (e.g., category, timestamp, user_id) alongside semantic vector similarity. In 2026, as RAG systems scale to millions of documents, pure vector search is rarely sufficient; filtering is essential for precision and compliance. Interviewers ask about this to test your understanding of how vector indices (like HNSW or IVF) interact with inverted indices. Junior candidates are expected to understand the difference between pre-filtering and post-filtering, while senior candidates must reason about the performance trade-offs, such as how high-selectivity filters can cause 'recall collapse' in approximate nearest neighbor (ANN) search and how to mitigate it using bitmasking or integrated graph traversal.
Metadata filtering is the primary mechanism for narrowing search spaces in production AI systems. Without it, a RAG pipeline might retrieve semantically relevant but contextually invalid documents (e.g., retrieving private data from another tenant). In 2026, the challenge has shifted from 'can we filter?' to 'can we filter without destroying latency or recall?'. A weak answer treats filtering as a simple SQL WHERE clause, ignoring that ANN algorithms like HNSW are optimized for global space, not constrained subspaces. A strong answer discusses how pre-filtering can lead to 'over-filtering' where the search space becomes too small for the ANN algorithm to find the true nearest neighbors, or how post-filtering leads to empty result sets if the filter is too restrictive. Understanding this is critical for designing multi-tenant SaaS platforms where data isolation is non-negotiable.
The architecture involves a dual-index approach where a vector index (e.g., HNSW) and a scalar index (e.g., Inverted Index) are synchronized. During query time, the system performs a cost-based decision to either intersect the indices or apply filters during the graph traversal.
The query enters the parser, which splits it into vector and scalar parts. The scalar filter hits the inverted index to produce a bitmask. This bitmask is passed to the vector search engine to prune the graph traversal or the candidate list.
Query Input
↓
[Query Parser]
↓ ↓
[Scalar] [Vector]
↓ ↓
[Inverted] [HNSW Graph]
↓ ↓
[Bitmask] → [Traversal]
↓
[Top-K Results]
During HNSW graph navigation, check the bitmask of a candidate node before calculating the distance.
Trade-offs: Reduces distance calculations but increases traversal overhead.
Estimate selectivity of a filter; if selectivity is low, use post-filtering; if high, use pre-filtering.
Trade-offs: Requires query planning overhead.
Store metadata in a separate B-Tree or GIN index and intersect with vector results.
Trade-offs: High consistency but slower intersection latency.
| Reliability | Use atomic transactions to ensure metadata and vector indices are updated in sync. |
| Scalability | Partition indices by tenant or category to limit the scope of filter lookups. |
| Performance | High-selectivity filters should be pre-filtered to minimize distance calculations. |
| Cost | Minimize memory footprint by using compressed bitsets for metadata indices. |
| Security | Enforce mandatory tenant-id filtering at the database layer to prevent cross-tenant data leakage. |
| Monitoring | Track 'filter_selectivity' and 'recall_at_k' as primary performance indicators. |
Pre-filtering applies metadata constraints before the vector search, limiting the search space. Post-filtering performs the vector search first and then filters the results. Pre-filtering is generally more efficient for highly selective filters, whereas post-filtering is better when the filter matches most documents.
Pre-filtering can remove the true nearest neighbors from the search space before the ANN algorithm can evaluate them. If the filter is too restrictive, the remaining vectors may be disconnected in the HNSW graph, preventing the search from finding the optimal results.
Filter selectivity is the proportion of documents that satisfy a given metadata condition. High selectivity means the filter matches a small percentage of the total index, while low selectivity means it matches a large percentage.
Bitmasks provide a compact, memory-efficient way to represent which documents satisfy a filter. During graph traversal, the search engine can check the bitmask to skip invalid nodes without performing expensive distance calculations.
Yes, many modern vector databases (like pgvector or Milvus) allow SQL-like syntax for filtering. However, the underlying implementation often involves intersecting an inverted index with a vector index, which is more complex than standard SQL execution.
Recall collapse occurs when an aggressive metadata filter removes so many vectors that the ANN algorithm fails to find the actual nearest neighbors. This is a common failure mode in HNSW-based vector databases.
Yes, post-filtering is preferred when the filter is not very selective (i.e., it matches most documents). In this case, the overhead of pre-filtering and the risk of recall collapse outweigh the cost of filtering the results after the search.
For high-cardinality fields, use specialized indices like B-Trees or inverted indices. Avoid storing these as raw strings; use integer IDs or categorical encoding to reduce memory usage and speed up intersection operations.
A query planner analyzes the filter conditions and the index statistics to decide whether to use pre-filtering or post-filtering. It helps ensure that the search strategy is optimal for the specific query and data distribution.
The most secure way is to enforce mandatory metadata filtering on a 'tenant_id' field. This ensures that every query is scoped to the user's data, preventing cross-tenant leakage at the database level.
AI Prep covers AI Agents, Generative AI, ML Fundamentals, NLP & LLMs and a lot more, with adaptive tests and daily challenges. Fully offline on Android. Free to try, one-time unlock for lifetime access.