What is mapping in Elasticsearch and why does it matter?
Learn what Elasticsearch mapping is, how field types like text and keyword affect search, dynamic vs explicit mapping, reindexing and interview tips.
Expected Interview Answer
Mapping is the schema of an Elasticsearch index: it defines each field's data type and how that field is stored, indexed, and analyzed, which directly controls how you can search, sort, and aggregate on it.
Mapping decides whether a field is treated as full-text (text) or an exact value (keyword, numeric, date, boolean), which analyzer applies, and whether the field is indexed at all. If you rely on dynamic mapping, Elasticsearch guesses types from the first document, which can misclassify a date as text or make a number un-aggregatable, so production indices usually define explicit mappings up front. Most field types cannot be changed after creation because it would require re-analyzing existing data, so getting mapping right early, or reindexing into a new index, is essential for correct and performant search.
- Controls field data types and analysis
- Enables correct sorting, filtering and aggregations
- Distinguishes full-text from exact-match fields
- Prevents dynamic mapping from guessing wrong types
- Improves storage and query performance
AI Mentor Explanation
Mapping is like the column definitions of a scorebook that declare each field's kind: runs are numbers you can total, a batter's name is exact text you filter on, and the date is a real date you can sort by. If you left the book to guess, it might treat an over count as loose text and you could no longer add up totals, which is why the format is fixed before the first ball is recorded.
Step-by-Step Explanation
Step 1
Decide field types
Choose text for full-text search and keyword, numeric, date or boolean for exact values.
Step 2
Define explicit mapping
Create the index with a mappings block instead of relying on dynamic guessing for production data.
Step 3
Pick analyzers
Assign analyzers to text fields so indexing and search tokenize consistently.
Step 4
Add multi-fields if needed
Give a text field a keyword sub-field so you can both search and sort or aggregate on it.
Step 5
Reindex to change types
Since most types are immutable, create a new index with the corrected mapping and reindex existing data into it.
What Interviewer Expects
- Mapping as the index schema defining field types
- Difference between text and keyword fields
- Awareness of dynamic vs explicit mapping
- Knowing most field types are immutable after creation
- Understanding reindexing to change a mapping
Common Mistakes
- Relying on dynamic mapping for production and getting wrong types
- Trying to change an existing field's type in place
- Not adding a keyword sub-field, then failing to sort on text
- Confusing mapping with settings like shards and replicas
- Assuming a numeric string will aggregate like a number
Best Answer (HR Friendly)
“Mapping is the blueprint that tells Elasticsearch what kind of data each field holds, such as text, numbers, or dates. It matters because the right types make search, sorting, and totals work correctly, and getting it wrong often means rebuilding the index later.”
Code Example
PUT /products
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"raw": { "type": "keyword" }
}
},
"price": { "type": "scaled_float", "scaling_factor": 100 },
"inStock": { "type": "boolean" },
"createdAt": { "type": "date" }
}
}
}Follow-up Questions
- What is the difference between text and keyword field types?
- Why can't you change most field types after index creation?
- What is a multi-field and when would you use one?
- How does dynamic mapping decide field types?
- How do you migrate data when a mapping must change?
MCQ Practice
1. Which field type is best for exact-match filtering and aggregations?
keyword stores the value un-analyzed, making it ideal for exact filters, sorting and aggregations.
2. You need to change a field's type on an existing index. What is the usual approach?
Most field types are immutable, so you create a new index with the correct mapping and reindex the data into it.
3. What is a risk of relying only on dynamic mapping in production?
Dynamic mapping guesses types from the first document and can misclassify fields, breaking sorting or aggregations.
Flash Cards
What is mapping? — The schema of an index defining each field's type and how it is stored, indexed and analyzed.
text vs keyword? — text is analyzed for full-text search; keyword is exact for filtering, sorting and aggregations.
Can you change a field type? — Usually no; most types are immutable, so you reindex into a new index.
What is a multi-field? — One field indexed multiple ways, e.g. text plus a keyword sub-field for sorting.
Continue Learning
Related Interview Questions
What is the difference between keyword and text field types in Elasticsearch?
medium
What is the difference between a term query and a match query in Elasticsearch?
medium
How does Elasticsearch handle full-text search vs exact matching?
medium
What is an alias in Elasticsearch and how does it help with reindexing?
medium