Painless script failes while deleting nested field

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.

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

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

My problem is not fixed yet. (

Try this script:

"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\") } } }"
1 Like

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?

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?

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

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