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

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

最近在给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"
	}
}'

 

 

 

 

 

 

 

今天的文章elasticsearch报错: illegal argument exception, variable is not found分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/12380.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注