Elasticsearch Index Mapping: What It Is and How to Configure It
Fast recall
Quick revision
Core answer
Whether you understand the difference between text and keyword field types, the role of analyzers in text fields, and why you should always define explicit mappings in production.
Using dynamic mapping in production and discovering that Elasticsearch inferred a field as text when you needed keyword (for exact-match filtering or aggregations), or vice versa.
Explain multi-fields -- mapping a single field as both text (for full-text search) and keyword (for sorting and aggregations) simultaneously.
Interview-ready guide
Interview answer
An Elasticsearch index mapping defines how documents are structured and how each field is indexed — which fields are searchable, which are filterable, and what data type is used for each.
Dynamic vs explicit mapping
- Without an explicit mapping, Elasticsearch infers types from the first document — a string field gets both
text(analyzed, full-text searchable) andkeyword(exact match, sortable) sub-fields by default - Dynamic mapping is convenient for prototyping but risky in production — if a malformed document is indexed first, the wrong type is inferred and subsequent correct documents may fail or store incorrectly
- Define explicit mappings before indexing production data:
PUT /products/_mapping { "properties": { "name": { "type": "text" }, "status": { "type": "keyword" } } }
`text` vs `keyword` — the critical distinction
textfields are analyzed — the analyzer tokenizes the value and indexes each token, which meansmatchqueries work for partial matches and the text is not suitable for sorting or aggregationkeywordfields are stored as-is — used for exact match (termqueries), filtering, sorting, and aggregations;status = "active"must be akeywordfield, nottext- The common mistake: mapping a field that you'll filter and aggregate on (like
status,category,user_id) astextinstead ofkeyword; text fields can't be used for exact filtering or aggregations
Index settings — analysis and performance
number_of_shardsis set at index creation and cannot be changed — choose based on expected document count; a shard that grows beyond 50GB performs poorly; plan for 20-30GB per shard as a targetnumber_of_replicascan be changed anytime — 1 replica per primary is the standard for production; replicas serve read requests and provide failover
Changing a field's mapping after indexing requires re-indexing the data — the _reindex API copies documents from one index to another with the new mapping applied.
Elasticsearch Index Mapping: What It Is and How to Configure It
Direct Answer
An Elasticsearch index mapping defines the schema for documents in that index -- the field names, their data types (text, keyword, date, integer, nested, etc.), and how each field is analyzed at index and query time. Configuring mapping explicitly prevents dynamic mapping from making wrong type inferences, ensures efficient storage, and controls which fields are searchable, sortable, or aggregatable.
Code Example
PUT /products
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1,
"analysis": {
"analyzer": {
"product_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase", "stop", "snowball"]
}
}
}
},
"mappings": {
"properties": {
"id": { "type": "long" },
"name": {
"type": "text",
"analyzer": "product_analyzer",
"fields": {
"keyword": { "type": "keyword", "ignore_above": 256 }
}
},
"description": { "type": "text", "analyzer": "product_analyzer" },
"category": { "type": "keyword" },
"price": { "type": "double" },
"stock": { "type": "integer" },
"tags": { "type": "keyword" },
"created_at": { "type": "date", "format": "yyyy-MM-dd'T'HH:mm:ssZ" },
"attributes": { "type": "nested" }
}
}
}
Common Mistakes & Pitfalls
Using dynamic mapping in production and discovering that Elasticsearch inferred a field as text when you needed keyword (for exact-match filtering or aggregations), or vice versa.
Check your understanding
Answer these without looking back at the article.
- What is the difference between a text field and a keyword field in Elasticsearch?
- What are multi-fields and when would you use them?
- How do you reindex data in Elasticsearch when you need to change a field mapping?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.