In Elasticsearch, mapping defines the data types for each field and how they are indexed and stored. Data types are a critical aspect, as they directly impact indexing methods and search performance. Elasticsearch supports various data types, which can be broadly categorized into the following types:
-
Core Datatypes:
- String types: Such as
text(for full-text search) andkeyword(for exact value search, such as filtering and aggregation). - Numeric types: Including
integer,long,short,byte,double,float,half_float,scaled_float, etc. - Date type:
date, which stores dates and times. - Boolean type:
boolean, representing true or false. - Binary type:
binary, used for storing binary data.
- String types: Such as
-
Complex Datatypes:
- Object type:
object, used for a single JSON object. - Nested type:
nested, used for JSON objects within an array, which can be indexed and searched.
- Object type:
-
Geo Datatypes:
- Geo point type:
geo_point, used for storing geographic coordinates (latitude and longitude). - Geo shape type:
geo_shape, used for storing complex shapes such as polygons.
- Geo point type:
-
Specialised Datatypes:
- IP type:
ip, used for storing IP addresses. - Completion type:
completion, used for autocomplete functionality. - Token count type:
token_count, used for counting tokens in text. - Range types: Such as
integer_range,float_range, etc., used for storing numerical ranges.
- IP type:
Example: Suppose we need to create an index for an e-commerce website to store product information. Product information includes product name, description, price, and release date. In Elasticsearch, we can design the mapping as follows:
json{ "mappings": { "properties": { "product_name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "description": { "type": "text" }, "price": { "type": "double" }, "release_date": { "type": "date", "format": "strict_date_optional_time||epoch_millis" } } } }
Here, the product_name field is of text type for full-text search and additionally defines a keyword subfield for exact search. description is also of text type, suitable for full-text search. price uses double type to store product prices. release_date uses date type, suitable for storing date information.
By selecting appropriate data types, the index structure can meet search requirements while ensuring optimal performance.