# Question about date histogram

**URL:** <https://discuss.elastic.co/t/question-about-date-histogram/29083>\
**Category:** Elasticsearch\
**Created:** [September 11, 2015, 3:25am UTC](https://discuss.elastic.co/t/question-about-date-histogram/29083 "2015-09-11T03:25:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![z\_w\_w1981](https://avatars.discourse-cdn.com/v4/letter/z/41988e/32.png) [@z\_w\_w1981](https://discuss.elastic.co/u/z_w_w1981)\
**Post date:** [September 11, 2015, 3:25am UTC](https://discuss.elastic.co/t/question-about-date-histogram/29083/1 "2015-09-11T03:25:54Z")

</div>

Hi,

I want to get distinct "clientip" count in a specified day (TimeZone +08:00), so I run the query:  
{  
"query" : {  
"filtered" : {  
"query" : {  
"match\_all" : { }  
},  
"filter" : {  
"and" : {  
"filters" : [ {  
"term" : {  
"response" : "200"  
}  
}, {  
"range" : {  
"timestamp" : {  
"from" : "2015-08-28T16:00:00.000Z",  
"to" : "2015-08-29T16:00:00.000Z",  
"include\_lower" : true,  
"include\_upper" : false  
}  
}  
}, {  
"prefix" : {"request" : "/v2/version"}  
} ]  
}  
}  
}  
},  
"aggregations" : {  
"distinct\_client\_ip\_count" : {  
"cardinality" : {  
"field" : "clientip"  
}  
}  
}  
}  
it works well, the result is 143. but when I change the query using "date histogram", the result is different (result is 139), my "date histogram" query is:  
{  
"query" : {  
"filtered" : {  
"query" : {  
"match\_all" : { }  
},  
"filter" : {  
"and" : {  
"filters" : [ {  
"term" : {  
"response" : "200"  
}  
}, {  
"range" : {  
"timestamp" : {  
"from" : "2015-08-28T16:00:00.000Z",  
"to" : "2015-08-29T16:00:00.000Z",  
"include\_lower" : true,  
"include\_upper" : false  
}  
}  
}, {  
"prefix" : {"request" : "/v2/version"}  
} ]  
}  
}  
}  
},  
"aggs" : {  
"over\_time" : {  
"date\_histogram" : {  
"field" : "timestamp",  
"interval" : "day",  
"time\_zone": "+08:00"  
},  
"aggregations" : {  
"distinct\_client\_ip\_count" : {  
"cardinality" : {  
"field" : "clientip"  
}  
}  
}  
}  
}  
}

What's wrong with my query? The only difference between the 2 query is I embedded the cardinality agg as a sub agg in the date histogram.  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![z\_w\_w1981](https://avatars.discourse-cdn.com/v4/letter/z/41988e/32.png) [@z\_w\_w1981](https://discuss.elastic.co/u/z_w_w1981)\
**Post date:** [September 12, 2015, 2:03am UTC](https://discuss.elastic.co/t/question-about-date-histogram/29083/2 "2015-09-12T02:03:50Z")

</div>

I have resolved the issue, it caused by cardinality, cardinality in the ES is not very accurate, we can set the precision\_threshold to change the accuracy.

---

<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:** [July 5, 2017, 11:50pm UTC](https://discuss.elastic.co/t/question-about-date-histogram/29083/3 "2017-07-05T23:50:38Z")

</div>


