我通过传输客户端运行一个groovy脚本: 脚本如下所示:
if (ctx._source.field.names == null)
ctx._source.field.names=newItems
else
ctx._source.field.names<< newItems;
ctx._source.field.names = ctx._source.field.names.flatten().unique()
我的问题是它大部分时间都有效,但有时我会收到以下错误:
Caused by: java.lang.IllegalArgumentException: failed to execute script
at org.elasticsearch.action.update.UpdateHelper.executeScript(UpdateHelper.java:256) ~[org.elasticsearch.elasticsearch-2.0.1.jar:2.0.1]
at org.elasticsearch.action.update.UpdateHelper.prepare(UpdateHelper.java:196) ~[org.elasticsearch.elasticsearch-2.0.1.jar:2.0.1]
at org.elasticsearch.action.update.UpdateHelper.prepare(UpdateHelper.java:79) ~[org.elasticsearch.elasticsearch-2.0.1.jar:2.0.1]
at org.elasticsearch.action.update.TransportUpdateAction.shardOperation(TransportUpdateAction.java:170) ~[org.elasticsearch.elasticsearch-2.0
at org.elasticsearch.action.update.TransportUpdateAction$3$1.doRun(TransportUpdateAction.java:227) ~[org.elasticsearch.elasticsearch-2.0.1.
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) ~[org.elasticsearch.elasticsearch-2.0.1.jar:2.0.1]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_74-cedar14]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_74-cedar14]
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_74-cedar14]
Caused by: org.elasticsearch.script.groovy.GroovyScriptExecutionException: failed to run inline script [if (ctx._source.field.names == null)
ctx._source.field.names=newItems
else
ctx._source.field.names<< newItems;
ctx._source.field.names = ctx._source.field.names.flatten().unique()] using lang [groovy]
at org.elasticsearch.script.groovy.GroovyScriptEngineService$GroovyScript.run(GroovyScriptEngineService.java:253) ~[org.elasticsearch.
at org.elasticsearch.action.update.UpdateHelper.executeScript(UpdateHelper.java:251) ~[org.elasticsearch.elasticsearch-2.0.1.jar:2.0.1]
... 8 common frames omitted
Caused by: java.lang.NullPointerException: Cannot get property 'names' on null object
at org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:60) ~[org.codehaus.groovy.groovy-all-2.4.0.jar:2.4.0]
at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:172) ~[org.codehaus.groovy.groovy-all-2.4.0.jar:2.4.0]
at org.codehaus.groovy.runtime.callsite.NullCallSite.getProperty(NullCallSite.java:47) ~[org.codehaus.groovy.groovy-all-2.4.0.jar:2.4.0]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:296) ~[org.codehaus.groovy.groovy-all-2.4.0.
at 889babff0826c95e16279a9fb1fd2eb97a63e642.run(889babff0826c95e16279a9fb1fd2eb97a63e642:1) ~[na:na]
at org.elasticsearch.script.groovy.GroovyScriptEngineService$GroovyScript.run(GroovyScriptEngineService.java:248) ~[org.elasticsearch.
这是一个文档示例:
{
"field": {
"names": ["A","B","C"],
"A": { "name": "A is awesome" },
"B": { "name": "B is awesome" },
"C": { "name": "C is awesome" }
},
"email": "x@gmail.com"
}
映射:
"my_index": {
"mappings": {
"my_type": {
"properties": {
"email": {
"type": "string",
"analyzer": "email"
},
"field": {
"properties": {
"A": {
"properties": {
"name": {
"type": "string",
"analyzer": "lowercase"
}
}
},
"B": {
"properties": {
"name": {
"type": "string",
"analyzer": "lowercase"
}
}
},
"C": {
"properties": {
"name": {
"type": "string",
"analyzer": "lowercase"
}
}
},
"names": {
"type": "string",
"analyzer": "lowercase"
}
}
}
}
}
}
}
我的代码:
override def addTerms(id: String, path: String, terms: Set[String]): Future[Unit] = {
val groovyScript =
s"""if (ctx._source.$path == null)
| ctx._source.$path=newItems
|else
| ctx._source.$path<< newItems;
|ctx._source.$path = ctx._source.$path.flatten().unique();""".stripMargin
client.execute(ElasticDsl.update id id in indexName / documentType retryOnConflict 5 script {
script(groovyScript).params(Map("newItems" -> terms.toArray))
}).map(_ => Unit)
}
override def update(id: String, field: NestedFieldValue) = {
client.execute {
ElasticDsl.update(id) in indexName / documentType retryOnConflict 5 doc field
}
}
我查看了脚本失败的文档,并尝试使用curl手动运行此脚本,并且它有效。 所以这份文件并不是真正的问题。
我正在为38个文档运行此更新,这意味着它将运行38次。 每次从头开始尝试此更新时都会在异常文档上抛出异常(意味着在这38个文档上再次运行它)
不确定我在这里缺少什么。有任何想法吗? 谢谢!