Type to search.

Reindex 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 a reindex operation to add, modify, or delete fields within each document in an original index as its reindexed into a target index.

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)
The value used to select a shard for document storage.
ctx['_index'] (String)
The name of the index.
ctx['_id'] (String)
The unique document id.
ctx['_version'] (int)
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 noop to specify no operation or delete to delete the current document from the index.
ctx['_routing']
Modify this to change the routing value for the current document.
ctx['_index']
Modify this to change the destination index for the current document.
ctx['_id']
Modify this to change the id for the current document.
ctx['_version'] (int)
Modify this to modify the version for the current document.
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 copies all documents from the kibana_sample_data_ecommerce index to a new index called data_ecommerce_usd. During this process, a script is used to convert all monetary values to a new currency using a specified exchange rate in the params.

JSON
POST /_reindex
{
  "source": {
    "index": "kibana_sample_data_ecommerce"
  },
  "dest": {
    "index": "data_ecommerce_usd"
  },
  "script": {
    "source": """
      float er = (float) params.exchange_rate;
      ctx._source.currency = params.target_currency;

      // Convert all prices to dollar
      ctx._source.taxful_total_price = Math.round(ctx._source.taxful_total_price * er * 100) / 100;
      ctx._source.taxless_total_price = Math.round(ctx._source.taxless_total_price * er * 100) / 100;

      for (product in ctx._source.products) {
        product.price = Math.round(product.price * er * 100) / 100;
        product.base_price = Math.round(product.base_price * er * 100) / 100;
        product.taxful_price = Math.round(product.taxful_price * er * 100) / 100;
        product.taxless_price = Math.round(product.taxless_price * er * 100) / 100;
      }
    """,
    "lang": "painless",
    "params": {
      "exchange_rate": 1.10,
      "target_currency": "USD"
    }
  }
}
  1. Cast parameter to float to ensure proper decimal calculations