Back to Elasticsearch

Update context [painless-update-context]

docs/reference/scripting-languages/painless/painless-update-context.md

9.4.02.8 KB
Original Source

Update context [painless-update-context]

Use a Painless script in an update operation to add, modify, or delete fields within a single document.

Check out the document update tutorial for related examples.

Variables

params (Map, read-only) : User-defined parameters passed in as part of the query.

ctx['op'] (String) : The name of the operation.

ctx['_routing'] (String, read-only) : The value used to select a shard for document storage.

ctx['_index'] (String, read-only) : The name of the index.

ctx['_id'] (String, read-only) : The unique document id.

ctx['_version'] (int, read-only) : The current version of the document.

ctx['_now'] (long, read-only) : The current timestamp in milliseconds.

ctx['_source'] (Map) : Contains extracted JSON in a Map and List structure for the fields existing in a stored document.

Side Effects

ctx['op'] : Use the default of index to update a document. Set to none to specify no operation or delete to delete the current document from the index.

ctx['_source'] : Modify the values in the Map/List structure to add, modify, or delete the fields of a document.

Return

void : No expected return value.

API

The standard Painless API is available.

Example

To run the example, first install the eCommerce sample data.

The following example demonstrates how to apply a promotional discount to an order.

Find a valid document ID:

json
GET /kibana_sample_data_ecommerce/_search
{
  "size": 1,
  "_source": false
}

The script performs two key actions:

  1. Calculates a discounted price by subtracting $5.00 from the original taxful_total_price.
  2. Adds the flag discount_applied to identify orders that received promotional pricing.
json
POST /kibana_sample_data_ecommerce/_update/YOUR_DOCUMENT_ID
{
  "script": {
    "lang": "painless",
    "source": """
      ctx._source.discount_applied = true; 
      ctx._source.final_price = ctx._source.taxful_total_price - params.discount_amount;
      """,
    "params": {
      "discount_amount": 5.00
    }
  }
}