2018-07-02 17:44:36 -04:00
|
|
|
[[painless-update-context]]
|
|
|
|
=== Update context
|
|
|
|
|
2018-07-25 13:59:25 -04:00
|
|
|
Use a Painless script in an {ref}/docs-update.html[update] operation to
|
2018-07-02 17:44:36 -04:00
|
|
|
add, modify, or delete fields within a single document.
|
|
|
|
|
|
|
|
*Variables*
|
|
|
|
|
|
|
|
`params` (`Map`, read-only)::
|
|
|
|
User-defined parameters passed in as part of the query.
|
|
|
|
|
|
|
|
`ctx['_op']` (`String`)::
|
|
|
|
The name of the operation.
|
|
|
|
|
2018-07-25 13:59:25 -04:00
|
|
|
{ref}/mapping-routing-field.html[`ctx['_routing']`] (`String`, read-only)::
|
2018-07-02 17:44:36 -04:00
|
|
|
The value used to select a shard for document storage.
|
|
|
|
|
2018-07-25 13:59:25 -04:00
|
|
|
{ref}/mapping-index-field.html[`ctx['_index']`] (`String`, read-only)::
|
2018-07-02 17:44:36 -04:00
|
|
|
The name of the index.
|
|
|
|
|
2018-07-25 13:59:25 -04:00
|
|
|
{ref}/mapping-type-field.html[`ctx['_type']`] (`String`, read-only)::
|
2018-07-02 17:44:36 -04:00
|
|
|
The type of document within an index.
|
|
|
|
|
2018-07-25 13:59:25 -04:00
|
|
|
{ref}/mapping-id-field.html[`ctx['_id']`] (`int`, read-only)::
|
2018-07-02 17:44:36 -04:00
|
|
|
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.
|
|
|
|
|
2018-07-25 13:59:25 -04:00
|
|
|
{ref}/mapping-source-field.html[`ctx['_source']`] (`Map`)::
|
2018-07-02 17:44:36 -04:00
|
|
|
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.
|
|
|
|
|
2018-07-25 13:59:25 -04:00
|
|
|
{ref}/mapping-source-field.html[`ctx['_source']`]::
|
2018-07-02 17:44:36 -04:00
|
|
|
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*
|
|
|
|
|
2019-01-28 18:57:27 -05:00
|
|
|
The standard <<painless-api-reference, Painless API>> is available.
|
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
|
|
|
To run this example, first follow the steps in
|
|
|
|
<<painless-context-examples, context examples>>.
|
|
|
|
|
|
|
|
The following query updates a document to be sold, and sets the cost
|
|
|
|
to the actual price paid after discounts:
|
|
|
|
|
2019-09-09 13:38:14 -04:00
|
|
|
[source,console]
|
2019-01-28 18:57:27 -05:00
|
|
|
--------------------------------------------------
|
|
|
|
POST /seats/_update/3
|
|
|
|
{
|
|
|
|
"script": {
|
|
|
|
"source": "ctx._source.sold = true; ctx._source.cost = params.sold_cost",
|
|
|
|
"lang": "painless",
|
|
|
|
"params": {
|
|
|
|
"sold_cost": 26
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TEST[setup:seats]
|