我正在尝试使用Elasticsearch的scripted_metric aggs,正常情况下,它与我的其他脚本运行得非常好。
然而,在下面的脚本中,我遇到了一个名为"null_pointer_exception“的错误,但是它们只是复制粘贴的脚本,并且已经为6个模块工作了
$max = 10;
{
"query": {
"match_all": {}
//omitted some queries here, so I just turned it into match_all
}
},
"aggs": {
"ARTICLE_CNT_PDAY": {
"histogram": {
"field": "pub_date",
"interval": "86400"
},
"aggs": {
"LATEST": {
"nested": {
"path": "latest"
},
"aggs": {
"SUM_SVALUE": {
"scripted_metric": {
"init_script": "
state.te = [];
state.g = 0;
state.d = 0;
state.a = 0;
",
"map_script": "
if(state.d != doc['_id'].value){
state.d = doc['_id'].value;
state.te.add(state.a);
state.g = 0;
state.a = 0;
}
state.a = doc['latest.soc_mm_score'].value;
",
"combine_script": "
state.te.add(state.a);
double count = 0;
for (t in state.te) {
count += ((t*10)/$max)
}
return count;
",
"reduce_script": "
double count = 0;
for (a in states) {
count += a;
}
return count;
"
}
}
}
}
}
}
}
}我尝试在Kibana运行这个脚本,下面是错误消息:

我得到的是,reduce_script部分有问题,试图更改这个部分:
来自
for (a in states) {
count += a;
}到
for (a in states) {
count += 1;
}而且工作得很好,我觉得a变量没有得到它应该持有的东西
这里有什么想法吗?非常感谢您的帮助,非常感谢!
发布于 2019-11-29 04:44:52
原因被解释为这里
如果脚本化度量聚合的父桶不收集任何文档,则将从带有空值的碎片返回空聚合响应。在这种情况下,减脚本的状态变量将包含null作为来自该碎片的响应。因此,reduce_script应该期望并处理来自碎片的空响应。
很明显,其中一个桶是空的,您需要像这样处理空桶:
"reduce_script": "
double count = 0;
for (a in states) {
count += (a ?: 0);
}
return count;
"https://stackoverflow.com/questions/59087489
复制相似问题