In Elasticsearch, mapping defines how to process data types for individual fields within documents. Mapping is similar to table structure definitions in databases, as it describes the field names, data types, and how fields are indexed.
Key Steps in Handling Mapping:
-
Define Data Types: Elasticsearch supports various data types, such as integers (integer), floats (float), strings (text and keyword), booleans (boolean), and dates (date), etc. To correctly index and search data, it is essential to specify the correct data type for each field in the document.
-
Auto-detection: Without predefined mappings, Elasticsearch can automatically detect the data types of input. For example, if a field contains numbers, Elasticsearch can identify it as an integer or float. While convenient, auto-detection may not always meet specific data requirements.
-
Explicit Mapping: To precisely control the indexing process, you can explicitly define mappings for documents. This can be achieved using Elasticsearch's PUT mapping API, where you can specify the data type for each field, whether it is indexed, and other specific indexing attributes.
Example: Suppose we have product information from an e-commerce platform; we can define the following mapping for these products:
jsonPUT /products { "mappings": { "properties": { "product_id": { "type": "integer" }, "name": { "type": "text" }, "price": { "type": "float" }, "in_stock": { "type": "boolean" }, "created_at": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss" } } } } -
Updating Mappings: Once a field's mapping is defined, modifying the mapping type of existing fields is typically not allowed, as it may lead to data inconsistencies. If modification is necessary, the common approach is to reindex the data into a new index with the updated mapping definition.
-
Dynamic Mapping Control: You can control the mapping behavior for fields not explicitly defined by setting dynamic mapping rules for the index. For example, you can configure all new unknown string fields to be automatically mapped as keyword instead of text, or completely disable dynamic mapping.
jsonPUT /my_index { "mappings": { "dynamic": "strict", // or "true" or "false" "properties": { // Define known fields } } }
Summary
Mapping is a crucial part of Elasticsearch's architecture. Correct mappings ensure that data is properly indexed and effectively queried. By appropriately using explicit mappings, controlling dynamic mappings, and reindexing data when necessary, you can efficiently manage the data structure in Elasticsearch.