Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Field and Document Level Security in Elasticsearch
📖 Scenario: You are managing a company's employee data stored in Elasticsearch. Some information is sensitive and should only be visible to certain users. You want to control which fields and documents each user can see.
🎯 Goal: Build an Elasticsearch role with field and document level security to restrict access to employee data. You will create a sample index, define a role with specific field and document filters, and test the access by querying the data.
📋 What You'll Learn
Create an Elasticsearch index called employees with sample employee documents
Define a role called employee_viewer that restricts access to certain fields and documents
Use field level security to allow viewing only name and department fields
Use document level security to allow viewing only employees in the sales department
Query the employees index using the employee_viewer role to see the filtered results
💡 Why This Matters
🌍 Real World
Companies often need to protect sensitive data by controlling who can see what information in their databases.
💼 Career
Understanding field and document level security is important for roles like Elasticsearch administrators, security engineers, and backend developers managing data access.
Progress0 / 4 steps
1
Create the employees index with sample data
Create an Elasticsearch index called employees and add these documents exactly: {"name": "Alice", "department": "sales", "salary": 70000}, {"name": "Bob", "department": "engineering", "salary": 90000}, {"name": "Carol", "department": "sales", "salary": 65000}
Elasticsearch
Hint
Use the PUT method to create the index with mappings, then use POST to add documents.
2
Define the employee_viewer role with field and document level security
Create a role called employee_viewer that grants read access to the employees index, allows viewing only the name and department fields, and restricts documents to those where department is sales
Elasticsearch
Hint
Use field_security to specify allowed fields and query to filter documents by department.
3
Query the employees index using the employee_viewer role
Write a search query to get all documents from the employees index using the employee_viewer role. Use the GET /employees/_search API and assume the role is applied. The query should return only employees in the sales department with only the name and department fields visible.
Elasticsearch
Hint
Use a simple match_all query to retrieve documents. The role's document and field level security will filter results automatically.
4
Display the filtered search results
Print the search results from the GET /employees/_search query showing only employees in the sales department with only the name and department fields visible. The output should list the documents returned by Elasticsearch.
Elasticsearch
Hint
The output should show only Alice and Carol with their name and department fields. The salary field should not appear.
Practice
(1/5)
1. What is the main purpose of field-level security in Elasticsearch?
easy
A. To restrict access to specific fields within documents
B. To encrypt the entire Elasticsearch index
C. To limit the number of documents returned in a query
D. To control user login credentials
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 A
Quick Check:
Field-level security = restrict fields [OK]
Hint: Field-level security hides fields, not whole documents [OK]
Common Mistakes:
Confusing field-level with document-level security
Thinking it encrypts data
Assuming it controls user passwords
2. Which of the following is the correct syntax to define field-level security in an Elasticsearch role?
easy
A. "fields": ["title", "author"]
B. "field_security": { "deny": ["title", "author"] }
C. "field_security": { "grant": ["title", "author"] }
D. "field_access": { "allow": ["title", "author"] }
But users report they see all documents and fields. What is the likely error?
medium
A. The query filter is incorrect or not applied properly
B. Field names in grant are misspelled
C. Privileges should include "write" to restrict fields
D. Role must include "manage" privilege for security to work
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 A
Quick Check:
Query filter controls docs; if ignored, all docs show [OK]
Hint: Check query syntax if document filtering fails [OK]
Common Mistakes:
Assuming 'write' privilege needed for filtering
Ignoring query filter syntax errors
Thinking field names cause document filtering issues
5. You want to create a role that allows users to read only documents where status is active and see only the name and email fields. Which role definition snippet correctly implements this?