# Painless script failes while deleting nested field

**URL:** <https://discuss.elastic.co/t/painless-script-failes-while-deleting-nested-field/134963>\
**Category:** Elasticsearch\
**Created:** [June 7, 2018, 11:05am UTC](https://discuss.elastic.co/t/painless-script-failes-while-deleting-nested-field/134963 "2018-06-07T11:05:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikentyi](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@Vikentyi](https://discuss.elastic.co/u/Vikentyi)\
**Post date:** [June 7, 2018, 11:05am UTC](https://discuss.elastic.co/t/painless-script-failes-while-deleting-nested-field/134963/1 "2018-06-07T11:05:05Z")

</div>

Hello.

Elasticsearch 5.4.2. I have the following index mapping:

```
{
"pop": {
	"mappings": {
		"poptype": {
			"properties": {
				"popnested": {
					"type": "nested",
					"properties": {
						"first": {
							"type": "keyword",
							"fields": {
								"_lowercase": {
									"type": "keyword",
									"normalizer": "lowercase_normalizer"
								}
							}
						},
						"second": {
							"type": "keyword",
							"fields": {
								"_lowercase": {
									"type": "keyword",
									"normalizer": "lowercase_normalizer"
								}
							}
						}
					}
				}
			}
		}
	}
}

```

}

I need to delete field from index, so I am trying to reindex my 'pop' index into new one without 'popnested.second' field. My code works fine only if 'popnested.second' does not contain any data but when it is not empty, I have the following exception:

```
  ReindexAction.INSTANCE.newRequestBuilder( transportClient )
              .source( pop )
              .destination( newPop )
              .script( new Script( "if (ctx._type==\"poptype"\) { if(ctx._source.popnested!==null )" + 
              " {ctx._source.popnested.remove(\"second\"); } } " ) ).execute().get();

```

RemoteTransportException[[elastic-my-node][127.0.0.1][indices:data/write/reindex]]; nested: ScriptException[runtime error]; nested: NotSerializableExceptionWrapper[wrong\_method\_type\_exception: cannot convert MethodHandle(List,int)Object to (Object,String)Object];

Could you explain me where is my mistake? please.

---

<div class="post-metadata">

**Author:** ![Vikentyi](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@Vikentyi](https://discuss.elastic.co/u/Vikentyi)\
**Post date:** [June 8, 2018, 8:26am UTC](https://discuss.elastic.co/t/painless-script-failes-while-deleting-nested-field/134963/2 "2018-06-08T08:26:14Z")

</div>

Is it correct painless script to remove only one field from nested object?

---

<div class="post-metadata">

**Author:** ![Vikentyi](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@Vikentyi](https://discuss.elastic.co/u/Vikentyi)\
**Post date:** [June 13, 2018, 9:14pm UTC](https://discuss.elastic.co/t/painless-script-failes-while-deleting-nested-field/134963/3 "2018-06-13T21:14:27Z")

</div>

Sorry, I need your help because I have tried different variants and nothing was working correctly.

---

<div class="post-metadata">

**Author:** ![Vikentyi](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@Vikentyi](https://discuss.elastic.co/u/Vikentyi)\
**Post date:** [July 9, 2018, 11:47am UTC](https://discuss.elastic.co/t/painless-script-failes-while-deleting-nested-field/134963/4 "2018-07-09T11:47:56Z")

</div>

My problem is not fixed yet. (

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [July 9, 2018, 1:38pm UTC](https://discuss.elastic.co/t/painless-script-failes-while-deleting-nested-field/134963/5 "2018-07-09T13:38:31Z")

</div>

Try this script:

```auto
"if (ctx._type==\"poptype\") { if(ctx._source.popnested!==null ) { for (int i = 0; i < ctx._source.popnested.length; ++i) { ctx._source.popnested[i].remove(\"second\") } } }"

```

---

<div class="post-metadata">

**Author:** ![Vikentyi](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@Vikentyi](https://discuss.elastic.co/u/Vikentyi)\
**Post date:** [July 9, 2018, 7:29pm UTC](https://discuss.elastic.co/t/painless-script-failes-while-deleting-nested-field/134963/6 "2018-07-09T19:29:45Z")

</div>

> [@abdon](#):
>
> "if (ctx.\_type=="poptype") { if(ctx.\_source.popnested!==null ) { for (int i = 0; i \< ctx.\_source.popnested.length; ++i) { ctx.\_source.popnested[i].remove("second") } } }

Thanks, it works fine BUT if it is running once... when i run the another removement of popnested field (example, "first") - it cleans data from the whole index. Does length method return correct value after previous removement?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [July 10, 2018, 6:55am UTC](https://discuss.elastic.co/t/painless-script-failes-while-deleting-nested-field/134963/7 "2018-07-10T06:55:44Z")

</div>

I'm not sure how this script could delete all data from the whole index. How are you using this script? In an `_update_by_query`? Can you post the request that you are sending to Elasticsearch?

---

<div class="post-metadata">

**Author:** ![Vikentyi](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@Vikentyi](https://discuss.elastic.co/u/Vikentyi)\
**Post date:** [July 11, 2018, 9:21am UTC](https://discuss.elastic.co/t/painless-script-failes-while-deleting-nested-field/134963/8 "2018-07-11T09:21:03Z")

</div>

It was my mistake. Thank you very much for your response. It works fine now! My problem is fixed!

---

<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:** [August 8, 2018, 9:21am UTC](https://discuss.elastic.co/t/painless-script-failes-while-deleting-nested-field/134963/9 "2018-08-08T09:21:06Z")

</div>

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