Elasticsearch - illegal_access_exception执行groovy脚本时出错

时间:2016-09-13 23:11:47

标签: elasticsearch groovy

我必须在更新到elasticsearch时将数组值合并到相应的索引。

我有一个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
}

知道如何解决此错误并运行脚本吗?

1 个答案:

答案 0 :(得分:0)

看起来这是一个已知的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()]}"