异常原因
原因就是因为官方给的例子参数位置书写错误,查看官方文档代码如下:
UpdateByQueryRequestBuilder updateByQuery =
UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
updateByQuery.source("source_index")
.script(new Script(
//1
ScriptType.INLINE,
//2
"if (ctx._source.awesome == 'absolutely') {"
+ " ctx.op='noop'"
+ "} else if (ctx._source.awesome == 'lame') {"
+ " ctx.op='delete'"
+ "} else {"
+ "ctx._source.awesome = 'absolutely'}",
+
//3
"painless",
//4
Collections.emptyMap()));
BulkByScrollResponse response = updateByQuery.get();
以上代码是官方提供的API例子,这个例子是错误的,因为Script的构造参数的正确顺序应该是1、3、2、4,源码API可以很好地证明这个事情
/**
* Constructor for a script that does not need to use compiler options.
* @param type :脚本的存储和索引的方式
* @param lang :脚本语言
* @param idOrCode :脚本代码
* @param params :脚本绑定用户输入参数
*/
public Script(ScriptType type, String lang, String idOrCode, Map<String, Object> params) {
this(type, lang, idOrCode, type == ScriptType.INLINE ? Collections.emptyMap() : null, params);
}