Skip to content

Elasticsearch index templates

Use composable index templates to inherit settings and mappings across index patterns. Component templates, priority resolution, and migration from legacy templates.

Index templates apply settings, mappings, and aliases to indices that match a name pattern. The modern composable template system (7.8+) splits reusable building blocks (component_template) from the top-level index_template that references them — making templates inheritable, sharable across teams, and aligned with ILM. This article authors a logs index template using component templates and shows how priority resolves conflicts. Audience is operators managing many index patterns with shared settings.

How to verify

After creating templates, confirm they apply to new indices:

curl -sk -u elastic:"$PW" 'https://es:9200/_index_template?pretty'
curl -sk -u elastic:"$PW" 'https://es:9200/_component_template?pretty'
curl -sk -u elastic:"$PW" 'https://es:9200/_index_template/_simulate/logs-2026.06.02?pretty'
curl -sk -u elastic:"$PW" -X PUT 'https://es:9200/logs-test-001'
curl -sk -u elastic:"$PW" 'https://es:9200/logs-test-001/_settings,_mapping?pretty'

The simulate API shows exactly what an index named logs-2026.06.02 would inherit — settings, mappings, aliases — before you ever create it.

What’s happening

The composable template system has three layers:

  1. Component templates — reusable building blocks. Each contains a subset (settings only, mappings only, aliases only, or any combination). Multiple component templates can be applied to a single index.
  2. Index templates — top-level objects. Each defines index_patterns (the glob), priority (resolves conflicts), an optional list of composed_of component templates, and an inline template block.
  3. Data streams — abstraction over rolled indices, used by Filebeat + Fleet. Always tied to an index template that has data_stream: {}.

Priority resolves which template wins when multiple match. Higher priority wins. Built-in templates from Elastic ship with priority < 200 (often 1, 100); user templates conventionally use 200-500. Templates with the same priority but matching the same index throw an error — pin priorities explicitly.

Inheritance is shallow: composed_of templates are merged into the index template in array order, with later templates overriding earlier ones at the same key. Mappings are deep-merged at the field level.

The procedure

1. Create reusable component templates. Settings:

curl -sk -u elastic:"$PW" -X PUT 'https://es:9200/_component_template/sh-logs-settings' \
  -H 'Content-Type: application/json' \
  -d '{
    "template": {
      "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 1,
        "refresh_interval": "30s",
        "index.codec": "best_compression",
        "index.lifecycle.name": "logs-policy",
        "index.lifecycle.rollover_alias": "logs"
      }
    },
    "_meta": {"description": "Stack Harbor log indices: hot-tier settings + ILM"}
  }'

Mappings:

curl -sk -u elastic:"$PW" -X PUT 'https://es:9200/_component_template/sh-logs-mappings' \
  -H 'Content-Type: application/json' \
  -d '{
    "template": {
      "mappings": {
        "dynamic": "strict",
        "_source": {"enabled": true},
        "properties": {
          "@timestamp": {"type": "date"},
          "message":    {"type": "text"},
          "service":    {"type": "keyword"},
          "env":        {"type": "keyword"},
          "level":      {"type": "keyword"},
          "host": {
            "properties": {
              "name": {"type": "keyword"},
              "ip":   {"type": "ip"}
            }
          }
        }
      }
    }
  }'

dynamic: "strict" rejects unknown fields — the right default for production. Unknown-field issues become Logstash errors instead of mapping explosions.

2. Compose the index template:

curl -sk -u elastic:"$PW" -X PUT 'https://es:9200/_index_template/sh-logs' \
  -H 'Content-Type: application/json' \
  -d '{
    "index_patterns": ["logs-*"],
    "priority": 300,
    "composed_of": ["sh-logs-settings", "sh-logs-mappings"],
    "template": {
      "aliases": {
        "logs-search": {}
      }
    },
    "_meta": {
      "owner": "platform",
      "managed_by": "stackharbor"
    }
  }'

The top-level template block adds index-template-specific overrides. Here, every logs-* index also gets a logs-search alias.

3. Simulate before applying:

curl -sk -u elastic:"$PW" -X POST 'https://es:9200/_index_template/_simulate/logs-2026.06.02?pretty'

The output shows what an index with that name would inherit. If the result is wrong, fix the components, not the index.

4. Override per environment. A higher-priority template can match a more specific pattern:

curl -sk -u elastic:"$PW" -X PUT 'https://es:9200/_index_template/sh-logs-prod' \
  -H 'Content-Type: application/json' \
  -d '{
    "index_patterns": ["logs-prod-*"],
    "priority": 400,
    "composed_of": ["sh-logs-settings", "sh-logs-mappings"],
    "template": {
      "settings": {"number_of_replicas": 2}
    }
  }'

logs-prod-* matches both logs-* (priority 300) and logs-prod-* (priority 400). The 400 wins. The component templates are reused but the replica count overrides to 2.

5. Migrate from legacy templates (_template):

# List legacy templates
curl -sk -u elastic:"$PW" 'https://es:9200/_template?pretty'

# Recreate as composable
# ... (steps 1-2)

# Delete the legacy template
curl -sk -u elastic:"$PW" -X DELETE 'https://es:9200/_template/old-logs'

Legacy templates have lower precedence than composable templates by definition, so they’re safe to delete once the composable replacement is verified.

6. Test with a new index:

curl -sk -u elastic:"$PW" -X PUT 'https://es:9200/logs-test-001'
curl -sk -u elastic:"$PW" 'https://es:9200/logs-test-001/_settings,_mapping?pretty' \
  | jq '{settings: .[].settings.index, mapping: .[].mappings}'
curl -sk -u elastic:"$PW" -X DELETE 'https://es:9200/logs-test-001'

Operational notes

  • Templates apply at index creation only. Editing a template does NOT update existing indices. Use _settings PUT or reindex.
  • Mapping conflicts. Component templates with overlapping field definitions resolve by deep-merge; if types disagree (keyword vs text on the same field), the later component wins for that property.
  • Built-in templates from Filebeat/Elastic Agent install at priority 100-200 with their own component templates. Don’t fight them — set your priority: 300+ and override what you need.
  • Templates and ILM rollover alias. The template sets index.lifecycle.rollover_alias, but the alias itself must be bootstrapped on logs-000001 (the template only declares it; it doesn’t create the alias). See aliases and rollover.
  • _simulate_index vs _simulate — the former simulates a specific (real or hypothetical) index name; the latter simulates a template definition without creating anything.

In the engagements we run, composable templates are how we keep dozens of index patterns consistent — shared settings live in component templates, environment-specific overrides go in higher-priority index templates. Audits become tractable: “show me every index using the sh-logs-mappings component” is one API call. See /en/services/managed-operations/ for how we run multi-tenant search clusters with disciplined template management.