# Cardinality in date histogram query

**URL:** <https://discuss.elastic.co/t/cardinality-in-date-histogram-query/83578>\
**Category:** Elasticsearch\
**Created:** [April 25, 2017, 2:01pm UTC](https://discuss.elastic.co/t/cardinality-in-date-histogram-query/83578 "2017-04-25T14:01:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JeroenvdV](https://avatars.discourse-cdn.com/v4/letter/j/a8b319/32.png) [@JeroenvdV](https://discuss.elastic.co/u/JeroenvdV)\
**Post date:** [April 25, 2017, 2:01pm UTC](https://discuss.elastic.co/t/cardinality-in-date-histogram-query/83578/1 "2017-04-25T14:01:24Z")

</div>

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"}}
                }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 23, 2017, 2:15pm UTC](https://discuss.elastic.co/t/cardinality-in-date-histogram-query/83578/2 "2017-05-23T14:15:48Z")

</div>

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