skip to content

Index & Component Templates

Templates apply settings, mappings, and aliases to every index whose name matches a pattern, so rolling indices come out consistent without manual setup. Interviews touch composable templates and priority because misordered templates are a classic cause of a field landing as the wrong type.

part ofElasticsearchoverview, primer and where to startread it →
on this pageshow

questions

6

In Elasticsearch, what does an index template configure and when is it applied?

level: juniorimportance: must knowfreq 68%

answer

  1. a recipe matched by index name
  2. settings, mappings and aliases together
  3. consulted at one moment only
  4. existing indices keep their old configuration

basics

~20 s

An index template attaches settings, mappings and aliases to any index whose name matches its index_patterns. It is applied once, at index creation time; indices that already exist keep whatever configuration they were created with.

solid answer

~50 s

An index template is a stored recipe registered with `PUT _index_template/<name>`. It holds `index_patterns` (the name patterns it claims), a `template` block containing `settings`, `mappings` and `aliases`, and optionally `composed_of`, a list of reusable component templates it stitches together. When Elasticsearch creates an index whose name matches one of the patterns, it composes the template and copies the result into that new index's metadata. That is the entire lifecycle: templates are consulted at index-creation time and never again. Editing a template does not rewrite indices that already exist and does not repair a field that was already mapped the wrong way — the new configuration lands on the next index created under the pattern, which for a rolling index family means after the next rollover and otherwise means a reindex. Templates are what keep time-series or per-tenant index families consistent without anyone remembering to pass settings on every create call.

code

bash · 16 lines
bash
PUT _index_template/logs-app
{
  "index_patterns": ["logs-app-*"],
  "priority": 500,
  "template": {
    "settings": { "number_of_shards": 3, "number_of_replicas": 1 },
    "mappings": {
      "properties": {
        "@timestamp": { "type": "date" },
        "level":      { "type": "keyword" },
        "message":    { "type": "text" }
      }
    },
    "aliases": { "logs-app": {} }
  }
}

go deeper

for a junior

Be able to say what a template holds - settings, mappings, aliases - and that it matches indices by name pattern at creation time. Knowing that it is not retroactive already puts you ahead of most screening candidates.

for a middle

Explain the mechanics: patterns match the whole name, the composed result is copied into index metadata, and the index then has no link back to the template. Know which settings can still be changed later on a live index.

for a senior

Show the operational consequence. A template fix implies a rollover or a reindex behind an alias, and you should be able to describe that cutover, including how you confirm the new indices really picked up the corrected mapping.

for a principal

Own templates as configuration-as-code: versioned in git, applied by CI, verified before the next index is born. Argue for the change-rollout policy, because every template edit has a latency measured in index lifecycles, not deploys.

