Hi,
I am trying to count buckets with averages to be able to loop though them in PHP but I am not getting the "cardinality" query to work in my existing query. Probably just putting it in the wrong place, I tried several things but am not getting it to work. Could anyone give me a hint? thx!
PS I read cardinality gives an approximate value. Would this also be the case if there are just around 30 buckets max? If not accurate I cannot use it to loop through the result as there is a risk of a non existing index...
mapping:
"device" : {
"properties" : {
"company_email" : {"type" : "text", "index" : "not_analyzed"},
"owner_email" : {"type" : "text", "index" : "not_analyzed"},
"type" : {"type" : "text", "index" : "not_analyzed"},
"identification" : {"type" : "text", "index" : "not_analyzed"}
},
"_parent": {"type": "user"}
},
"device_payload" : {
"properties" : {
"timestamp" : {"type" : "date", "format" : "epoch_second"},
"temperature" : {"type" : "float"},
"humidity" : {"type" : "half_float"},
"voltage_1" : {"type" : "half_float"},
"voltage_2" : {"type" : "half_float"}
},
"_parent": {"type": "device"}
}
query:
POST /app/device_payload/_search
{
"size": 0,
"query" : {
"has_parent" : {
"parent_type" : "device",
"query" : {
"term" : {"_id" : "wifi_temp_hum1"}
}
}
},
"aggs": {
"last_7_days": {
"filter": {
"range": {
"timestamp": {
"gte": "now-7d",
"lte": "now"
}
}
},
"aggs": {
"average_per_day": {
"date_histogram": {
"field": "timestamp",
"interval": "6h"
},
"aggs": {
"avg_temp": {"avg" : {"field": "temperature"}},
"avg_hum": {"avg": {"field": "humidity"}},
"avg_volt_1": {"avg": {"field": "voltage_1"}},
"avg_volt_2": {"avg": {"field": "voltage_2"}}
}
}
}
}
}
}