0
0
ElasticsearchComparisonBeginner · 4 min read

Index vs Type in Elasticsearch: Key Differences and Usage

In Elasticsearch, a 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.

FactorIndexType
DefinitionMain container for documentsLogical category inside an index (deprecated)
PurposeStores and organizes dataSeparates document kinds within an index
UniquenessUnique name per clusterUnique name per index
StatusCore concept, always usedDeprecated since ES 6.x, removed in ES 7.0
MappingDefines fields for all documentsDefined per type (legacy)
Usage TodayUse multiple indexes for separationAvoid 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):

json
PUT /my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "name": { "type": "text" },
        "age": { "type": "integer" }
      }
    }
  }
}

PUT /my_index/my_type/1
{
  "name": "Alice",
  "age": 30
}
Output
{ "acknowledged": true, "shards_acknowledged": true, "index": "my_index" } { "_index": "my_index", "_type": "my_type", "_id": "1", "_version": 1, "result": "created" }
↔️

Index Equivalent

Creating an index without types and adding a document (modern approach):

json
PUT /my_index
{
  "mappings": {
    "properties": {
      "name": { "type": "text" },
      "age": { "type": "integer" }
    }
  }
}

PUT /my_index/_doc/1
{
  "name": "Alice",
  "age": 30
}
Output
{ "acknowledged": true, "shards_acknowledged": true, "index": "my_index" } { "_index": "my_index", "_type": "_doc", "_id": "1", "_version": 1, "result": "created" }
🎯

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

An index is the main way to organize data in Elasticsearch.
Types are deprecated and removed; avoid using them.
Use separate indexes to separate different data categories.
Modern Elasticsearch uses a single mapping per index without types.
Always prefer the latest Elasticsearch syntax for compatibility.