AI Hints
Home›Elasticsearch Tutorial›RAG, Ingest Pipelines and Runtime Fields
Elasticsearch · Lesson 105 of 120

Runtime Fields

Fields made at query time.

⏱ 2 min read▶ Video at 1:38:58🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

The team wants to group products into cheap and premium. That field doesn't exist, and reindexing takes time. A runtime field calculates it with a small script, at search time.

  • Need a new field
  • No reindex
  • Runtime field

A price band, on the fly

Runtime mappings defines price band, a keyword made by a script. Emit returns cheap below fifty dollars, and premium otherwise. Then a terms aggregation groups by it, like any real field. Run it. Seven cheap products, and forty two premium.

Dev Tools Consoletext
GET products/_search?filter_path=aggregations.bands.buckets
{
  "size": 0,
  "runtime_mappings": {
    "price_band": { "type": "keyword", "script":
      "emit(doc['price'].value < 50 ? 'cheap' : 'premium')" } },
  "aggs": { "bands": { "terms": { "field": "price_band" } } }
}
Output
{
  "aggregations": {
    "bands": {
      "buckets": [
        {"key": "premium", "doc_count": 42},
        {"key": "cheap", "doc_count": 7}
      ]
    }
  }
}
Also uses _load
catalog

Runtime fields

They're great for trying out a field, or fixing a mistake without reindexing. They're slower than indexed fields, because the script runs on every search. Once a runtime field proves useful, index it for real.

keyvalue
Good forquick experiments
No reindexadd any time
Costscript per doc
Laterindex it for real

Summary

Runtime fields compute values with a script at search time, with no reindexing. Next, how Elasticsearch spreads data with shards and replicas.

Key takeaways
  • runtime_mappings
  • emit()
  • No reindex
Next up: Shards and Replicas — how data is spread