Runtime fields let you create new fields on the fly when you search your data. You don't need to change your stored data.
Runtime fields in Elasticsearch
Start learning this pattern below
Jump into concepts and practice - no test required
{
"runtime_mappings": {
"field_name": {
"type": "keyword|long|double|date|boolean|ip",
"script": {
"source": "emit(your_script_here)"
}
}
}
}The runtime_mappings section defines new fields for the search request.
The script uses painless language to calculate the field's value.
full_name field by joining first and last names.{
"runtime_mappings": {
"full_name": {
"type": "keyword",
"script": {
"source": "emit(doc['first_name'].value + ' ' + doc['last_name'].value)"
}
}
}
}is_adult that is true if age is 18 or more.{
"runtime_mappings": {
"is_adult": {
"type": "boolean",
"script": {
"source": "emit(doc['age'].value >= 18)"
}
}
}
}price_with_tax by adding 20% tax to the price.{
"runtime_mappings": {
"price_with_tax": {
"type": "double",
"script": {
"source": "emit(doc['price'].value * 1.2)"
}
}
}
}This search query returns all documents. It adds a runtime field full_name by joining first_name and last_name. The results show only full_name and age.
{
"query": {
"match_all": {}
},
"runtime_mappings": {
"full_name": {
"type": "keyword",
"script": {
"source": "emit(doc['first_name'].value + ' ' + doc['last_name'].value)"
}
}
},
"_source": ["full_name", "age"]
}Runtime fields are calculated at search time, so they can slow down queries if complex.
They do not change your stored data or index mappings permanently.
Use runtime fields to experiment or add temporary fields without reindexing.
Runtime fields create new fields dynamically during search without changing stored data.
They use painless scripts to calculate values from existing fields.
Useful for quick calculations, testing, or adding fields without reindexing.
Practice
runtime fields in Elasticsearch?Solution
Step 1: Understand runtime fields concept
Runtime fields are used to add fields dynamically at query time without modifying the stored data.Step 2: Compare options with concept
Only To create new fields dynamically during search without changing stored data describes creating fields dynamically during search without changing stored data.Final Answer:
To create new fields dynamically during search without changing stored data -> Option BQuick Check:
Runtime fields = dynamic fields at search time [OK]
- Confusing runtime fields with permanent mapping changes
- Thinking runtime fields modify stored documents
- Assuming runtime fields delete data
full_name that concatenates first_name and last_name using painless script?Solution
Step 1: Identify correct runtime field syntax
Runtime fields are defined underruntime_mappingswith atypeand ascriptobject containingsourcecode.Step 2: Check script correctness
{ "runtime_mappings": { "full_name": { "type": "keyword", "script": { "source": "emit(doc['first_name'].value + ' ' + doc['last_name'].value)" } } } } usesemit()insidesourcestring and accessesdoc['field'].valuecorrectly.Final Answer:
{ "runtime_mappings": { "full_name": { "type": "keyword", "script": { "source": "emit(doc['first_name'].value + ' ' + doc['last_name'].value)" } } } } -> Option DQuick Check:
runtime_mappings + emit() + doc['field'].value = correct syntax [OK]
- Using mappings instead of runtime_mappings
- Missing emit() function in script
- Incorrect script syntax without source object
{
"runtime_mappings": {
"age_plus_ten": {
"type": "long",
"script": {
"source": "emit(doc['age'].value + 10)"
}
}
}
}What will be the value of
age_plus_ten for a document with age = 25?Solution
Step 1: Understand the script logic
The script emits the value ofagefield plus 10.Step 2: Calculate the result for age=25
25 + 10 = 35.Final Answer:
35 -> Option AQuick Check:
age + 10 = 35 [OK]
- Confusing addition with subtraction
- Assuming runtime fields modify stored data
- Expecting syntax error instead of calculation
{
"runtime_mappings": {
"discounted_price": {
"type": "double",
"script": {
"source": "emit(doc['price'].value * 0.9)"
}
}
}
}But the query fails with an error:
Field [price] not found in doc. What is the likely cause?Solution
Step 1: Analyze error message
Error sayspricefield not found in document, meaning some docs lack this field.Step 2: Understand runtime field behavior
Runtime scripts fail if they access missing fields without checks.Final Answer:
Thepricefield is missing in some documents -> Option CQuick Check:
Missing field in doc causes runtime script error [OK]
- Assuming script syntax error without checking data
- Thinking runtime fields require mapping changes
- Ignoring missing field presence in documents
status that returns "adult" if age ≥ 18, otherwise "minor". Which painless script correctly implements this logic?Solution
Step 1: Check correct painless syntax for runtime fields
Runtime fields useemit()to output values; accessing field value requiresdoc['age'].value.Step 2: Verify conditional logic
"emit(doc['age'].value >= 18 ? 'adult' : 'minor')" uses ternary operator with >= 18 and emits 'adult' or 'minor' correctly.Final Answer:
"emit(doc['age'].value >= 18 ? 'adult' : 'minor')" -> Option AQuick Check:
emit() + ternary + doc['age'].value = correct [OK]
- Using return instead of emit() in runtime fields
- Accessing doc['age'] without .value
- Using > instead of >= changing logic
