Prompt
I'm building search for private content with access control per user
Latest observation
A good pattern for searching private content with per-user access control is to treat search as a filtered retrieval problem, not just a text matching problem.
Core idea
When a user searches, only return documents they’re allowed to see. That means your search system needs:
- Full-text search over the content
- Authorization filter applied at query time
- A way to keep permissions indexed efficiently
Common approaches
1) Filter at query time
Store each document with an ACL field, for example:
owner_user_idshared_with_user_idsshared_with_group_idsvisibility = private/team/org
Then on search:
- run keyword/vector search
- filter results by the current user’s permissions
This is the simplest pattern, but the filter must be implemented carefully so unauthorized docs never leak.
2) Precompute access lists
For each document, store an “allowed principals” list:
- user IDs
- group IDs
- org IDs
- roles
At search time, compute the current user’s principals and query:
doc.allowed_principals intersects user.principals
This works well for many apps and search engines that support filtering.
3) Security-trimmed search index
If you’re using a search engine like Elasticsearch/OpenSearch, index permissions alongside content. Then every query includes a mandatory access filter.
This is often called security trimming:
- search results are ranked normally
- only accessible docs are returned
Important design rules
Never rely on client-side filtering
Do not return all matching docs to the frontend and hide unauthorized ones in the UI. The backend must enforce access.
Keep authorization data in the same trust boundary
The search index should not be directly queryable by clients. Put an API in front of it.
Be careful with group membership changes
If users can join/leave groups, you need permissions to reflect that quickly:
- either query against group IDs dynamically
- or reindex affected documents if you denormalize access data
Recommended data model
A practical schema:
{
"id": "doc_123",
"title": "Quarterly report",
"body": "....",
"owner_user_id": "user_1",
"allowed_user_ids": ["user_1", "user_7"],
"allowed_group_ids": ["group_a"],
"org_id": "org_9",
"visibility": "private"
}
At search time, the user context might be:
{
"user_id": "user_7",
"group_ids": ["group_a", "group_c"],
"org_id": "org_9"
}
Then your filter becomes something like:
owner_user_id = user_7- OR
allowed_user_ids contains user_7 - OR
allowed_group_ids intersects ["group_a", "group_c"] - OR
visibility = org AND org_id = org_9
If you’re using vector search / embeddings
Same rule applies: vector similarity should be computed only over docs the user can access, or results must be filtered before returning.
Best practice:
- apply access filters in the vector DB if supported
- otherwise do a post-filter with care, but ensure you fetch enough candidates so valid results aren’t missed
Performance considerations
For large-scale ACLs:
- avoid huge per-doc user lists if possible
- prefer group/org-based permissions
- use bitsets, inverted ACL indexes, or principal-based filtering
- keep permission checks cheap at query time
Safer architecture
A robust setup is:
- App DB stores documents and permissions
- Search index stores searchable content + permission metadata
- Search API authenticates user
- Search API injects mandatory authorization filter
- Results are returned only if authorized
Questions to decide the best approach
- How many documents and users do you expect?
- Are permissions mostly per-user, per-group, or per-org?
- Is this keyword search, vector search, or both?
- Do permissions change frequently?
- Do you need real-time permission revocation?
If you want, I can help you design:
- a schema for your exact permission model
- an Elasticsearch/OpenSearch query pattern
- a Postgres-based search approach
- or a vector-search-safe architecture