0
0
Elasticsearchquery~5 mins

Index mappings overview in Elasticsearch

Choose your learning style9 modes available
Introduction

Index mappings tell Elasticsearch how to store and understand your data. They help organize data so you can search it quickly and correctly.

When you want to define how text, numbers, or dates are stored in Elasticsearch.
When you need to control how search works on your data fields.
When you want to make sure data types are correct for filtering or sorting.
When you want to add special rules like ignoring some fields or using custom analyzers.
When setting up a new Elasticsearch index for your application.
Syntax
Elasticsearch
{
  "mappings": {
    "properties": {
      "field_name": {
        "type": "data_type"
      },
      "another_field": {
        "type": "data_type"
      }
    }
  }
}

The mappings section defines the structure of your data.

Each field_name has a type like text, keyword, date, or integer.

Examples
This example defines a simple mapping with a text field, a number, and a date.
Elasticsearch
{
  "mappings": {
    "properties": {
      "name": { "type": "text" },
      "age": { "type": "integer" },
      "joined": { "type": "date" }
    }
  }
}
Here, email is a keyword (exact match), and bio is full text searchable.
Elasticsearch
{
  "mappings": {
    "properties": {
      "email": { "type": "keyword" },
      "bio": { "type": "text" }
    }
  }
}
Sample Program

This query creates a new index called users with mappings for username, age, and signup date.

Elasticsearch
PUT /users
{
  "mappings": {
    "properties": {
      "username": { "type": "keyword" },
      "age": { "type": "integer" },
      "signup_date": { "type": "date" }
    }
  }
}
OutputSuccess
Important Notes

If you don't define mappings, Elasticsearch tries to guess data types automatically.

Defining mappings helps avoid mistakes and improves search speed.

Once data is indexed, changing mappings can be difficult, so plan ahead.

Summary

Index mappings define how Elasticsearch stores and understands your data.

They help control search, filtering, and sorting behavior.

Always define mappings before adding data for best results.