# Deleting nested field object with hyphens via inline scripting

**URL:** <https://discuss.elastic.co/t/deleting-nested-field-object-with-hyphens-via-inline-scripting/164648>\
**Category:** Elasticsearch\
**Created:** [January 17, 2019, 2:27pm UTC](https://discuss.elastic.co/t/deleting-nested-field-object-with-hyphens-via-inline-scripting/164648 "2019-01-17T14:27:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kamharrah](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kamharrah](https://discuss.elastic.co/u/kamharrah)\
**Post date:** [January 17, 2019, 2:27pm UTC](https://discuss.elastic.co/t/deleting-nested-field-object-with-hyphens-via-inline-scripting/164648/1 "2019-01-17T14:27:05Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![kamharrah](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kamharrah](https://discuss.elastic.co/u/kamharrah)\
**Post date:** [February 4, 2019, 1:38pm UTC](https://discuss.elastic.co/t/deleting-nested-field-object-with-hyphens-via-inline-scripting/164648/2 "2019-02-04T13:38:18Z")

</div>

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");"  
}

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 4, 2019, 1:38pm UTC](https://discuss.elastic.co/t/deleting-nested-field-object-with-hyphens-via-inline-scripting/164648/3 "2019-03-04T13:38:23Z")

</div>

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