How to aggregate boolean values in elastic search?

2019-08-03 16:46发布

问题:

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.