Aggregation on Boolean Fields

To save space I want to convert one field to boolean type.

The mapping of the index is-

{
"siddharth-03": {
"mappings": {
"_doc": {
"properties": {
"age": {
"type": "boolean"
}
}
}
}
}
}

I can write a query on elasticsearch to give me the addition of this boolean field in its bucket.

GET siddharth-03/_search
{
"aggs": {
"NAME": {
"sum": {
"field": "age"
}
}
}
}

This results in the correct aggregation value-

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "siddharth-03",
"_type": "_doc",
"_id": "2",
"_score": 1,
"_source": {
"age": "true"
}
},
{
"_index": "siddharth-03",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"age": "true"
}
},
{
"_index": "siddharth-03",
"_type": "_doc",
"_id": "3",
"_score": 1,
"_source": {
"age": "false"
}
}
]
},
"aggregations": {
"NAME": {
"value": 2,
"value_as_string": "true"
}
}
}

When I want to add an aggregation on this boolean field in kibana, it shows me that the boolean field is not a compatible field for aggregations. How can I sum over this field in kibana?

sum aggregation only works on numeric fields. you could use a scripted field to convert your boolean value to numeric, if you wanted to trade space for performance ... i don't think that's a good idea however ...

you can however use count and set a filter to age:true if you want to count your documents where count is true (instead of using a sum on age where age can only be 1 or 0

To save space, we require 1 bit numeric datatype to perform aggregations, but they are not available. Using filters is slower than sum aggregations. Why doesnt elasticsearch support 1 bit numeric datatype

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.