Bird
0
0

You want to increment the views field by 1 for a document, but only if the field exists. Which update script achieves this safely?

hard🚀 Application Q9 of 15
Elasticsearch - Document Operations
You want to increment the views field by 1 for a document, but only if the field exists. Which update script achieves this safely?
APOST /articles/_update/10 { "script": "ctx._source.views += 1" }
BPOST /articles/_update/10 { "script": "ctx._source.views = ctx._source.views + 1" }
CPOST /articles/_update/10 { "doc": { "views": views + 1 } }
DPOST /articles/_update/10 { "script": "if (ctx._source.views != null) { ctx._source.views += 1 }" }
Step-by-Step Solution
Solution:
  1. Step 1: Understand the need for safe increment

    Increment only if views exists to avoid errors.
  2. Step 2: Analyze script options

    POST /articles/_update/10 { "script": "if (ctx._source.views != null) { ctx._source.views += 1 }" } checks for null before incrementing, preventing errors. POST /articles/_update/10 { "script": "ctx._source.views += 1" } and D do not check existence. POST /articles/_update/10 { "doc": { "views": views + 1 } } uses invalid syntax.
  3. Final Answer:

    POST /articles/_update/10 { "script": "if (ctx._source.views != null) { ctx._source.views += 1 }" } -> Option D
  4. Quick Check:

    Safe increment requires null check = POST /articles/_update/10 { "script": "if (ctx._source.views != null) { ctx._source.views += 1 }" } [OK]
Quick Trick: Check field exists before incrementing in script [OK]
Common Mistakes:
MISTAKES
  • Not checking field existence
  • Using invalid doc syntax
  • Assuming increment works without checks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Elasticsearch Quizzes