Elasticsearch Mappings Tutorial
Introduction to Mappings
In Elasticsearch, mappings are used to define how documents and their fields are stored and indexed. They are similar to the schema in a relational database. A mapping defines:
- The data types for fields (e.g., string, integer, date).
- The configurations for various field properties (e.g., indexing options, analyzers).
Creating a Mapping
To create a mapping in Elasticsearch, you typically define it when creating an index. Here is an example of how to create an index with a mapping:
PUT /my_index { "mappings": { "properties": { "title": { "type": "text" }, "date": { "type": "date" }, "views": { "type": "integer" } } } }
This command creates an index named my_index
with fields title
, date
, and views
.
Understanding Field Data Types
Elasticsearch supports various field data types. Some common ones include:
- text: Used for full-text search.
- keyword: Used for structured search, exact values.
- integer: Numeric data without a fraction.
- float: Numeric data with a fraction.
- date: Date values.
- boolean: True or false values.
Updating Mappings
Once an index is created, you cannot change the mapping of existing fields. However, you can add new fields. Here is how you can add a new field to an existing index:
PUT /my_index/_mapping { "properties": { "author": { "type": "keyword" } } }
This command adds a new field author
to the my_index
index.
Analyzers
Analyzers are used to process text fields during indexing and searching. They break down text into tokens (terms) and optionally apply filters. Here's an example of how to define an analyzer in a mapping:
PUT /my_index { "settings": { "analysis": { "analyzer": { "my_analyzer": { "type": "standard", "stopwords": "_english_" } } } }, "mappings": { "properties": { "content": { "type": "text", "analyzer": "my_analyzer" } } } }
In this example, we create a custom analyzer named my_analyzer
and apply it to the content
field.
Dynamic vs. Explicit Mappings
Elasticsearch supports both dynamic and explicit mappings:
- Dynamic Mappings: Elasticsearch automatically detects and adds new fields to the mapping.
- Explicit Mappings: You define the mapping for each field explicitly.
By default, dynamic mapping is enabled. You can control or disable it using the dynamic
parameter:
PUT /my_index { "mappings": { "dynamic": "strict", "properties": { "title": { "type": "text" } } } }
Setting dynamic
to strict
will prevent Elasticsearch from dynamically adding new fields.
Example: Complete Mapping
Here is a complete example of an index mapping in Elasticsearch:
PUT /blog { "settings": { "number_of_shards": 1, "number_of_replicas": 1, "analysis": { "analyzer": { "english_analyzer": { "type": "standard", "stopwords": "_english_" } } } }, "mappings": { "properties": { "title": { "type": "text", "analyzer": "english_analyzer" }, "author": { "type": "keyword" }, "content": { "type": "text", "analyzer": "english_analyzer" }, "publish_date": { "type": "date" }, "views": { "type": "integer" } } } }
This example creates an index named blog
with various field mappings and custom analyzer settings.
Conclusion
Mappings in Elasticsearch are a powerful way to control how your data is indexed and searched. Understanding and configuring mappings appropriately can greatly enhance your search capabilities and performance.
We hope this tutorial has provided a comprehensive understanding of mappings in Elasticsearch. Happy indexing!