Deleting nested field object with hyphens via inline scripting

This is an issue I am running into with our v2.4 cluster (using groovy), but I can also reproduce the error in v5.6 (using painless).

When I delete fields inside of an object, I send a request to the Update API with an inline script to remove the inner field.

E.g. for an document in the format below:
{
person_details: {
first_name: "Kamerynn"
last_name: "Harrah"
}
}

The request to delete the last name field would look like:
POST hostname/index/person/1/_update
{
"script": "ctx._source.person_details.remove("first_name");"
}

However, there is an issue I am running into, specifically when the object fields have a hyphen in them.

E.g., if the object instead looked like this (notice the hyphen instead of the underscore)
{
person-details: {
first-name: "Kamerynn"
last-name: "Harrah"
}
}

The following script would throw an IllegalArgumentException:
{
"script": "ctx._source.person-details.remove("first-name");"
}

The problem is not from the hyphen in the field being removed. The problem is from "person-details" having a hyphen, which seems to cause issues with the groovy syntax.

I have googled approaches to this problem with no success. One suggestion on StackOverflow was to use a sequence of chained methods "{field}.[path.to](http://path.to/)[\"{inner_field}\"]" within groovy, but that does not seem to work, and I cannot find any documentation for those methods.

I am hopeful that, since Elasticsearch allows the use of hyphens in inner object field names, there is a syntax for removing these fields that I am unaware of!

Appreciate any help, thank you!

Elastic support helped with this issue. You are able to use square brackets to reference nested fields with special characters (like Javascript's syntax). So the following would work:

{
"script": "ctx._source["person-details"].remove("first-name");"
}

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.