Field and document level security helps control who can see specific parts of your data. It keeps sensitive information safe by limiting access.
Field and document level security in Elasticsearch
Start learning this pattern below
Jump into concepts and practice - no test required
PUT /_security/role/role_name
{
"indices": [
{
"names": ["index_name"],
"privileges": ["read"],
"field_security": {
"grant": ["field1", "field2"]
},
"query": {
"term": {
"user_id": "{{_user.username}}"
}
}
}
]
}field_security.grant lists fields the user can see.
query filters documents the user can access.
PUT /_security/role/hr_role
{
"indices": [
{
"names": ["employees"],
"privileges": ["read"],
"field_security": {
"grant": ["name", "salary", "department"]
}
}
]
}PUT /_security/role/support_role
{
"indices": [
{
"names": ["customers"],
"privileges": ["read"],
"field_security": {
"grant": ["name", "email"]
},
"query": {
"term": {
"region": "us-east"
}
}
}
]
}PUT /_security/role/owner_role
{
"indices": [
{
"names": ["documents"],
"privileges": ["read"],
"query": {
"term": {
"owner": "{{_user.username}}"
}
}
}
]
}This example creates a role that lets users read only the report_name and total_revenue fields from financial_reports index, but only for documents where department is 'finance'.
When a user with this role searches, they see only allowed fields and documents.
PUT /_security/role/finance_role
{
"indices": [
{
"names": ["financial_reports"],
"privileges": ["read"],
"field_security": {
"grant": ["report_name", "total_revenue"]
},
"query": {
"term": {
"department": "finance"
}
}
}
]
}
# After creating this role, assign it to a user to restrict their access.
GET /financial_reports/_search
{
"query": {
"match_all": {}
}
}Field and document level security works together to limit data exposure.
Use {{_user.username}} to personalize document access based on the logged-in user.
Remember to assign roles to users after creating them.
Field level security controls which fields a user can see.
Document level security controls which documents a user can access.
Together, they protect sensitive data in Elasticsearch.
Practice
field-level security in Elasticsearch?Solution
Step 1: Understand field-level security concept
Field-level security controls which fields in a document a user can see or query.Step 2: Compare with other options
Encryption and login control are unrelated to field-level security; limiting documents is document-level security.Final Answer:
To restrict access to specific fields within documents -> Option AQuick Check:
Field-level security = restrict fields [OK]
- Confusing field-level with document-level security
- Thinking it encrypts data
- Assuming it controls user passwords
Solution
Step 1: Recall correct field-level security syntax
Elasticsearch uses "field_security" with a "grant" array to specify allowed fields.Step 2: Eliminate incorrect options
"deny" is not valid here; "fields" and "field_access" are incorrect keys.Final Answer:
"field_security": { "grant": ["title", "author"] } -> Option CQuick Check:
Use "field_security" with "grant" for allowed fields [OK]
- Using "deny" instead of "grant"
- Wrong key names like "fields" or "field_access"
- Confusing syntax with document-level security
{
"indices": [
{
"names": ["books"],
"privileges": ["read"],
"query": { "term": { "genre": "fiction" } },
"field_security": { "grant": ["title", "author"] }
}
]
}What documents and fields will a user with this role see when querying the
books index?Solution
Step 1: Analyze document-level security query
The "query" limits documents to those with genre 'fiction'.Step 2: Analyze field-level security grant
Only "title" and "author" fields are visible due to "field_security".Final Answer:
Only documents where genre is 'fiction' showing only 'title' and 'author' fields -> Option DQuick Check:
Query filters docs + grant limits fields = Only documents where genre is 'fiction' showing only 'title' and 'author' fields [OK]
- Ignoring the query filter on documents
- Assuming all fields are visible
- Confusing document and field level restrictions
{
"indices": [
{
"names": ["library"],
"privileges": ["read"],
"query": { "term": { "category": "science" } },
"field_security": { "grant": ["title", "summary"] }
}
]
}But users report they see all documents and fields. What is the likely error?
Solution
Step 1: Check query filter correctness
If the query filter is malformed or ignored, document filtering won't happen.Step 2: Verify field_security and privileges
Field names look correct; "read" privilege is enough for filtering; "write" or "manage" not needed.Final Answer:
The query filter is incorrect or not applied properly -> Option AQuick Check:
Query filter controls docs; if ignored, all docs show [OK]
- Assuming 'write' privilege needed for filtering
- Ignoring query filter syntax errors
- Thinking field names cause document filtering issues
status is active and see only the name and email fields. Which role definition snippet correctly implements this?Solution
Step 1: Verify document-level security query
Using "term" query on "status" with "active" correctly filters documents.Step 2: Verify field-level security syntax
"field_security" with "grant" array specifying "name" and "email" fields is correct.Step 3: Eliminate incorrect options
{ "indices": [ { "names": ["users"], "privileges": ["read"], "query": { "match": { "status": "active" } }, "field_security": { "deny": ["password"] } } ] } uses "deny" which is invalid; { "indices": [ { "names": ["users"], "privileges": ["read"], "query": { "term": { "status": "active" } }, "fields": ["name", "email"] } ] } uses wrong key "fields"; { "indices": [ { "names": ["users"], "privileges": ["read"], "query": { "term": { "status": "active" } } } ] } lacks field-level security.Final Answer:
Role with "query" term filter and "field_security" grant for "name" and "email" -> Option BQuick Check:
Use "query" for docs + "field_security" grant for fields [OK]
- Using "deny" instead of "grant" in field_security
- Using wrong keys like "fields" instead of "field_security"
- Omitting field-level security to restrict fields
