Mappings tell Elasticsearch how to understand and organize your data. They define the structure of documents so Elasticsearch can store and search them correctly.
0
0
Why mappings define document structure in Elasticsearch
Introduction
When you want to control how data fields are stored and searched in Elasticsearch.
When you need to specify data types like text, numbers, or dates for your documents.
When you want to improve search speed by defining exact field formats.
When you want to avoid errors by telling Elasticsearch what to expect in your data.
When you want to customize how Elasticsearch indexes and analyzes your data.
Syntax
Elasticsearch
{
"mappings": {
"properties": {
"field_name": {
"type": "data_type"
},
"another_field": {
"type": "data_type"
}
}
}
}The mappings section defines the structure of your documents.
Each field_name has a type that tells Elasticsearch what kind of data it holds.
Examples
This mapping defines a document with a text field for name, an integer for age, and a date for joined.
Elasticsearch
{
"mappings": {
"properties": {
"name": { "type": "text" },
"age": { "type": "integer" },
"joined": { "type": "date" }
}
}
}This mapping defines a product with a price as a float number and a boolean to show if it is in stock.
Elasticsearch
{
"mappings": {
"properties": {
"price": { "type": "float" },
"in_stock": { "type": "boolean" }
}
}
}Sample Program
This example creates an index called my_index with a mapping that defines three fields: title, pages, and published. Then it retrieves the mapping to show the structure.
Elasticsearch
PUT /my_index
{
"mappings": {
"properties": {
"title": { "type": "text" },
"pages": { "type": "integer" },
"published": { "type": "date" }
}
}
}
GET /my_index/_mappingOutputSuccess
Important Notes
Without mappings, Elasticsearch guesses field types, which can cause mistakes.
Defining mappings helps Elasticsearch search faster and more accurately.
You cannot change field types easily after data is indexed, so plan mappings carefully.
Summary
Mappings define how Elasticsearch understands your document fields.
They improve search accuracy and speed by specifying data types.
Always set mappings before adding data to avoid errors and confusion.