0
0
Elasticsearchquery~5 mins

Component templates in Elasticsearch

Choose your learning style9 modes available
Introduction

Component templates help you reuse settings and mappings for your Elasticsearch indexes. They save time and keep your index setup consistent.

When you want to apply the same settings or mappings to many indexes.
When you need to update common index configurations easily in one place.
When you want to organize index templates into smaller reusable parts.
When managing multiple indexes with similar structures in Elasticsearch.
Syntax
Elasticsearch
{
  "template": {
    "settings": { ... },
    "mappings": { ... },
    "aliases": { ... }
  }
}

The template object contains the settings, mappings, and aliases you want to reuse.

You create component templates separately and then combine them in index templates.

Examples
A simple component template with one shard and a text field called name.
Elasticsearch
{
  "template": {
    "settings": {
      "number_of_shards": 1
    },
    "mappings": {
      "properties": {
        "name": { "type": "text" }
      }
    }
  }
}
A component template that only sets the refresh interval for indexes.
Elasticsearch
{
  "template": {
    "settings": {
      "refresh_interval": "1s"
    }
  }
}
Sample Program

This example creates a component template named my_component_template with 2 replicas and mappings for user and message fields. Then it retrieves the template to show its content.

Elasticsearch
PUT _component_template/my_component_template
{
  "template": {
    "settings": {
      "number_of_replicas": 2
    },
    "mappings": {
      "properties": {
        "user": { "type": "keyword" },
        "message": { "type": "text" }
      }
    }
  }
}

GET _component_template/my_component_template
OutputSuccess
Important Notes

You can combine multiple component templates into an index template for flexible index creation.

Component templates make it easier to manage large Elasticsearch setups by reusing common parts.

Summary

Component templates store reusable index settings and mappings.

They help keep index configurations consistent and easy to update.

You create component templates separately and then use them in index templates.