I have a Painless script I run in a Script processor in an Ingest Pipeline which moves fields from within a temporary jsonPayload field to the document root. The source looks like this (field names redacted for safety):
List bad_fields = new ArrayList();
bad_fields.add("foo");
bad_fields.add("bar");
for (String key : ctx.tmp.jsonPayload.keySet()) {
if (!bad_fields.contains(key)) {
ctx.key = ctx.tmp.jsonPayload.get(key)
}
}
The unwanted fields are not moved to the document root, which is all fine and well, and expected. However, it also looks like the value of the alphabetically last field in jsonPayload is put at the field literally named "key" at the document root, which is not expected. Apart from removing the field if it exists, what can I do to prevent this?