Elasticsearch - illegal_access_exception执行groovy脚本时出错
问题描述:
我必须在更新到elasticsearch时将数组值合并到相应的索引。Elasticsearch - illegal_access_exception执行groovy脚本时出错
我有Groovy脚本:
def m1 = [ a:[1, 1, 1], b:[1, 1, 1], d:[1,1,1] ]
def m2 = [ b:[1, 1, 1], c:[1, 1, 1] ]
def newMap = [m1,m2]*.keySet().flatten().unique().collectEntries {
[ (it): [m1,m2]*.get(it).findAll().transpose()*.sum() ]
}
Result: [a:[1, 1, 1], b:[2, 2, 2], d:[1, 1, 1], c:[1, 1, 1]]
当我在elasticsearch添加为脚本:
{
"script": "ctx._source.fn=[ctx._source.fn,fn]*.keySet().flatten().unique().collectEntries {[ (it): [ctx._source.fn,fn]*.get(it).findAll().transpose()*.sum() ]}",
"params": {
"fn": {
"test1": [2],
"test2": [2,2]
}
},
"upsert": {
"fn": {
"test1": [2],
"test2": [2,2]
}
}
}
,我发现了以下错误:
{
"error": {
"root_cause": [
{
"type": "remote_transport_exception",
"reason": "[es:9300][indices:data/write/update[s]]"
}
],
"type": "illegal_argument_exception",
"reason": "failed to execute script",
"caused_by": {
"type": "script_exception",
"reason": "failed to run inline script [ctx._source.fn=[ctx._source.fn,fn]*.keySet().flatten().unique().collectEntries {[ (it): [ctx._source.fn,fn]*.get(it).findAll().transpose()*.sum() ]}] using lang [groovy]",
"caused_by": {
"type": "privileged_action_exception",
"reason": "privileged_action_exception: null",
"caused_by": {
"type": "illegal_access_exception",
"reason": "illegal_access_exception: Class org.codehaus.groovy.reflection.CachedMethod can not access a member of class groovy.lang.Closure$1 with modifiers \"public\""
}
}
}
},
"status": 400
}
任何想法如何解决这个错误并运行脚本?
答
看起来这是一个已知elasticsearch bug
我发现一种解决方法,通过改变findAll()
方法minus(null)
。现在脚本看起来像这样:
"script": "ctx._source.fn=[ctx._source.fn,fn]*.keySet().flatten().unique().collectEntries { [ (it): [ctx._source.fn,fn]*.get(it).minus(null).transpose()*.sum()]}"
'fn'似乎是地图列表,而不是'm2'中的地图 –
哦!得到它了 !!谢谢!! –