📖 Scenario: You are setting up an Elasticsearch index to store information about books in a library. You want to understand how Elasticsearch handles data fields automatically (dynamic mapping) and how you can control the data types explicitly (explicit mapping).
🎯 Goal: Build an Elasticsearch index with dynamic mapping enabled, then create another index with explicit mapping for the title, author, and year fields. Learn how to define mappings and see the difference between letting Elasticsearch guess the data types and specifying them yourself.
📋 What You'll Learn
Create an index called library_dynamic with dynamic mapping enabled.
Create an index called library_explicit with explicit mapping for title (text), author (keyword), and year (integer).
Add a sample document to each index with the fields title, author, and year.
Verify the mappings of both indices to observe the difference.
💡 Why This Matters
🌍 Real World
In real-world applications, controlling how data is indexed in Elasticsearch is crucial for search accuracy and performance. Dynamic mapping is convenient but can lead to unexpected data types, while explicit mapping ensures data consistency.
💼 Career
Understanding dynamic and explicit mapping is essential for roles like search engineers, backend developers, and data engineers who work with Elasticsearch to build search and analytics solutions.
Progress0 / 4 steps
1
Create the library_dynamic index with dynamic mapping
Write the Elasticsearch command to create an index called library_dynamic with dynamic mapping enabled (default behavior). Use the PUT method and specify an empty mappings object.
Elasticsearch
Hint
Dynamic mapping is enabled by default, so you can create the index with an empty mappings object.
2
Create the library_explicit index with explicit mapping
Write the Elasticsearch command to create an index called library_explicit with explicit mapping. Define the title field as text, the author field as keyword, and the year field as integer. Use the PUT method and specify the mappings with these properties.
Elasticsearch
Hint
Explicit mapping requires defining the properties object with each field and its data type.
3
Add a sample document to each index
Write the Elasticsearch commands to add a document to library_dynamic and library_explicit indices. Use the POST method to add a document with title as "The Great Gatsby", author as "F. Scott Fitzgerald", and year as 1925.
Elasticsearch
Hint
Use the POST method with /_doc to add documents to each index.
4
Verify the mappings of both indices
Write the Elasticsearch commands to get the mappings of library_dynamic and library_explicit indices. Use the GET method with /_mapping endpoint for each index.
Elasticsearch
Hint
Use the GET method with /_mapping to see the field types Elasticsearch assigned or you defined.
Practice
(1/5)
1. What is the main difference between dynamic mapping and explicit mapping in Elasticsearch?
easy
A. Dynamic mapping automatically detects field types, explicit mapping requires manual definition.
B. Dynamic mapping requires manual field type definition, explicit mapping guesses types automatically.
C. Dynamic mapping disables indexing, explicit mapping enables indexing.
D. Dynamic mapping is only for text fields, explicit mapping is only for numeric fields.
Solution
Step 1: Understand dynamic mapping
Dynamic mapping lets Elasticsearch guess the data type of fields automatically when new data arrives.
Step 2: Understand explicit mapping
Explicit mapping means you define each field's type and settings manually before indexing data.
Final Answer:
Dynamic mapping automatically detects field types, explicit mapping requires manual definition. -> Option A
Quick Check:
Dynamic = auto detect, Explicit = manual define [OK]
Hint: Dynamic guesses types; explicit means you set types yourself [OK]
Common Mistakes:
Confusing which mapping guesses types
Thinking dynamic disables indexing
Assuming explicit mapping is only for numbers
2. Which JSON snippet correctly defines an explicit mapping for a field age as an integer?
easy
A. {"mappings": {"properties": {"age": "number"}}}
B. {"mappings": {"age": {"type": "text"}}}
C. {"properties": {"age": "integer"}}
D. {"mappings": {"properties": {"age": {"type": "integer"}}}}
Solution
Step 1: Check correct mapping structure
Explicit mapping requires "mappings" with "properties" and field name with type inside.
Step 2: Verify field type for age
"age" field must have "type": "integer" to define it as integer.
Final Answer:
{"mappings": {"properties": {"age": {"type": "integer"}}}} -> Option D
Quick Check:
Correct JSON structure with integer type [OK]
Hint: Explicit mapping needs mappings > properties > field > type [OK]
Common Mistakes:
Missing 'properties' level in JSON
Using wrong type names like 'number' instead of 'integer'
Placing fields outside 'mappings'
3. Given this explicit mapping, what will be the type of the price field after indexing?
After indexing a document with "date": "not-a-date", what error will occur?
medium
A. No error, field stored as text
B. Document rejected with parsing exception
C. Field ignored silently
D. Mapping conflict error due to wrong type
Solution
Step 1: Check mapping type for date
The field "date" is explicitly mapped as "date" type, so Elasticsearch expects a valid date format.
Step 2: Indexing invalid date string
Indexing "not-a-date" will cause a parsing exception because it cannot convert the string to a date.
Final Answer:
Document rejected with parsing exception -> Option B
Quick Check:
Invalid date string causes parsing error [OK]
Hint: Invalid date string causes parsing error, not silent ignore [OK]
Common Mistakes:
Thinking field stores as text automatically
Assuming no error occurs
Believing field is ignored silently
5. You want to prevent Elasticsearch from automatically adding new fields to your index but still allow existing fields to be indexed. Which mapping setting should you use?
hard
A. "dynamic": "strict"
B. "dynamic": "true"
C. "dynamic": "false"
D. "dynamic": "runtime"
Solution
Step 1: Understand dynamic mapping options
"dynamic": "true" allows new fields to be added automatically, "false" ignores new fields, "strict" rejects documents with unknown fields.
Step 2: Choose setting to prevent new fields but allow existing
"strict" rejects documents with new fields, so only existing fields are indexed, preventing unwanted additions.
Final Answer:
"dynamic": "strict" -> Option A
Quick Check:
Use strict to block new fields, allow existing [OK]
Hint: Use dynamic: strict to block new fields but keep existing [OK]
Common Mistakes:
Using dynamic: false which ignores new fields silently
Using dynamic: true which allows new fields
Confusing runtime with dynamic setting
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.