Update context
Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud. Availability This functionality is generally available and ready for production usage. Serverless interfaces and procedures might differ from classic Elastic Stack deployments. The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats. Availability This functionality is generally available and ready for production usage. Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
Serverless
Generally available
Stack
Generally available
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.
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
MapandListstructure for the fields existing in a stored document.
ctx['op']- Use the default of
indexto update a document. Set tononeto specify no operation ordeleteto delete the current document from the index. ctx['_source']- Modify the values in the
Map/Liststructure to add, modify, or delete the fields of a document.
void- No expected return value.
The standard Painless API is available.
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:
GET /kibana_sample_data_ecommerce/_search
{
"size": 1,
"_source": false
}
The script performs two key actions:
- Calculates a discounted price by subtracting $5.00 from the original
taxful_total_price. - Adds the flag
discount_appliedto identify orders that received promotional pricing.
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
}
}
}