Elasticsearch Indexes and Mappings
Mapping is schema for index: defines field types, analyzers, and how fields are indexed for search.
Create Index with Mapping
PUT /products
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2,
"index.analysis": {
"analyzer": {
"custom_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase", "stop"]
}
}
}
},
"mappings": {
"properties": {
"id": { "type": "keyword" },
"name": {
"type": "text",
"analyzer": "custom_analyzer",
"fields": {
"raw": { "type": "keyword" }
}
},
"description": { "type": "text" },
"price": { "type": "scaled_float", "scaling_factor": 100 },
"stock": { "type": "integer" },
"category": { "type": "keyword" },
"created_at": { "type": "date" },
"tags": { "type": "keyword" },
"location": { "type": "geo_point" }
}
}
}
Common Field Types
- text — analyzed text (search support)
- keyword — exact match (used for filtering, aggregation)
- integer, long — numbers
- double, scaled_float — decimals
- date — date/time
- boolean — true/false
- geo_point — coordinates
- nested — array of objects
- object — embedded object
Reindex with Mapping Change
# Create new index with new mapping
PUT /products_v2
{ ... new mapping ... }
# Reindex data from old to new
POST /_reindex
{
"source": { "index": "products" },
"dest": { "index": "products_v2" },
"script": {
"source": "ctx._source.new_field = 'default'"
}
}
# Switch alias
POST /_aliases
{
"actions": [
{ "remove": { "index": "products", "alias": "products_alias" } },
{ "add": { "index": "products_v2", "alias": "products_alias" } }
]
}







