![]() |
VOOZH | about |
Mapping types and field data types are fundamental concepts in Elasticsearch that define how data is indexed, stored and queried within an index. Understanding these concepts is crucial for effectively modeling our data and optimizing search performance.
In this article, We will learn about the mapping types, field data types, and their significance in Elasticsearch. Also, understand some examples for better understanding.
Field data types define the type of data that can be stored in a field within a document. Elasticsearch provides a wide range of data types to accommodate various types of data, including text, numbers, dates, and more. Let's explore some common field data types in Elasticsearch:
Let's create a simple index with mappings for various field data types to understand how mappings and field data types work in Elasticsearch.
PUT /my_index
{
"mappings": {
"properties": {
"title": {
"type": "text"
},
"category": {
"type": "keyword"
},
"quantity": {
"type": "integer"
},
"price": {
"type": "float"
},
"is_active": {
"type": "boolean"
},
"created_at": {
"type": "date"
}
}
}
}
Explanation:
Suppose We need to index a document into our my_index index to demonstrate how field data types are applied in Elasticsearch.
POST /my_index/_doc/1
{
"title": "Product A",
"category": "Electronics",
"quantity": 100,
"price": 49.99,
"is_active": true,
"created_at": "2022-05-01T12:00:00"
}
Explanation: In this example, we use a POST request to index a document with various field data types into the my_index index. Each field in the document corresponds to a specific field data type in Elasticsearch, such as text, keyword, integer, float, boolean, and date.
Suppose We want to retrieve mapping information for the my_index index to understand how field data types are mapped in Elasticsearch.
GET /my_index/_mappingSample Output:
{
"my_index": {
"mappings": {
"properties": {
"title": {
"type": "text"
},
"category": {
"type": "keyword"
},
"quantity": {
"type": "integer"
},
"price": {
"type": "float"
},
"is_active": {
"type": "boolean"
},
"created_at": {
"type": "date"
}
}
}
}
}
Explanation: The GET request to the _mapping endpoint retrieves the mapping information for the my_index index. This information includes the field data types for each field in the index, allowing us to understand how Elasticsearch interprets and indexes our data.
Suppose We need to query documents based on their field data types, such as finding products with a price less than 50
GET /my_index/_search
{
"query": {
"range": {
"price": {
"lt": 50
}
}
}
}
Explanation: This GET request to the _search endpoint uses a range query to find documents in the my_index index where the price field is less than 50. By specifying the field data type (float) and the range (lt: less than), we can retrieve relevant documents based on their numeric values.
Mapping types and field data types are foundational concepts in Elasticsearch that play a crucial role in organizing and querying data. By understanding mapping types and selecting appropriate field data types for your index, you can optimize search performance and ensure accurate representation of your data.
In this article, we explored the basics of mapping types, common field data types, and their significance in Elasticsearch. We also provided practical examples and outputs to illustrate how mappings and field data types are defined, indexed, and queried in Elasticsearch. With this knowledge, you'll be better equipped to design and manage Elasticsearch indices effectively for your applications.