如何在弹性搜索中聚合布尔值? [英] How to aggregate boolean values in elastic search?

查看:145
本文介绍了如何在弹性搜索中聚合布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在几个实验中存储实验的错误和弹性搜索中的布尔值值(如果结果正常)。
现在,我想在图中显示结果(使用highchart js)。
我使用这样的聚合查询来接收每天的聚合错误,包括标准偏差:

 查询: {
过滤:{
过滤器:{
范围:{
日期:{
gte:2015-1-1,
lte:2016-1-1,
time_zone:+1:00
}
}
}
}
}
//汇总结果
aggs:{
group_by_date:{
terms:{
field:date,
order:{_term: asc}
},
aggs:{
error_stats:{
extended_stats:{
field:error
}
}
}
}
}

我遇到的问题是我无法以与从DB中获取双重错误相同的方式检索布尔值。
当我在

  aggs:{
error_stats:{
extended_stats:{
field:ok
}
}
}

我收到此错误消息:

  ClassCastException [org.elasticsearch.index.fielddata。 plain.PagedBytesIndexFieldData不能转换为org.elasticsearch.index.fielddata.IndexNumericFieldData 

但是,这将是OK可以将所有布尔值usign true设为1,将false设为零,然后接收每天的平均值。



任何人都可以帮助我吗? p>

谢谢alot!

解决方案

第一个0/1表示不完全是ES布尔表示法。有一个布尔类型为true / false。
第二次统计聚合只能在数字字段上进行,而不能在字符串字段上完成。
这就是为什么它适用于0/1表示。



您可以使用扩展统计信息中的脚本



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ :{
extended_stats:{
field:grade,
script:_value =='T'?1:0,
}
}
}
}
}

汇总中使用脚本的一些示例,您可以查看此处


I am having several experiments a day storing the error of the experiment and a boolean value (if the result is ok) in elasticsearch. Now, I would like to display the results in a graph (using highchart js). I use an aggregation query like this to receive the aggregated errors for each day including the standard deviation:

query: {
                    filtered: {
                        filter: {
                            range : {
                                date: {
                                    "gte":"2015-1-1",
                                    "lte": "2016-1-1,
                                    "time_zone": "+1:00"
                                }
                            }
                        }
                    }
                },
                // Aggregate on the results
                aggs: {
                    group_by_date: {
                        terms:{
                            field:"date",
                            order: {_term:"asc"}
                        }, 
                        aggs:{
                            error_stats:{
                                extended_stats:{
                                    field:"error"
                                } 
                            }
                        }
                    }
                }

The problem I face is that I cannot retrieve the boolean values the same way as I get the double errors from the DB. When I just change the field name to "ok" in

aggs:{
                            error_stats:{
                                extended_stats:{
                                    field:"ok"
                                } 
                            }
                        }

I receive this error message:

ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData

However, it would be OK to aggreate all the boolean values usign true as 1 and false as zero and then to receive a mean value for each day.

Can anyone help me with this?

Thanks alot!

解决方案

First 0/1 representation is not exactly ES Boolean representation. There is a Boolean type for as true/false. Second stats aggregation can be only done on numeric field and not on string field. That is why it worked for 0/1 representation.

You can transform this value using scripts in extended stats

{
    "aggs" : {
        ...

        "aggs" : {
            "grades_stats" : {
                "extended_stats" : {
                    "field" : "grade",
                    "script" : "_value == 'T' ? 1 : 0",
                }
            }
        }
    }
}

To see some example usage of scripting in aggregation , you can look here.

这篇关于如何在弹性搜索中聚合布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