What happens when you realize a critical microservice logs its response latency or tenant ID inside an unparsed raw text string, right when you need a 30-day executive audit dashboard? With Kibana & Elasticsearch Runtime Fields ("Schema on Read"), you can extract, cast, and aggregate that metric immediately—without reindexing terabytes of historical data.
1. Schema on Write vs Schema on Read in Elasticsearch
Traditionally, Elasticsearch relies on Schema on Write: documents are parsed and indexed according to a strict mapping at ingestion time. While this delivers blazing-fast queries, it becomes a bottleneck if a field was mapped with the wrong type (e.g., keyword instead of double) or omitted from your Logstash/Ingest pipeline.
Runtime Fields bring Schema on Read: the field takes zero disk space in the inverted index and is evaluated dynamically at query execution time.
2. Practical Example: Computing SLA Compliance with Painless
Directly from your Kibana Data View (or index mapping), you can define a Runtime Field backed by a Painless script or a dissect / grok pattern.
Here is how to create a dynamic sla_compliance field of type keyword based on event duration:
if (doc['event.duration'].size() == 0) {
emit('Unknown');
return;
}
double durationMs = doc['event.duration'].value / 1000000.0;
if (durationMs < 200) {
emit('Compliant (< 200ms)');
} else if (durationMs < 1000) {
emit('Degraded (200ms - 1s)');
} else {
emit('Breached (> 1s)');
}
Once saved, sla_compliance appears in Discover and Lens just like any indexed field. You can filter on it via KQL and group by its values in charts immediately.
3. Best Practices to Protect Cluster Performance
- Prefer
doc['field'].valueoverparams._source: Reading columnar doc_values is orders of magnitude faster than loading and parsing the raw JSON_sourcefrom disk. - Combine with indexed KQL filters first: When your query narrows the document set using indexed fields (e.g.,
service.name : "checkout-api"), Elasticsearch only executes the Runtime Field script on the matching subset. - Hybrid Lifecycle Strategy: Use a Runtime Field to unblock urgent analysis on historical data today, and add the field to your ingest pipeline for future index rollovers. Elasticsearch will seamlessly query the indexed field on newer indices and fall back to the Runtime Field on older indices!