我使用无痛语言的map / reduce功能进行聚合。
在 map_script 部分,我试图过滤掉某个字段设置为 null 的所有文档,但我无法这样做。
我已尝试执行containsKey()
和!= null
,但即使我查看了基础数据,表达式也会评估每个文档的 true 绝大多数文档都将此字段设置为 null 。
我无法解释这一点,但它表现得像几乎每个文档的这个字段的值被视为0,而不是空,这是我聚合它的时候。但事实并非如此,因为当我查询这个字段时,我看到了一堆空值。
{
"aggs": {
"my_agg_name": {
"scripted_metric": {
"combine_script": "... combine script ...",
"map_script": "if (doc.containsKey(\"my_field\") && doc.my_field.value != null) { ... } }",
"init_script": "... init script ...",
"reduce_script": "... reduce script ..."
}
}
},
"size": 0
}
有没有人知道发生了什么?如何使用无痛过滤掉map / reduce聚合中的空值?
答案 0 :(得分:1)
我已经弄清楚了。 doc.my_field.value
显然将null值转换为0。
使用!doc.my_field.empty
代替了什么。