## The problem templates solve Most real Elasticsearch deployments do not have one index; they have a *family* of indices with the same shape — `logs-app-2024.06.01`, `logs-app-2024.06.02`, or one index per tenant, or one per daily snapshot of a catalogue. Every one of those indices needs the same number of shards, the same analyzers, the same explicit mapping and often the same alias. Passing all of that on every create call is unworkable: indices are frequently created implicitly by an indexing request or by a rollover, where no human is present to supply a body. An **index template** is the stored answer. It says: *any index whose name matches these patterns should be born with this configuration*. ## Anatomy A composable index template is stored at `PUT _index_template/<name>` and has these parts: - `index_patterns` — one or more name patterns, e.g. `["logs-app-*"]`. Matching is against the **whole** index name, so `logs-*` does not match `app-logs-1`. - `template` — the payload, containing any of `settings` (shards, replicas, refresh interval, analysis definitions), `mappings` (field types, dynamic settings, dynamic templates) and `aliases`. - `composed_of` — an ordered list of **component template** names whose contents are merged in before the `template` block. Component templates are stored separately with `PUT _component_template/<name>` and are the reuse mechanism: a shared "base settings" component and a shared "common fields" component can be referenced by twenty index templates. - `priority` — a number used to pick a single winner when several templates match a name. - `version` and `_meta` — free-form bookkeeping fields Elasticsearch stores but does not interpret; useful for recording who owns the template and which revision is deployed. A template may also declare a `data_stream` section, which makes matching names create data streams rather than plain indices; the data-stream mechanics themselves are a separate topic. ## When it is applied — and only then Elasticsearch evaluates templates at exactly one moment: when an index is created. That creation can be explicit (`PUT /logs-app-000001`), implicit (indexing a document into a name that does not exist, if auto-create is allowed), or the result of a rollover. At that moment the engine finds the matching template, composes it, merges anything supplied in the create-index request itself on top, and writes the resulting settings, mappings and aliases into the cluster state as that index's metadata. Afterwards, the index and the template are **completely independent objects**. The index does not hold a reference to the template. Nothing re-reads the template later. This is the single most common misunderstanding in interviews, and the source of a whole class of production surprises: - You fix a field from `text` to `keyword` in the template — yesterday's index still has `text` and still fails your `terms` aggregation. - You raise `number_of_replicas` in the template — existing indices keep the old value until you change them with `PUT /<index>/_settings`. - You delete the template — every index created from it carries on unchanged. ## What a template cannot do Templates cannot perform an operation Elasticsearch does not otherwise support. Field types are immutable once an index exists, so no amount of template editing changes an existing field's type; the fix is to reindex into a fresh index (usually behind an alias so readers do not notice the swap) or, for a rolling family, to roll over and let only new data get the corrected mapping. Templates also do not validate the data you send; they only shape the index. Some settings *are* dynamically updatable on a live index (`number_of_replicas`, `refresh_interval`), so for those the template edit plus an explicit settings update on existing indices gets you a fully consistent fleet. Static settings such as `number_of_shards` can never be changed on an existing index. ## Practical consequences Because the template is only ever a birth certificate, treat it as configuration-as-code: keep the JSON in version control, apply it from CI, and give it a `version` and `_meta` so you can tell which revision an environment has. Verify that a change actually resolves the way you expect *before* the next index is born, using the simulate API, because a template mistake is only visible once the wrong index already exists, and by then correcting it costs a reindex. Finally, remember the interaction with dynamic mapping. If no template matches a new index name, the index is created with cluster defaults and Elasticsearch guesses field types from the first document it sees. A missing or mistyped `index_patterns` therefore does not fail loudly — it silently falls back to guessing, which is why a field that "used to be a keyword" suddenly shows up as `text` with a `.keyword` sub-field.

  • You corrected a field type in the template but the wrong mapping is still in production. What are your options?
    The existing index cannot change that field's type. Either reindex into a new index built from the corrected template and swap an alias over atomically, or, for a rolling family, roll over so only new backing indices get the fix and let the bad index age out. Templates alone will never repair it.
  • If the create-index request body sets a value the template also sets, which one wins?
    The request body. Templates are composed first, then anything explicitly supplied in the create-index call is merged on top and takes precedence. That is useful for one-off overrides but dangerous for automation, because it hides drift from the template that reviewers assume is authoritative.
  • What happens if an index name matches no template at all?
    The index is created with cluster defaults and no predefined mapping, so dynamic mapping infers field types from the first documents indexed. Nothing errors, which is why a typo in index_patterns produces silently wrong field types rather than an obvious failure.

saying these in an interview costs you the question

  • Claims editing a template updates mappings on existing indices
  • Thinks the template is consulted on every indexing request
  • Believes a template can change an existing field's type
  • Confuses index templates with search templates or ingest pipelines
  • Assumes deleting a template affects indices already created from it

context

open as a page

When several Elasticsearch index templates match a new index name, which one wins?

level: middleimportance: must knowfreq 62%

basics

~20 s

Exactly one composable index template is applied: the matching one with the highest priority. Elasticsearch refuses to store two templates whose index_patterns overlap at the same priority, so ties are prevented when you save the template rather than resolved at index creation.

open as a page

In what order does Elasticsearch merge composed_of component templates into an index's configuration?

level: middleimportance: should knowfreq 52%

basics

~20 s

Component templates named in composed_of are merged in array order, each overriding the previous one on conflicts. The index template's own template block is applied last and beats them all, and anything supplied in the create-index request overrides even that.

open as a page

How do you verify which Elasticsearch template will apply to an index before it is created?

level: middleimportance: should knowfreq 44%

basics

~20 s

Call POST _index_template/_simulate_index/<name>. Elasticsearch resolves that name against every stored template and returns the settings, mappings and aliases the index would actually receive, plus an overlapping list naming the templates that matched but lost on priority.

open as a page

How do legacy _template definitions interact with composable _index_template definitions?

level: seniorimportance: should knowfreq 33%

basics

~20 s

Composable templates win outright: if any composable index template matches a new index name, Elasticsearch ignores every legacy _template for that index. Legacy templates are deprecated, merge all matches by order, and cannot back data streams.

open as a page

How would you structure Elasticsearch component and index templates for many teams and index families?

level: principalimportance: should knowfreq 26%

basics

~20 s

Layer a small number of semantic component templates - platform defaults, shared schema, per-team fields, an override slot - and give each family one narrow index template with a priority from a documented band. Keep all of it in version control and gate changes on simulated output.

open as a page