Index mappings overview in Elasticsearch - Time & Space Complexity
When we create index mappings in Elasticsearch, we define how data is stored and searched. Understanding time complexity helps us see how the setup work grows as we add more fields or documents.
We want to know how the time to create or update mappings changes when the index size or field count increases.
Analyze the time complexity of this index mapping creation snippet.
PUT /my-index
{
"mappings": {
"properties": {
"name": { "type": "text" },
"age": { "type": "integer" },
"email": { "type": "keyword" }
}
}
}
This code creates an index with three fields and their types defined.
Look at what repeats when processing mappings.
- Primary operation: Processing each field's definition in the mapping.
- How many times: Once for each field in the mapping.
As you add more fields, the work to process mappings grows.
| Input Size (fields) | Approx. Operations |
|---|---|
| 10 | 10 operations |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: The time grows directly with the number of fields.
Time Complexity: O(n)
This means the time to process mappings grows linearly with the number of fields defined.
[X] Wrong: "Adding more fields does not affect mapping processing time much."
[OK] Correct: Each field requires separate processing, so more fields mean more work and longer processing time.
Knowing how mapping size affects processing helps you design efficient indexes and answer questions about scaling Elasticsearch setups confidently.
"What if we added nested fields inside the mapping? How would that change the time complexity?"
Practice
Solution
Step 1: Understand the role of index mappings
Index mappings tell Elasticsearch how to store and interpret each field in your data.Step 2: Compare options with this role
Only To define how data fields are stored and searched matches this purpose; others relate to unrelated tasks.Final Answer:
To define how data fields are stored and searched -> Option DQuick Check:
Index mappings = data structure definition [OK]
- Confusing mappings with user management
- Thinking mappings handle backups
- Assuming mappings monitor server health
Solution
Step 1: Identify the correct field type for text data
Text fields in Elasticsearch use the type "text" for full-text search.Step 2: Match the option with the correct type
"name": { "type": "text" } correctly uses "text"; others use types for keywords, numbers, or dates.Final Answer:
"name": { "type": "text" } -> Option AQuick Check:
Text fields = type "text" [OK]
- Using "keyword" for full-text search fields
- Assigning numeric or date types to text data
- Missing quotes around field names
{ "properties": { "age": { "type": "integer" }, "name": { "type": "keyword" } } }What will happen if you try to search for documents with
name containing partial words?Solution
Step 1: Understand keyword field behavior
Keyword fields store exact values and do not support partial or full-text search.Step 2: Analyze the search behavior for partial words
Sincenameis keyword type, partial matches will fail; only exact matches work.Final Answer:
Partial word matches will not work because keyword fields are exact values -> Option BQuick Check:
Keyword = exact match only [OK]
- Assuming keyword fields support partial matching
- Expecting syntax errors from valid mappings
- Confusing integer field with text search behavior
{ "properties": { "date": { "type": "text" } } }What is the problem with this mapping if you want to filter by date range?
Solution
Step 1: Identify the correct type for date fields
Date range filtering requires the field to be of type "date" in Elasticsearch.Step 2: Check the given mapping type
The mapping uses "text" type, which is not suitable for date operations like range filtering.Final Answer:
The type should be "date" to enable date range filtering -> Option CQuick Check:
Date filtering needs type "date" [OK]
- Using text type for date fields
- Thinking field names are reserved keywords
- Adding index false disables searching, not filtering
username (exact match), bio (full-text search), and signup_date (date range queries). Which mapping correctly defines these fields?Solution
Step 1: Match each field to its correct type
Username needs exact matches, so "keyword" is correct. Bio needs full-text search, so "text" is correct. Signup_date needs date range queries, so "date" is correct.Step 2: Verify the options
{ "properties": { "username": { "type": "keyword" }, "bio": { "type": "text" }, "signup_date": { "type": "date" } } } matches all these requirements correctly. Others mix types incorrectly.Final Answer:
{ "properties": { "username": { "type": "keyword" }, "bio": { "type": "text" }, "signup_date": { "type": "date" } } } -> Option AQuick Check:
Correct types for exact, full-text, and date fields [OK]
- Mixing keyword and text types incorrectly
- Using integer for date fields
- Confusing full-text and exact match types
Ready to prove it?
Pass the test and this pattern moves unknown
