Bird
Raised Fist0
Elasticsearchquery~10 mins

Partial updates in Elasticsearch - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to update the document with id '1' in the 'products' index using a partial update.

Elasticsearch
{
  "script": {
    "source": "ctx._source.price = [1]",
    "params": {
      "price": 20
    }
  }
}
Drag options to blanks, or click blank then click option'
Actx._price
Bprice
Cparams.price
Dctx.price
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'price' directly without 'params.' prefix
Trying to access 'ctx.price' which does not exist
2fill in blank
medium

Complete the code to update the 'stock' field by incrementing it by 5 using a script.

Elasticsearch
{
  "script": {
    "source": "ctx._source.stock [1]= 5"
  }
}
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' which would decrement the value
Using '*' or '/' which would multiply or divide
3fill in blank
hard

Fix the error in the partial update to set the 'status' field to 'active'.

Elasticsearch
{
  "doc": {
    "status": [1]
  }
}
Drag options to blanks, or click blank then click option'
Astatus
B'active'
Cactive
D"active"
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted active which is invalid JSON
Using single quotes which are not valid in JSON
4fill in blank
hard

Fill both blanks to create a partial update that adds a tag 'new' to the 'tags' array.

Elasticsearch
{
  "script": {
    "source": "ctx._source.tags.[1](params.tag)",
    "params": {
      "tag": [2]
    }
  }
}
Drag options to blanks, or click blank then click option'
Aadd
B"new"
Cappend
D'new'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'append' which is not valid in painless scripts
Using single quotes for strings
5fill in blank
hard

Fill all three blanks to create a partial update that sets 'rating' to 4.5 only if the current rating is less than 4.5.

Elasticsearch
{
  "script": {
    "source": "if (ctx._source.[1] [2] params.rating) { ctx._source.[3] = params.rating }",
    "params": {
      "rating": 4.5
    }
  }
}
Drag options to blanks, or click blank then click option'
Ascore
B<
Crating
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' in the condition
Using different field names for check and update

Practice

(1/5)
1. What is the main purpose of using partial updates in Elasticsearch?
easy
A. To delete a document completely from the index
B. To create a new document with all fields specified
C. To update only specific fields of a document without replacing the whole document
D. To retrieve documents matching a query

Solution

  1. Step 1: Understand partial update concept

    Partial updates allow changing only some fields of a document instead of replacing the entire document.
  2. Step 2: Identify the main purpose

    This saves bandwidth and time by sending only the changed fields, not the whole document.
  3. Final Answer:

    To update only specific fields of a document without replacing the whole document -> Option C
  4. Quick Check:

    Partial update = update some fields only [OK]
Hint: Partial updates change only some fields, not whole document [OK]
Common Mistakes:
  • Confusing partial update with document deletion
  • Thinking partial update creates new documents
  • Believing partial update retrieves documents
2. Which of the following is the correct syntax to perform a partial update on a document with ID '1' in index 'products'?
easy
A. POST /products/_doc/1 { "price": 20 }
B. POST /products/_update/1 { "doc": { "price": 20 } }
C. PUT /products/_update/1 { "price": 20 }
D. GET /products/_update/1 { "doc": { "price": 20 } }

Solution

  1. Step 1: Identify correct HTTP method and endpoint

    Partial updates use POST method on the _update API with the document ID.
  2. Step 2: Check request body format

    The update body must include a "doc" key with the fields to update.
  3. Final Answer:

    POST /products/_update/1 { "doc": { "price": 20 } } -> Option B
  4. Quick Check:

    Use POST + _update + doc key [OK]
Hint: Use POST with _update and doc key for partial updates [OK]
Common Mistakes:
  • Using GET instead of POST for update
  • Omitting the 'doc' key in request body
  • Using PUT on _update endpoint incorrectly
  • Updating with _doc endpoint instead of _update
3. Given this partial update request on document ID '2' in index 'users':
POST /users/_update/2
{
  "doc": {
    "age": 30
  }
}

If the original document was {"name": "Alice", "age": 25}, what will be the document after the update?
medium
A. {"name": "Alice", "age": 30}
B. {"age": 30}
C. {"name": "Alice"}
D. {"name": "Alice", "age": 25, "updated": true}

Solution

  1. Step 1: Understand partial update effect

    The partial update changes only the "age" field to 30, keeping other fields unchanged.
  2. Step 2: Apply update to original document

    Original document {"name": "Alice", "age": 25} becomes {"name": "Alice", "age": 30} after update.
  3. Final Answer:

    {"name": "Alice", "age": 30} -> Option A
  4. Quick Check:

    Partial update changes only specified fields [OK]
Hint: Partial update changes only given fields, others stay same [OK]
Common Mistakes:
  • Thinking update replaces whole document
  • Assuming fields not in doc are removed
  • Adding extra fields not in update request
4. You try this partial update:
POST /orders/_update/5
{
  "doc": {
    "status": "shipped"
  }
}

But you get an error. Which is the most likely cause?
medium
A. The document with ID '5' does not exist in 'orders' index
B. You used GET instead of POST
C. You forgot to include the 'doc' key in the body
D. The 'status' field cannot be updated

Solution

  1. Step 1: Check common update errors

    Partial update errors often happen if the document ID does not exist.
  2. Step 2: Analyze given request and error cause

    The syntax is correct, so the likely cause is missing document with ID '5'.
  3. Final Answer:

    The document with ID '5' does not exist in 'orders' index -> Option A
  4. Quick Check:

    Missing document ID causes update error [OK]
Hint: Check if document ID exists before updating [OK]
Common Mistakes:
  • Confusing HTTP method errors with missing document
  • Assuming all fields are immutable
  • Omitting 'doc' key causing syntax errors
5. You want to increase the 'stock' field by 10 in a product document with ID '10' using a partial update. Which request body correctly achieves this?
hard
A. { "update": { "stock": 10 } }
B. { "doc": { "stock": 10 } }
C. { "doc": { "stock": "stock + 10" } }
D. { "script": { "source": "ctx._source.stock += params.count", "params": { "count": 10 } } }

Solution

  1. Step 1: Understand increment operation in partial update

    To increase a numeric field, use a script that adds the value to the existing field.
  2. Step 2: Identify correct syntax for scripted update

    { "script": { "source": "ctx._source.stock += params.count", "params": { "count": 10 } } } uses the 'script' key with source code to add 10 to 'stock'.
  3. Final Answer:

    { "script": { "source": "ctx._source.stock += params.count", "params": { "count": 10 } } } -> Option D
  4. Quick Check:

    Use script to increment numeric fields [OK]
Hint: Use script with ctx._source to increment fields [OK]
Common Mistakes:
  • Trying to update with doc key using expression string
  • Using 'update' key instead of 'doc' or 'script'
  • Assigning value directly instead of incrementing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard