# Why i got different result from these two DSL?

**URL:** https://discuss.elastic.co/t/why-i-got-different-result-from-these-two-dsl/69557
**Category:** Elasticsearch
**Created:** [December 20, 2016, 10:46am UTC](https://discuss.elastic.co/t/why-i-got-different-result-from-these-two-dsl/69557 "2016-12-20T10:46:46Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![moonnap](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@moonnap](https://discuss.elastic.co/u/moonnap)
#### Post date: [December 20, 2016, 10:46am UTC](https://discuss.elastic.co/t/why-i-got-different-result-from-these-two-dsl/69557/1 "2016-12-20T10:46:46Z")

</div>

#1  
{  
"size": 0,  
"aggs": {  
"by\_days": {  
"filter": {  
"bool": {  
"must": {"term": {"AppID": 5091}},  
"must": {"range": {  
"RecordTime": {  
"gte": 1479312000,  
"lt": 1479398400  
}}  
}  
}  
},  
"aggs": {  
"day\_data": {  
"date\_histogram": {  
"field": "RecordTime",  
"interval": "day",  
"format": "yyyy-MM-dd",  
"time\_zone": "+08:00"  
},  
"aggs": {  
"distinct\_user": {  
"cardinality": {  
"script": "doc['Channel'].value + ' ' + doc['AreaServerID'].value + ' ' + doc['RoleID'].value"  
}  
}  
}  
}  
}  
}  
}  
}

#result

{  
"key\_as\_string": "2016-11-17",  
"key": 1479312000000,  
"doc\_count": 22636,  
"distinct\_user": {  
"value": 3103  
}  
}

#2

{  
"size": 0,  
"aggs": {  
"by\_days": {  
"filter": {  
"bool": {  
"must": {"term": {"AppID": 5091}},  
"must": {"range": {  
"RecordTime": {  
"gte": 1479312000,  
"lt": 1479398400  
}}  
}  
}  
},  
"aggs": {  
"distinct\_user": {  
"cardinality": {  
"script": "doc['Channel'].value + ' ' + doc['AreaServerID'].value + ' ' + doc['RoleID'].value"  
}  
}  
}  
}  
}  
}

result

"by\_days": {  
"doc\_count": 22636,  
"distinct\_user": {  
"value": 3067  
}  
}

the distinct\_user is different, i thought they should be same.

please help, thanks.

---

<div class="post-metadata">

### Author: ![jakommo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakommo/32/6716_2.png) [@jakommo](https://discuss.elastic.co/u/jakommo)
#### Post date: [December 20, 2016, 1:15pm UTC](https://discuss.elastic.co/t/why-i-got-different-result-from-these-two-dsl/69557/2 "2016-12-20T13:15:10Z")

</div>

Hi @moonnap,

from the documentation "A single-value metrics aggregation that calculates an approximate count of distinct values".  
You can find more information in [this paragraph of the documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html#_counts_are_approximate) and on [this page](https://www.elastic.co/guide/en/elasticsearch/guide/current/cardinality.html).  
Might be worth playing with the [precision\_threshold setting](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html#_precision_control).

Hope this helps.  
Jakob

---

<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: [January 17, 2017, 1:15pm UTC](https://discuss.elastic.co/t/why-i-got-different-result-from-these-two-dsl/69557/3 "2017-01-17T13:15:17Z")

</div>

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