Index vs Type in Elasticsearch: Key Differences and Usage
index is a collection of documents that share similar characteristics and is the main container for data storage. A type was a way to logically separate different kinds of documents within an index, but it is now deprecated and removed in recent versions, so index is the primary way to organize data.Quick Comparison
Here is a quick side-by-side comparison of index and type in Elasticsearch.
| Factor | Index | Type |
|---|---|---|
| Definition | Main container for documents | Logical category inside an index (deprecated) |
| Purpose | Stores and organizes data | Separates document kinds within an index |
| Uniqueness | Unique name per cluster | Unique name per index |
| Status | Core concept, always used | Deprecated since ES 6.x, removed in ES 7.0 |
| Mapping | Defines fields for all documents | Defined per type (legacy) |
| Usage Today | Use multiple indexes for separation | Avoid using types; use one mapping per index |
Key Differences
An index in Elasticsearch is like a database in a traditional system. It holds all the documents and their data. Each index has its own settings and mappings that define how data is stored and searched.
A type was used to group different document categories inside a single index, similar to tables in a database. However, this caused confusion and technical issues, so Elasticsearch deprecated types starting from version 6 and completely removed them in version 7.
Now, you should create separate indexes for different data categories instead of using types. This simplifies data management and avoids conflicts in field definitions.
Code Comparison
Creating an index with a type and adding a document (legacy approach):
PUT /my_index
{
"mappings": {
"my_type": {
"properties": {
"name": { "type": "text" },
"age": { "type": "integer" }
}
}
}
}
PUT /my_index/my_type/1
{
"name": "Alice",
"age": 30
}Index Equivalent
Creating an index without types and adding a document (modern approach):
PUT /my_index
{
"mappings": {
"properties": {
"name": { "type": "text" },
"age": { "type": "integer" }
}
}
}
PUT /my_index/_doc/1
{
"name": "Alice",
"age": 30
}When to Use Which
Choose index as your main data container in Elasticsearch for all current and future projects. Avoid using types because they are deprecated and removed in recent versions. If you need to separate data logically, create multiple indexes instead of relying on types. This approach is simpler, more robust, and fully supported.
Key Takeaways
index is the main way to organize data in Elasticsearch.Types are deprecated and removed; avoid using them.