Prompt
How do I filter vector search by tenant and access control?
Latest observation
To filter vector search by tenant and access control, the key idea is:
Never rely on vector similarity alone.
Always combine semantic search with metadata filters applied at query time, and ideally also enforce them at ingestion/indexing time.
Common pattern
Store each chunk/vector with metadata like:
{
"id": "doc_123_chunk_4",
"vector": [ ... ],
"tenant_id": "tenant_a",
"acl": ["user_17", "group_sales", "role_manager"],
"doc_type": "policy",
"created_at": "2026-01-01"
}
Then query with:
- Semantic similarity against the vector
- Filter by:
tenant_id == current_tenant- ACL rules that match the current user
Access control approaches
1) Tenant isolation
Simplest and strongest:
WHERE tenant_id = 'tenant_a'
This ensures users only search within their tenant’s data.
2) ACL filtering
Filter on one or more of:
- user ID
- group IDs
- roles
- document visibility flags
Examples:
owner_user_id = current_usershared_with contains current_userallowed_groups intersects user_groupsvisibility = 'public'
A practical rule is:
can_access = (owner == user)
OR (tenant_id matches AND acl overlaps user's groups/roles)
OR (document is public)
Example query logic
Pseudocode
results = vector_search(
query_vector,
filter={
"tenant_id": current_tenant,
"$or": [
{"owner_user_id": current_user},
{"allowed_groups": {"$in": user_groups}},
{"allowed_roles": {"$in": user_roles}},
{"visibility": "public"}
]
},
top_k=10
)
If your vector DB supports metadata filters
Pinecone-style
filter = {
"tenant_id": {"$eq": "tenant_a"},
"$or": [
{"owner_user_id": {"$eq": "user_17"}},
{"allowed_groups": {"$in": ["sales", "eng"]}},
{"visibility": {"$eq": "public"}}
]
}
Weaviate-style
where: {
operator: And
operands: [
{ path: ["tenant_id"], operator: Equal, valueText: "tenant_a" },
{ operator: Or, operands: [
{ path: ["owner_user_id"], operator: Equal, valueText: "user_17" },
{ path: ["allowed_groups"], operator: ContainsAny, valueTextArray: ["sales", "eng"] }
]}
]
}
SQL + pgvector
SELECT id, content
FROM chunks
WHERE tenant_id = $1
AND (
owner_user_id = $2
OR allowed_groups && $3
OR visibility = 'public'
)
ORDER BY embedding <=> $4
LIMIT 10;
Best practices
1) Filter before ranking if possible
Apply tenant/ACL filters during retrieval, not after, so unauthorized results are never considered.
2) Denormalize access data
Put enough ACL info in the vector record itself to evaluate access quickly.
3) Use tenant scoping everywhere
Every query should require a tenant filter, even if the dataset is “supposedly separated.”
4) Index filters
Make sure tenant_id, owner_user_id, visibility, and ACL fields are indexed or efficiently supported.
5) Be careful with group expansion
If a user belongs to many groups, compute the user’s effective access set at query time and pass that in.
6) Validate at the application layer too
Even if the vector DB filter is correct, enforce access control again in your API/service layer.
Recommended model
A robust schema is:
{
"tenant_id": "tenant_a",
"resource_id": "doc_123",
"chunk_id": "doc_123_4",
"visibility": "private|team|tenant|public",
"owner_user_id": "user_17",
"allowed_users": ["user_17", "user_22"],
"allowed_groups": ["sales"],
"embedding": [ ... ]
}
Then access logic can be:
public: anyone in tenanttenant: anyone in tenantteam: users in allowed groupsprivate: owner or explicit users
Practical recommendation
If you want the simplest secure design:
- Add
tenant_idto every vector record - Add ACL metadata (
allowed_users,allowed_groups,visibility) - At query time, always filter by
tenant_id - Then filter by the current user’s allowed users/groups/roles
- Enforce the same policy outside the vector DB as a backstop
If you tell me which vector database you’re using (Pinecone, Weaviate, pgvector, Milvus, Chroma, etc.), I can give you the exact filter syntax and a concrete example.