Bird
Raised Fist0
Elasticsearchquery~20 mins

Runtime fields in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Runtime Fields Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a runtime script field calculation
What is the output of the following runtime field script when applied to a document with price = 100 and tax = 0.15?
Elasticsearch
{
  "runtime_mappings": {
    "total_price": {
      "type": "double",
      "script": {
        "source": "emit(doc['price'].value * (1 + doc['tax'].value))"
      }
    }
  }
}
AError: field not found
B15.0
C100.15
D115.0
Attempts:
2 left
💡 Hint
Remember the formula for total price including tax is price * (1 + tax).
Predict Output
intermediate
2:00remaining
Runtime field with conditional logic output
Given the runtime field script below, what value will it emit for a document with status = "active"?
Elasticsearch
{
  "runtime_mappings": {
    "status_flag": {
      "type": "keyword",
      "script": {
        "source": "if (doc['status'].value == 'active') { emit('1') } else { emit('0') }"
      }
    }
  }
}
A"1"
B"0"
C"active"
Dnull
Attempts:
2 left
💡 Hint
Check the condition comparing the status field value.
Predict Output
advanced
2:00remaining
Output of runtime field script with array field
What will the runtime field script emit for a document where scores = [10, 20, 30]?
Elasticsearch
{
  "runtime_mappings": {
    "max_score": {
      "type": "long",
      "script": {
        "source": "emit(Collections.max(doc['scores'].values))"
      }
    }
  }
}
A20
B10
C30
DError: Collections not defined
Attempts:
2 left
💡 Hint
The script uses Collections.max to find the highest value in the scores array.
Predict Output
advanced
2:00remaining
Runtime field script error type
What error will occur when running this runtime field script if the document does NOT have the discount field?
Elasticsearch
{
  "runtime_mappings": {
    "final_price": {
      "type": "double",
      "script": {
        "source": "emit(doc['price'].value * (1 - doc['discount'].value))"
      }
    }
  }
}
AIndexOutOfBoundsException
BNullPointerException
CNoSuchFieldException
DArithmeticException
Attempts:
2 left
💡 Hint
Accessing a missing field's value causes a null reference error.
🧠 Conceptual
expert
3:00remaining
Number of runtime fields created by this mapping
Given the following runtime_mappings configuration, how many runtime fields will be created?
Elasticsearch
{
  "runtime_mappings": {
    "field1": { "type": "keyword", "script": { "source": "emit('a')" } },
    "field2": { "type": "long", "script": { "source": "emit(1)" } },
    "field3": { "type": "double", "script": { "source": "emit(1.0)" } },
    "field4": { "type": "boolean", "script": { "source": "emit(true)" } },
    "field5": { "type": "date", "script": { "source": "emit(params._source['date_field'])" } }
  }
}
A5
B4
C3
D0
Attempts:
2 left
💡 Hint
Count each runtime field defined under runtime_mappings.

Practice

(1/5)
1. What is the main purpose of runtime fields in Elasticsearch?
easy
A. To backup the index data automatically
B. To create new fields dynamically during search without changing stored data
C. To delete existing fields from documents
D. To permanently add new fields to the index mapping

Solution

  1. Step 1: Understand runtime fields concept

    Runtime fields are used to add fields dynamically at query time without modifying the stored data.
  2. 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.
  3. Final Answer:

    To create new fields dynamically during search without changing stored data -> Option B
  4. Quick Check:

    Runtime fields = dynamic fields at search time [OK]
Hint: Runtime fields add data on-the-fly, not stored permanently [OK]
Common Mistakes:
  • Confusing runtime fields with permanent mapping changes
  • Thinking runtime fields modify stored documents
  • Assuming runtime fields delete data
