How to aggregate boolean values in elastic search?

时光怂恿深爱的人放手 提交于 2019-12-11 11:41:30

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/28050292/how-to-aggregate-boolean-values-in-elastic-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!