0
0
Elasticsearchquery~20 mins

Updating documents in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Elasticsearch Update Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Elasticsearch update script?
Given the following update request, what will be the value of the field counter after the update?
Elasticsearch
{
  "script" : {
    "source" : "ctx._source.counter += params.count",
    "params" : {
      "count" : 4
    }
  }
}
AThe <code>counter</code> field will be set to 4
BThe <code>counter</code> field will increase by 4
CThe <code>counter</code> field will decrease by 4
DThe update will fail with a script error
Attempts:
2 left
💡 Hint
Look at the script source and the operation it performs on ctx._source.counter.
Predict Output
intermediate
2:00remaining
What happens if you try to update a non-existent document?
Consider this update request targeting a document ID that does not exist. What is the expected result?
Elasticsearch
{
  "doc" : {
    "status" : "active"
  }
}
AElasticsearch returns a 404 error indicating document not found
BElasticsearch creates a new document with the given fields
CElasticsearch updates the document silently without error
DElasticsearch throws a script error
Attempts:
2 left
💡 Hint
Think about the default behavior of update requests on missing documents.
🔧 Debug
advanced
3:00remaining
Why does this update script fail with a runtime error?
This update script is intended to append a new tag to the tags array. Why does it fail?
Elasticsearch
{
  "script": {
    "source": "ctx._source.tags.add(params.new_tag)",
    "params": {
      "new_tag": "urgent"
    }
  }
}
AThe <code>add</code> method is not supported in Elasticsearch scripts
BThe parameter <code>new_tag</code> is not passed correctly
CThe script syntax is invalid due to missing semicolon
DThe <code>tags</code> field is null or missing, so <code>add</code> method fails
Attempts:
2 left
💡 Hint
Check if the tags field exists and is an array before calling add.
📝 Syntax
advanced
3:00remaining
Which update script syntax is correct to conditionally update a field?
You want to update the status field to "closed" only if its current value is "open". Which script is correct?
A"source": "if (ctx._source.status === 'open') { ctx._source.status = 'closed' }"
B"source": "if (ctx._source.status = 'open') { ctx._source.status = 'closed' }"
C"source": "if (ctx._source.status == 'open') { ctx._source.status = 'closed' }"
D"source": "if ctx._source.status == 'open' { ctx._source.status = 'closed' }"
Attempts:
2 left
💡 Hint
Check the syntax for if statements and equality operators in painless scripts.
🚀 Application
expert
4:00remaining
How to atomically increment a nested field in an update request?
You have a document with a nested field metrics.views. Which update request correctly increments views by 1 atomically?
A{ "script": { "source": "ctx._source.metrics.views += 1" } }
B{ "doc": { "metrics.views": "metrics.views + 1" } }
C{ "script": { "source": "ctx._source['metrics.views'] += 1" } }
D{ "doc": { "metrics": { "views": "metrics.views + 1" } } }
Attempts:
2 left
💡 Hint
Think about how to access nested fields in painless scripts and how to increment them.