2. Which of the following is the correct syntax to define a runtime field named full_name that concatenates first_name and last_name using painless script?
easy
A. { "runtime_mappings": { "full_name": { "type": "keyword", "script": "return doc['first_name'] + ' ' + doc['last_name']" } } }
B. { "mappings": { "full_name": { "type": "text" } } }
C. { "runtime_fields": { "full_name": { "type": "keyword", "script": "emit(doc['first_name'].value + ' ' + doc['last_name'].value)" } } }
D. { "runtime_mappings": { "full_name": { "type": "keyword", "script": { "source": "emit(doc['first_name'].value + ' ' + doc['last_name'].value)" } } } }

Solution

  1. Step 1: Identify correct runtime field syntax

    Runtime fields are defined under runtime_mappings with a type and a script object containing source code.
  2. Step 2: Check script correctness

    { "runtime_mappings": { "full_name": { "type": "keyword", "script": { "source": "emit(doc['first_name'].value + ' ' + doc['last_name'].value)" } } } } uses emit() inside source string and accesses doc['field'].value correctly.
  3. Final Answer:

    { "runtime_mappings": { "full_name": { "type": "keyword", "script": { "source": "emit(doc['first_name'].value + ' ' + doc['last_name'].value)" } } } } -> Option D
  4. Quick Check:

    runtime_mappings + emit() + doc['field'].value = correct syntax [OK]
Hint: Use runtime_mappings with script source and emit() for runtime fields [OK]
Common Mistakes:
  • Using mappings instead of runtime_mappings
  • Missing emit() function in script
  • Incorrect script syntax without source object
3. Given this runtime field definition in a search query:
{
  "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?
medium
A. 35
B. 15
C. 25
D. Error: field not found

Solution

  1. Step 1: Understand the script logic

    The script emits the value of age field plus 10.
  2. Step 2: Calculate the result for age=25

    25 + 10 = 35.
  3. Final Answer:

    35 -> Option A
  4. Quick Check:

    age + 10 = 35 [OK]
Hint: Add 10 to age field value as scripted [OK]
Common Mistakes:
  • Confusing addition with subtraction
  • Assuming runtime fields modify stored data
  • Expecting syntax error instead of calculation
4. You wrote this runtime field script:
{
  "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?
medium
A. Runtime fields cannot use numeric types
B. The script syntax is incorrect
C. The price field is missing in some documents
D. The discounted_price field must be defined in mappings

Solution

  1. Step 1: Analyze error message

    Error says price field not found in document, meaning some docs lack this field.
  2. Step 2: Understand runtime field behavior

    Runtime scripts fail if they access missing fields without checks.
  3. Final Answer:

    The price field is missing in some documents -> Option C
  4. Quick Check:

    Missing field in doc causes runtime script error [OK]
Hint: Check if all docs have fields used in runtime scripts [OK]
Common Mistakes:
  • Assuming script syntax error without checking data
  • Thinking runtime fields require mapping changes
  • Ignoring missing field presence in documents
5. You want to create a runtime field status that returns "adult" if age ≥ 18, otherwise "minor". Which painless script correctly implements this logic?
hard
A. "emit(doc['age'].value >= 18 ? 'adult' : 'minor')"
B. "if (doc['age'].value >= 18) { return 'adult' } else { return 'minor' }"
C. "emit(doc['age'] >= 18 ? 'adult' : 'minor')"
D. "emit(doc['age'].value > 18 ? 'adult' : 'minor')"

Solution

  1. Step 1: Check correct painless syntax for runtime fields

    Runtime fields use emit() to output values; accessing field value requires doc['age'].value.
  2. Step 2: Verify conditional logic

    "emit(doc['age'].value >= 18 ? 'adult' : 'minor')" uses ternary operator with >= 18 and emits 'adult' or 'minor' correctly.
  3. Final Answer:

    "emit(doc['age'].value >= 18 ? 'adult' : 'minor')" -> Option A
  4. Quick Check:

    emit() + ternary + doc['age'].value = correct [OK]
Hint: Use emit() with ternary and doc['field'].value for conditions [OK]
Common Mistakes:
  • Using return instead of emit() in runtime fields
  • Accessing doc['age'] without .value
  • Using > instead of >= changing logic