📖 Scenario: You are setting up a simple search index for a small online bookstore. You want to organize the data so Elasticsearch knows how to handle each piece of information about books.
🎯 Goal: Create an Elasticsearch index mapping that defines the structure for storing book information, including title, author, publication year, and price.
📋 What You'll Learn
Create an index mapping named bookstore.
Define fields: title as text, author as keyword, year as integer, and price as float.
Use the mappings property to specify the field types.
Ensure the mapping is valid JSON for Elasticsearch.
💡 Why This Matters
🌍 Real World
Index mappings help Elasticsearch understand how to store and search your data efficiently, like organizing books in a library by title, author, and year.
💼 Career
Knowing how to define index mappings is essential for roles involving search engine setup, data indexing, and backend data management.
Progress0 / 4 steps
1
Create the index mapping structure
Create a JSON object called bookstore_mapping with a mappings key containing an empty properties object.
Elasticsearch
Hint
Start by creating the basic structure for the index mapping with empty properties.
2
Add field definitions to the mapping
Inside bookstore_mapping["mappings"]["properties"], add the fields title as text, author as keyword, year as integer, and price as float.
Elasticsearch
Hint
Define each field with its correct type inside the properties object.
3
Add a setting for dynamic mapping
Add a dynamic key with value false inside bookstore_mapping["mappings"] to prevent Elasticsearch from adding fields automatically.
Elasticsearch
Hint
Place the dynamic setting alongside properties inside mappings.
4
Complete the index mapping JSON
Ensure the entire bookstore_mapping JSON object includes dynamic set to false and the properties with all four fields exactly as specified.
Elasticsearch
Hint
Review the entire JSON to confirm all parts are included and correct.
Practice
(1/5)
1. What is the main purpose of index mappings in Elasticsearch?
easy
A. To monitor server health
B. To create user accounts for Elasticsearch
C. To backup data automatically
D. To define how data fields are stored and searched
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 D
Quick Check:
Index mappings = data structure definition [OK]
Hint: Mappings control data structure and search behavior [OK]
Common Mistakes:
Confusing mappings with user management
Thinking mappings handle backups
Assuming mappings monitor server health
2. Which of the following is the correct way to define a text field in an Elasticsearch mapping?
easy
A. "name": { "type": "text" }
B. "name": { "type": "keyword" }
C. "name": { "type": "integer" }
D. "name": { "type": "date" }
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 A
Quick Check:
Text fields = type "text" [OK]
Hint: Use "text" type for searchable text fields [OK]
What will happen if you try to search for documents with name containing partial words?
medium
A. Partial word matches will work because keyword fields support full-text search
B. Partial word matches will not work because keyword fields are exact values
C. Elasticsearch will throw a syntax error
D. Partial word matches will work only if age is also searched
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
Since name is 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 B
Quick Check:
Keyword = exact match only [OK]
Hint: Keyword fields do exact matches only, no partial search [OK]
Common Mistakes:
Assuming keyword fields support partial matching
Expecting syntax errors from valid mappings
Confusing integer field with text search behavior
4. You defined this mapping:
{ "properties": { "date": { "type": "text" } } }
What is the problem with this mapping if you want to filter by date range?
medium
A. The field name "date" is reserved and cannot be used
B. There is no problem; text type works for date filtering
C. The type should be "date" to enable date range filtering
D. You must add "index": false to filter by date
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 C
Quick Check:
Date filtering needs type "date" [OK]
Hint: Use type "date" for date range filters [OK]
Common Mistakes:
Using text type for date fields
Thinking field names are reserved keywords
Adding index false disables searching, not filtering
5. You want to store a user's profile with fields: username (exact match), bio (full-text search), and signup_date (date range queries). Which mapping correctly defines these fields?
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.