elasticsearch报错: illegal argument exception, variable is not found

最近在给es升级,从es2.3.3升级到es6.0.0, 由于新版的es不支持ScriptType.FILE类型,所以打算改成STORE类型。于是按照官方文档添加script:

 

curl -XPOST -H 'Content-Type:application/json' 'http://localhost:9200/_scripts/video_hot_score' -d '{
	"script": {
		"lang" : "painless", 
		"source" : "doc['hot_score'].value/((2+(params.current-doc['publish_time'].value)/86400000.0)*1.7)+1"
	}
}'

然后在代码里面使用这个script:

Map<String, Object> params = new HashMap<String, Object>();
        params.put("current", System.currentTimeMillis());
        Script script = new Script(ScriptType.STORED, null, "video_hot_score", params);
        ScoreFunctionBuilder scorefuntion = ScoreFunctionBuilders.scriptFunction(script);

直接报错:

 

 

Caused by: ScriptException[compile error]; nested: IllegalArgumentException[Variable [hot_score] is not defined.];
	at org.elasticsearch.painless.PainlessScriptEngine.convertToScriptException(PainlessScriptEngine.java:536)
	at org.elasticsearch.painless.PainlessScriptEngine.compile(PainlessScriptEngine.java:439)
	at org.elasticsearch.painless.PainlessScriptEngine.compile(PainlessScriptEngine.java:131)
	at org.elasticsearch.script.ScriptService.compile(ScriptService.java:335)
	at org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder.doToFunction(ScriptScoreFunctionBuilder.java:95)
	at org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder.toFunction(ScoreFunctionBuilder.java:138)
	at org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.doToQuery(FunctionScoreQueryBuilder.java:298)
	at org.elasticsearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:98)
	at org.elasticsearch.index.query.QueryShardContext.lambda$toQuery$2(QueryShardContext.java:311)
	at org.elasticsearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:323)
	at org.elasticsearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:310)
	at org.elasticsearch.search.SearchService.parseSource(SearchService.java:669)

首先确认该索引的mapping里面是有hot_score这个字段的,谷歌了好久,发现问题出在对引号的编码上。把添加的script里面单引号写成unicode编码的形式即可,如下:

curl -XPOST -H 'Content-Type:application/json' 'http://localhost:9200/_scripts/video_hot_score' -d '{
	"script": {
		"lang" : "painless", 
		"source" : "doc[\u0027hot_score\u0027].value/((2+(params.current-doc[\u0027publish_time\u0027].value)/86400000.0)*1.7)+1"
	}
}'

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值