Variable variables in Painless and Ingest Node

By | November 6, 2017

Just a short note: it is possible to create variable (or dynamic) variables in both Painless scripts and the Ingest Node Set Processor.

In Painless, you can do achieve this with the following notation:

ctx[ctx.key_variable] = value

Say you have two arrays that you’d like to ‘merge’ in Painless. So, the values of one of the arrays need to become the become variables named after them. In Painless script for Elasticsearch 6.x:

POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "script": {
          "lang": "painless",
          "source": """
    for (int i=0; i < ctx.keys.length ; i++) {
      ctx[ctx.keys[i]] = ctx.values[i]
    }
"""
        },
        "remove": {
          "field": ["keys", "values"]
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "keys": ["key1", "key2"],
        "values": ["val1", "val2"]
      }
    }
  ]
}

Mind you, you might have to scroll in this code window.

A note of warning here. Obviously a script like this one can blow up really fast if it gets different data then expected. You might want to look into strict mappings to limit the possibility of field explosions.

Variable Variables in Ingest Node Set Processor

The Set Processor in the Ingest Node can set a value to a variable, like so:

{
  "set": {
    "field": "field1",
    "value": 582.1
  }
}

If you'd like to make both of them dynamic, you can do something like:

{
  "set": {
    "field": "{{key}}",
    "value": "{{value}}"
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.