Webhook icon
Schedule icon
If icon
Fail icon
Request icon
Script icon
Process icon
SlackIncomingWebhook icon
Set icon
Pause icon
Loop icon
Sequential icon

Stop Dynamic Mapping From Exhausting Elasticsearch Field Limits

Detect Elasticsearch dynamic mapping explosions before the field limit rejects documents, then fix the mapping with approval.

Categories
DataInfrastructure

Elasticsearch maps every new key it sees in a document. When the keys come from users (attribute names, tags, form fields), the mapping keeps growing until the index hits index.mapping.total_fields.limit (1000 by default). From then on, documents with a new key are rejected with Limit of total fields [1000] has been exceeded, and ingestion stops for every producer that sends them.

This blueprint checks the mappings on a schedule and on a webhook. It finds the object where new fields keep appearing, tells on-call which object it is and how many keys it has, and offers to make that one object dynamic: false (or flattened for new indices) in the template and in the write index.

How it works

  1. explosion_reported (Webhook) or hourly_check (Schedule, off by default) starts a run.
  2. validate_inputs (If) rejects inconsistent settings. check_cluster (retries) fails fast when the cluster is unreachable or the credentials are rejected.
  3. fetch_mappings reads _mapping for the pattern and fetch_limits reads each index's total_fields.limit (with the cluster default).
  4. analyze (Python) counts every object, leaf and multi-field per index, as the limit does, and compares the paths with the baseline in the KV store. Each new path is assigned to the deepest object above it that gained object_growth_min mapped fields; new keys at the top level of the document are grouped as (root). The baseline is stored per cluster and index pattern (es_mapping_baseline_<sha256 of es_url and index_pattern>), so two copies of the flow never share it.
  5. alert_change (If) posts one Slack message per change: the index, the level, the top offending objects and sample keys.
  6. store_baseline (KV) advances the baseline after the alert went out.
  7. decide (If) asks on-call when an object gained enough new fields. wait_for_decision (Pause, 2 hours) takes the approval and the template mode.
  8. On approval, template_patch (If) changes the object in the index template, write_mapping (Loop) sets dynamic: false on each target index, and rollover (If) rolls the write alias over if the input says so.
  9. recheck_mappings and recheck (Python) read the counts and the object's state again and report what changed. notify_outcome posts the result.

Error handling

  • Settings are checked before any call. Warn below critical, a non-empty pattern, and a write alias for rollover.
  • Errors carry the cause. Every HTTP response is checked for an error status, and the Elasticsearch error.reason is shown in the failure. A wrong password says so, and the errors alert names the task.
  • Network calls retry with exponential backoff.
  • Alerts once per change. The baseline advances only after the alert is sent, so a failed Slack call is retried by the next run instead of being lost.
  • Each index is isolated. A rejected _mapping update inside the loop does not stop the other indices, and the recheck reports the real state, not the request status.
  • Nothing changes by default. No answer within two hours resumes the pause with the defaults, and the run posts that nothing changed.

What this blueprint teaches about Kestra

  • A Pause whose onResume inputs choose both the approval and a mode for the template fix.
  • A Loop with an isolated Sequential per index, and a recheck that reads the state back instead of trusting the HTTP status.
  • A KV store baseline that makes alerts edge-triggered: the flow reports a change, not a state.
  • A Python step using the standard library only, with the Process runner, for logic that is awkward in Pebble.

Prerequisites

  • Elasticsearch 8.x with the REST API reachable from Kestra (tested on 8.15.3).
  • A user that can read _mapping and _settings, _alias, and _index_template; write _mapping and _index_template for the fix; and call _rollover if you enable it.
  • A Slack incoming webhook.

Secrets

  • ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD: basic auth for the cluster. Use a user with the privileges above.
  • SLACK_WEBHOOK_URL: Slack incoming webhook.
  • ES_MAPPING_GUARD_WEBHOOK_KEY: a long random string, the key in the webhook URL.

Inputs

  • es_url (STRING, default http://localhost:9200): cluster base URL.
  • index_pattern (STRING, default user-events-*): indices to watch.
  • field_warn_percent, field_critical_percent (FLOAT, default 70 / 90): share of the field limit.
  • object_growth_min (INT, default 20): mapped fields added under one object needed to name it.
  • write_alias, index_template (STRING, optional): the write alias, and the template to patch.
  • rollover_on_approval (BOOLEAN, default false): roll the write alias over after an approved fix.
  • kestra_url (STRING, default http://localhost:8080): Kestra UI base for the Slack links.

Outputs

  • indices (JSON): per index, the field count, limit, percentage, level, new fields since the last run, and the offending objects. Set on successful runs only.

Quick start

  1. Add the secrets and the Slack webhook. Set es_url, index_pattern, and write_alias.
  2. Run once. The first run stores the baseline and stays quiet unless an index is already at a warning level.
  3. Index a document with a new key under a user-controlled object. The next run names that object in Slack.
  4. Approve on the execution page, or wait two hours to take no action.

How to read the result

  • level is warn or critical for the share of the field limit. The count includes every object, leaf and multi-field, which is what the limit counts.
  • new_fields is the number of mapped fields added since the last run, including keyword sub-fields. An alert names an object only when it gained at least object_growth_min of them.

How to extend

  • Use a flattened template mode for every new index, or a dynamic template that maps user keys to flattened.
  • Replace the Slack step with an incident tool webhook, and keep the same message.
  • Read the mappings from a Git-tracked mapping file and compare it with the cluster to alert on drift as well.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.