Prompt
vector search with metadata filter
Latest observation
Oct 10, 2026
OpenAI APIWeb search: off
Vector search with metadata filtering means:
- Find semantically similar items using embeddings / vectors.
- Restrict the results to items whose metadata matches some condition
like
category = "invoice"ortenant_id = 123.
Common approaches
1) Pre-filter then vector search
Apply the metadata filter first, then run nearest-neighbor search only on the remaining items.
Good when:
- Filter is selective
- Metadata is indexed well
- You want exact filter behavior
Example
SELECT *
FROM documents
WHERE tenant_id = 42 AND status = 'active'
ORDER BY embedding <-> query_embedding
LIMIT 10;
2) Vector search then post-filter
Run vector search first, then discard results that don’t match metadata.
Good when:
- Filter is weak / broad
- System doesn’t support efficient metadata filtering during ANN search
Downside:
- You may need to fetch many more candidates to get enough valid results
3) Hybrid / integrated ANN + filter
Some vector databases support metadata filters natively during ANN search.
Examples:
- Pinecone
- Weaviate
- Milvus
- Qdrant
- Elasticsearch / OpenSearch kNN with filters
- PostgreSQL with
pgvector+ SQL filters
This is usually the best option if your database supports it efficiently.
Why metadata filtering matters
Without filtering, a top-k vector search can return semantically similar but irrelevant items, such as:
- wrong user / tenant
- wrong document type
- wrong language
- wrong time range
- wrong access permissions
So filtering is often required for:
- multi-tenant apps
- RAG systems
- enterprise search
- recommendation systems with business constraints
Practical pattern
A typical query looks like:
{
"vector": [0.12, -0.03, ...],
"top_k": 10,
"filter": {
"tenant_id": 42,
"doc_type": "policy",
"published": true
}
}
Design tips
- Index metadata fields you filter on often.
- Keep filters simple and selective.
- If using approximate nearest neighbor search, test recall under filters.
- For RAG, store fields like:
tenant_idsourcedoc_typecreated_atlanguageacl_roles
If you want implementation help
I can show this for a specific stack, for example:
- Postgres + pgvector
- Pinecone
- Weaviate
- Qdrant
- Milvus
- OpenSearch / Elasticsearch
- FAISS with metadata in your app layer
If you tell me your database, I can give you the exact query/code.