Skip to content
mentoor

Topic 3 of 5 · 6 min read

Filtering by metadata during vector search

Goal: Fix a filtered vector query that returns too few or wrong results by changing where and how the filter is applied.

With an approximate index, a metadata filter can be applied after the nearest-neighbour search, and a selective filter then leaves you with fewer than k results, or none.

Check yourself · 1 of 3

A filtered vector query with LIMIT 10 returns only 2 rows, though 5,000 rows match the filter. What is the most likely cause?

Choose one answer

Why it matters

Real RAG queries are filtered: one tenant's documents, one language, files the user may see, pages from this year. A filter that returns nothing, or returns a short list, gives the model too little context, and nobody gets an error. Access filters are also a security boundary, so "mostly applied" is not acceptable.

How the filter meets the index

In pgvector the planner picks one of two plans.

  • Index first, filter after (post-filtering). The HNSW scan returns its candidate list (hnsw.ef_search, 40 by default), and Postgres then drops rows that fail WHERE. If the filter matches 1% of rows, a list of 40 candidates leaves about 0 to 1 rows. You asked for LIMIT 5 and got 1.
  • Filter first (pre-filtering). The planner uses a normal B-tree index on the filter column, then sorts the matches exactly. Correct and complete, and fast only when the filter is selective enough to leave a small set.

Dedicated databases such as Qdrant apply the filter during graph traversal, so the search keeps walking until it has enough matching points. pgvector 0.8.0 added iterative scans (hnsw.iterative_scan), which keep scanning the index until enough rows pass the filter.

How to fix a short result

  1. Check the symptom: SELECT count(*) with the filter alone, and compare to rows returned.
  2. Index the filter column (B-tree) so the planner can choose filter-first when it is selective.
  3. Turn on iterative scan, or raise hnsw.ef_search, for filters that match a medium share of rows.
  4. For a handful of huge tenants, use partial indexes or partitioning by tenant, so each tenant has its own small vector index.

Before and after

-- Before: tenant 42 owns 0.5% of rows, ef_search is 40
SELECT id FROM chunks WHERE tenant_id = 42
ORDER BY embedding <=> $1 LIMIT 10;   -- returns 0 to 2 rows
CREATE INDEX ON chunks (tenant_id);
SET hnsw.iterative_scan = relaxed_order;
SELECT id FROM chunks WHERE tenant_id = 42
ORDER BY embedding <=> $1 LIMIT 10;   -- returns 10 rows

What goes wrong

  • Filtering in application code. Fetching the top 10 then dropping other tenants' rows leaves fewer than 10, and risks leaking data if the drop is forgotten.
  • Testing on a large tenant. The small tenants break, and they are discovered in production.
  • Ignoring relaxed_order. That mode may return results slightly out of order; re-sort the final list if order matters.

Examples

Ten results asked, two returned

Tenant 42 has 5,000 of 1,000,000 chunks. The HNSW scan collects 40 candidates, about 0.2 of one belongs to tenant 42, and the query returns almost nothing. Enabling iterative scan returns 10 rows without changing the SQL's meaning.

Per-tenant index

For the three largest tenants, CREATE INDEX ON chunks USING hnsw (embedding vector_cosine_ops) WHERE tenant_id = 7; gives each a graph that holds only its own rows. Queries add WHERE tenant_id = 7 and use it.

Key points

  • A filter applied after an approximate index scan can leave fewer than k results, with no error.
  • The more selective the filter, the more likely a post-filtered query returns too few rows.
  • Fixes are a B-tree index on the filter column, iterative scans or a larger ef_search, and partial indexes or partitions for big tenants.
  • Dedicated vector databases such as Qdrant apply payload filters during graph traversal.
  • Never filter by tenant or permission in application code after the search; put it in the query.

Common mistakes

  • Filtering the top-k in code after the query.
  • Testing filters only with common values such as the largest tenant.
  • Assuming LIMIT 10 guarantees 10 rows.
  • Putting access control in a prompt instead of the query.

Check yourself · 2 of 3

Where should a per-user access filter be applied in a RAG pipeline?

Choose one answer

Check yourself · 3 of 3

Which change helps when a selective filter returns too few rows from an HNSW search?

Choose one answer

Practice items, not counted towards your mastery.

Start here