0
0
ElasticsearchConceptBeginner · 3 min read

What is Mapping in Elasticsearch: Definition and Examples

Mapping in Elasticsearch is like a blueprint that defines how documents and their fields are stored and indexed. It tells Elasticsearch what type each field is (like text, number, date) and how to handle it for searching and sorting.
⚙️

How It Works

Imagine you have a filing cabinet where you keep different types of documents. Mapping in Elasticsearch is like the labels and rules you put on each drawer and folder to know what kind of information is inside and how to find it quickly.

When you add data to Elasticsearch, mapping tells it whether a field is a number, text, date, or something else. This helps Elasticsearch decide how to store the data and how to search it efficiently. For example, text fields can be broken into words for searching, while numbers can be used for calculations.

Without mapping, Elasticsearch tries to guess the type automatically, but setting mapping explicitly gives you control and better search results.

💻

Example

This example shows how to create an index with a mapping that defines fields for a product catalog.

json
{
  "mappings": {
    "properties": {
      "name": { "type": "text" },
      "price": { "type": "float" },
      "available": { "type": "boolean" },
      "release_date": { "type": "date" }
    }
  }
}
Output
{ "acknowledged": true, "shards_acknowledged": true, "index": "products" }
🎯

When to Use

Use mapping when you want to control how Elasticsearch understands your data fields. This is important for accurate searching, sorting, and aggregations.

For example, if you have a date field, mapping it as date allows you to search by date ranges. If you have a price, mapping it as float lets you do numeric comparisons.

Mapping is also useful when you want to customize how text is analyzed for search, like making some fields case-insensitive or searchable by keywords.

Key Points

  • Mapping defines the data types and how fields are indexed in Elasticsearch.
  • It acts like a blueprint for storing and searching data efficiently.
  • Explicit mapping improves search accuracy and performance.
  • Without mapping, Elasticsearch guesses types but may not be optimal.
  • Mapping is essential for date, numeric, boolean, and text fields with special search needs.

Key Takeaways

Mapping tells Elasticsearch how to store and index each field in your data.
Explicit mapping improves search accuracy and performance by defining field types.
Use mapping to control how text, numbers, dates, and booleans are handled.
Without mapping, Elasticsearch guesses field types, which may not fit your needs.
Mapping is essential for advanced search features like sorting and filtering.