Type to search.

Update by query context

Serverless

Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Serverless interfaces and procedures might differ from classic Elastic Stack deployments.

Stack

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Generally available

    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.

Use a Painless script in an update by query operation to add, modify, or delete fields within each of a set of documents collected as the result of query.

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['_source'] (Map)
Contains extracted JSON in a Map and List structure for the fields existing in a stored document.
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.
void
No expected return value.

The standard Painless API is available.

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

The following request uses the _update_by_query API to apply a discount to products that meet two conditions:

  1. The product’s base_price is greater than or equal to 20.
  2. The product currently has a discount_percentage of 0 (no discount applied).
JSON
POST /kibana_sample_data_ecommerce/_update_by_query
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "products.base_price": {
              "gte": 20
            }
          }
        },
        {
          "match": {
            "products.discount_percentage": 0
          }
        }
      ]
    }
  },
  "script": {
    "lang": "painless",
    "source": """
        for (product in ctx._source.products) {
            // If product has no discount applied
            if (product.discount_percentage == 0) {
                product.discount_percentage = params.discount_rate;
                product.discount_amount = product.base_price * (params.discount_rate / 100) ;

                product.price = product.base_price - product.discount_amount;
            }
        }
    """,
    "params": {
      "discount_rate": 15
    }
  }
}
  1. Assigns the discount rate
  2. Calculates and assigns the total discount
  3. Calculates and assigns the product price with discount