# Terms Stats Question/Problem

**URL:** <https://discuss.elastic.co/t/terms-stats-question-problem/5571>\
**Category:** Elasticsearch\
**Created:** [October 12, 2011, 4:31am UTC](https://discuss.elastic.co/t/terms-stats-question-problem/5571 "2011-10-12T04:31:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![danoyoung](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@danoyoung](https://discuss.elastic.co/u/danoyoung)\
**Post date:** [October 12, 2011, 4:31am UTC](https://discuss.elastic.co/t/terms-stats-question-problem/5571/1 "2011-10-12T04:31:10Z")

</div>

I'm trying to run a term\_stats facet to calculate some values with a  
grouping, but am curious why the values returned don't add to what I  
expect....I might be missing something on how the term\_stats is suppose to  
work.

Here's my query:

curl -s -XGET '[http://localhost:9200/my\_index/my\_type/\_search?pretty=true](http://localhost:9200/my_index/my_type/_search?pretty=true)'  
-d '  
{  
"query": {  
"filtered": {  
"query": {  
"bool" : {  
"must" : { "field" : { "campaign\_group\_id" : 187 } },  
"must" : { "field" : { "optimizer\_id" : 79 } },  
"must" : { "field" : { "keyword\_id" : 638489 } }  
}  
},  
"filter": {  
"numeric\_range": { "dw\_date\_marker\_id": { "from": 1022, "to":  
1022,"include\_lower" : true,"include\_upper":true } }  
}  
}  
},  
"facets" : {  
"actual\_click\_cost\_last\_24\_hours\_stats" : {  
"terms\_stats" : {  
"key\_field" : "keyword\_id",  
"value\_field" : "actual\_click\_cost\_last\_24\_hours"  
}  
}  
}  
}'

I have two records that meet the filter criteria. The two values for  
the actual\_click\_cost\_last\_24\_hours in the documents are as follows:

doc1:  
actual\_click\_cost\_last\_24\_hours: 2.54

doc2:  
actual\_click\_cost\_last\_24\_hours: 1.75

When I run the query, these are the results of the facet:  
.....  
.....  
"facets" : {  
"actual\_click\_cost\_last\_24\_hours\_stats" : {  
"\_type" : "terms\_stats",  
"missing" : 0,  
"terms" : [ {  
"term" : 638489,  
"count" : 2,  
"total\_count" : 2,  
"min" : 1.0,  
"max" : 2.0,  
"total" : 3.0,  
"mean" : 1.5  
} ]  
}  
}  
}

Why wouldn't the total be 2.54+1.75=4.29 vs 3.0? it seems that the .54 and  
.75 are being lost.....how can if prevent the rounding?

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![danoyoung](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@danoyoung](https://discuss.elastic.co/u/danoyoung)\
**Post date:** [October 12, 2011, 4:39am UTC](https://discuss.elastic.co/t/terms-stats-question-problem/5571/2 "2011-10-12T04:39:32Z")

</div>

Looks like if I use the \_source field the values appear to be correct  
now:

curl -s -XGET '[http://localhost:9200/my\_index/my\_type/\_search](http://localhost:9200/my_index/my_type/_search)?  
pretty=true' -d '  
{  
"query": {  
"filtered": {  
"query": {  
"bool" : {  
"must" : { "field" : { "campaign\_group\_id" : 187 } },  
"must" : { "field" : { "optimizer\_id" : 79 } },  
"must" : { "field" : { "keyword\_id" : 638489 } }  
}  
},  
"filter": {  
"numeric\_range": { "dw\_date\_marker\_id": { "from": 1022, "to":  
1022,"include\_lower" : true,"include\_upper":true } }  
}  
}  
},  
"facets" : {  
"actual\_click\_cost\_last\_24\_hours\_stats" : {  
"terms\_stats" : {  
"key\_field" : "keyword\_id",  
"value\_script" :  
"\_source.actual\_click\_cost\_last\_24\_hours"  
}  
}  
}  
}'

results:

"facets" : {  
"actual\_click\_cost\_last\_24\_hours\_stats" : {  
"\_type" : "terms\_stats",  
"missing" : 0,  
"terms" : [ {  
"term" : 638489,  
"count" : 2,  
"total\_count" : 2,  
"min" : 1.75,  
"max" : 2.54,  
"total" : 4.29,  
"mean" : 2.145

On Oct 11, 10:31 pm, Dan Young [danoyo...@gmail.com](mailto:danoyo...@gmail.com) wrote:

> I'm trying to run a term\_stats facet to calculate some values with a  
> grouping, but am curious why the values returned don't add to what I  
> expect....I might be missing something on how the term\_stats is suppose to  
> work.
> 
> Here's my query:
> 
> curl -s -XGET '[http://localhost:9200/my\_index/my\_type/\_search?pretty=true](http://localhost:9200/my_index/my_type/_search?pretty=true)'  
> -d '  
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "bool" : {  
> "must" : { "field" : { "campaign\_group\_id" : 187 } },  
> "must" : { "field" : { "optimizer\_id" : 79 } },  
> "must" : { "field" : { "keyword\_id" : 638489 } }  
> }  
> },  
> "filter": {  
> "numeric\_range": { "dw\_date\_marker\_id": { "from": 1022, "to":  
> 1022,"include\_lower" : true,"include\_upper":true } }}  
> }
> 
> },  
> "facets" : {  
> "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> "terms\_stats" : {  
> "key\_field" : "keyword\_id",  
> "value\_field" : "actual\_click\_cost\_last\_24\_hours"  
> }  
> }  
> }
> 
> }'
> 
> I have two records that meet the filter criteria. The two values for  
> the actual\_click\_cost\_last\_24\_hours in the documents are as follows:
> 
> doc1:  
> actual\_click\_cost\_last\_24\_hours: 2.54
> 
> doc2:  
> actual\_click\_cost\_last\_24\_hours: 1.75
> 
> When I run the query, these are the results of the facet:  
> .....  
> .....  
> "facets" : {  
> "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> "\_type" : "terms\_stats",  
> "missing" : 0,  
> "terms" : [ {  
> "term" : 638489,  
> "count" : 2,  
> "total\_count" : 2,  
> "min" : 1.0,  
> "max" : 2.0,  
> "total" : 3.0,  
> "mean" : 1.5  
> } ]  
> }  
> }
> 
> }
> 
> Why wouldn't the total be 2.54+1.75=4.29 vs 3.0? it seems that the .54 and  
> .75 are being lost.....how can if prevent the rounding?
> 
> Regards,
> 
> Dan

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [October 12, 2011, 9:14pm UTC](https://discuss.elastic.co/t/terms-stats-question-problem/5571/3 "2011-10-12T21:14:21Z")

</div>

That should not really be the case, can you gist a recreation (including  
indexing sample data)? See [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/help).

On Wed, Oct 12, 2011 at 6:39 AM, Dan Young [danoyoung@gmail.com](mailto:danoyoung@gmail.com) wrote:

> Looks like if I use the \_source field the values appear to be correct  
> now:
> 
> curl -s -XGET '[http://localhost:9200/my\_index/my\_type/\_search](http://localhost:9200/my_index/my_type/_search)?  
> pretty=true' -d '  
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "bool" : {  
> "must" : { "field" : {  
> "campaign\_group\_id" : 187 } },  
> "must" : { "field" : {  
> "optimizer\_id" : 79 } },  
> "must" : { "field" : { "keyword\_id"  
> : 638489 } }  
> }  
> },  
> "filter": {  
> "numeric\_range": { "dw\_date\_marker\_id": { "from": 1022, "to":  
> 1022,"include\_lower" : true,"include\_upper":true } }  
> }  
> }  
> },  
> "facets" : {  
> "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> "terms\_stats" : {  
> "key\_field" : "keyword\_id",  
> "value\_script" :  
> "\_source.actual\_click\_cost\_last\_24\_hours"  
> }  
> }  
> }  
> }'
> 
> results:
> 
> "facets" : {  
> "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> "\_type" : "terms\_stats",  
> "missing" : 0,  
> "terms" : [ {  
> "term" : 638489,  
> "count" : 2,  
> "total\_count" : 2,  
> "min" : 1.75,  
> "max" : 2.54,  
> "total" : 4.29,  
> "mean" : 2.145
> 
> On Oct 11, 10:31 pm, Dan Young [danoyo...@gmail.com](mailto:danoyo...@gmail.com) wrote:
> 
> > I'm trying to run a term\_stats facet to calculate some values with a  
> > grouping, but am curious why the values returned don't add to what I  
> > expect....I might be missing something on how the term\_stats is suppose  
> > to  
> > work.
> > 
> > Here's my query:
> > 
> > curl -s -XGET '  
> > [http://localhost:9200/my\_index/my\_type/\_search?pretty=true](http://localhost:9200/my_index/my_type/_search?pretty=true)'  
> > -d '  
> > {  
> > "query": {  
> > "filtered": {  
> > "query": {  
> > "bool" : {  
> > "must" : { "field" : { "campaign\_group\_id" : 187 } },  
> > "must" : { "field" : { "optimizer\_id" : 79 } },  
> > "must" : { "field" : { "keyword\_id" : 638489 } }  
> > }  
> > },  
> > "filter": {  
> > "numeric\_range": { "dw\_date\_marker\_id": { "from": 1022, "to":  
> > 1022,"include\_lower" : true,"include\_upper":true } }}  
> > }
> > 
> > },  
> > "facets" : {  
> > "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> > "terms\_stats" : {  
> > "key\_field" : "keyword\_id",  
> > "value\_field" : "actual\_click\_cost\_last\_24\_hours"  
> > }  
> > }  
> > }
> > 
> > }'
> > 
> > I have two records that meet the filter criteria. The two values for  
> > the actual\_click\_cost\_last\_24\_hours in the documents are as follows:
> > 
> > doc1:  
> > actual\_click\_cost\_last\_24\_hours: 2.54
> > 
> > doc2:  
> > actual\_click\_cost\_last\_24\_hours: 1.75
> > 
> > When I run the query, these are the results of the facet:  
> > .....  
> > .....  
> > "facets" : {  
> > "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> > "\_type" : "terms\_stats",  
> > "missing" : 0,  
> > "terms" : [ {  
> > "term" : 638489,  
> > "count" : 2,  
> > "total\_count" : 2,  
> > "min" : 1.0,  
> > "max" : 2.0,  
> > "total" : 3.0,  
> > "mean" : 1.5  
> > } ]  
> > }  
> > }
> > 
> > }
> > 
> > Why wouldn't the total be 2.54+1.75=4.29 vs 3.0? it seems that the .54  
> > and  
> > .75 are being lost.....how can if prevent the rounding?
> > 
> > Regards,
> > 
> > Dan

---

<div class="post-metadata">

**Author:** ![danoyoung](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@danoyoung](https://discuss.elastic.co/u/danoyoung)\
**Post date:** [October 13, 2011, 2:55am UTC](https://discuss.elastic.co/t/terms-stats-question-problem/5571/4 "2011-10-13T02:55:48Z")

</div>

SHay,

Here's a gist.....Let me know if you need anything else. Thank you  
for such a great product/project!

> <https://gist.github.com/danoyoung/0ea80c38e7ebe89b3314>

As noted, if I reference \_source, the results come back correctly.

Regards,

Dan

On Oct 12, 3:14 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> That should not really be the case, can you gist a recreation (including  
> indexing sample data)? Seehttp://www.elasticsearch.org/help.
> 
> On Wed, Oct 12, 2011 at 6:39 AM, Dan Young [danoyo...@gmail.com](mailto:danoyo...@gmail.com) wrote:
> 
> > Looks like if I use the \_source field the values appear to be correct  
> > now:
> 
> > curl -s -XGET '[http://localhost:9200/my\_index/my\_type/\_search](http://localhost:9200/my_index/my_type/_search)?  
> > pretty=true' -d '  
> > {  
> > "query": {  
> > "filtered": {  
> > "query": {  
> > "bool" : {  
> > "must" : { "field" : {  
> > "campaign\_group\_id" : 187 } },  
> > "must" : { "field" : {  
> > "optimizer\_id" : 79 } },  
> > "must" : { "field" : { "keyword\_id"  
> > : 638489 } }  
> > }  
> > },  
> > "filter": {  
> > "numeric\_range": { "dw\_date\_marker\_id": { "from": 1022, "to":  
> > 1022,"include\_lower" : true,"include\_upper":true } }  
> > }  
> > }  
> > },  
> > "facets" : {  
> > "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> > "terms\_stats" : {  
> > "key\_field" : "keyword\_id",  
> > "value\_script" :  
> > "\_source.actual\_click\_cost\_last\_24\_hours"  
> > }  
> > }  
> > }  
> > }'
> 
> > results:
> 
> > "facets" : {  
> > "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> > "\_type" : "terms\_stats",  
> > "missing" : 0,  
> > "terms" : [ {  
> > "term" : 638489,  
> > "count" : 2,  
> > "total\_count" : 2,  
> > "min" : 1.75,  
> > "max" : 2.54,  
> > "total" : 4.29,  
> > "mean" : 2.145
> 
> > On Oct 11, 10:31 pm, Dan Young [danoyo...@gmail.com](mailto:danoyo...@gmail.com) wrote:
> > 
> > > I'm trying to run a term\_stats facet to calculate some values with a  
> > > grouping, but am curious why the values returned don't add to what I  
> > > expect....I might be missing something on how the term\_stats is suppose  
> > > to  
> > > work.
> 
> > > Here's my query:
> 
> > > curl -s -XGET '  
> > > [http://localhost:9200/my\_index/my\_type/\_search?pretty=true](http://localhost:9200/my_index/my_type/_search?pretty=true)'  
> > > -d '  
> > > {  
> > > "query": {  
> > > "filtered": {  
> > > "query": {  
> > > "bool" : {  
> > > "must" : { "field" : { "campaign\_group\_id" : 187 } },  
> > > "must" : { "field" : { "optimizer\_id" : 79 } },  
> > > "must" : { "field" : { "keyword\_id" : 638489 } }  
> > > }  
> > > },  
> > > "filter": {  
> > > "numeric\_range": { "dw\_date\_marker\_id": { "from": 1022, "to":  
> > > 1022,"include\_lower" : true,"include\_upper":true } }}  
> > > }
> 
> > > },  
> > > "facets" : {  
> > > "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> > > "terms\_stats" : {  
> > > "key\_field" : "keyword\_id",  
> > > "value\_field" : "actual\_click\_cost\_last\_24\_hours"  
> > > }  
> > > }  
> > > }
> 
> > > }'
> 
> > > I have two records that meet the filter criteria. The two values for  
> > > the actual\_click\_cost\_last\_24\_hours in the documents are as follows:
> 
> > > doc1:  
> > > actual\_click\_cost\_last\_24\_hours: 2.54
> 
> > > doc2:  
> > > actual\_click\_cost\_last\_24\_hours: 1.75
> 
> > > When I run the query, these are the results of the facet:  
> > > .....  
> > > .....  
> > > "facets" : {  
> > > "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> > > "\_type" : "terms\_stats",  
> > > "missing" : 0,  
> > > "terms" : [ {  
> > > "term" : 638489,  
> > > "count" : 2,  
> > > "total\_count" : 2,  
> > > "min" : 1.0,  
> > > "max" : 2.0,  
> > > "total" : 3.0,  
> > > "mean" : 1.5  
> > > } ]  
> > > }  
> > > }
> 
> > > }
> 
> > > Why wouldn't the total be 2.54+1.75=4.29 vs 3.0? it seems that the .54  
> > > and  
> > > .75 are being lost.....how can if prevent the rounding?
> 
> > > Regards,
> 
> > > Dan

---

<div class="post-metadata">

**Author:** ![danoyoung](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@danoyoung](https://discuss.elastic.co/u/danoyoung)\
**Post date:** [October 13, 2011, 3:24am UTC](https://discuss.elastic.co/t/terms-stats-question-problem/5571/5 "2011-10-13T03:24:13Z")

</div>

Ok Shay,

Something weird....possibly on my side. I deleted my index, recreated  
it and the mapping, reindexed the data and now the query works and  
returns the correct results.

curl -s -XGET '[http://localhost:9200/daily\_ad\_network\_keywords\_2011/](http://localhost:9200/daily_ad_network_keywords_2011/)  
daily\_ad\_network\_keyword/\_search?pretty=true' -d '  
{  
"query": {  
"filtered": {  
"query": {  
"bool" : {  
"must" : { "field" : { "campaign\_group\_id" : 187 } },  
"must" : { "field" : { "optimizer\_id" : 79 } },  
"must" : { "field" : { "keyword\_id" : 638489 } }  
}  
},  
"filter": {  
"numeric\_range": { "dw\_date\_marker\_id": { "from": 1022, "to":  
1022,"include\_lower" : true,"include\_upper":true } }  
}  
}  
},  
"facets" : {  
"actual\_click\_cost\_last\_24\_hours\_stats" : {  
"terms\_stats" : {  
"key\_field" : "keyword\_id",  
"value\_field" : "actual\_click\_cost\_last\_24\_hours"  
}  
}  
}  
}'

Results:

.....  
.....  
"facets" : {  
"actual\_click\_cost\_last\_24\_hours\_stats" : {  
"\_type" : "terms\_stats",  
"missing" : 0,  
"terms" : [ {  
"term" : 638489,  
"count" : 2,  
"total\_count" : 2,  
"min" : 1.75,  
"max" : 2.54,  
"total" : 4.29,  
"mean" : 2.145  
} ]  
}  
}  
}

This is weird....I'm using the bulk API via the REST interface, where  
I put the message on rabbitmq and then have the messages consumed via  
the rabbitmq river. I'm going to do some additional testing....but  
this is strange...

On Oct 12, 3:14 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> That should not really be the case, can you gist a recreation (including  
> indexing sample data)? Seehttp://www.elasticsearch.org/help.
> 
> On Wed, Oct 12, 2011 at 6:39 AM, Dan Young [danoyo...@gmail.com](mailto:danoyo...@gmail.com) wrote:
> 
> > Looks like if I use the \_source field the values appear to be correct  
> > now:
> 
> > curl -s -XGET '[http://localhost:9200/my\_index/my\_type/\_search](http://localhost:9200/my_index/my_type/_search)?  
> > pretty=true' -d '  
> > {  
> > "query": {  
> > "filtered": {  
> > "query": {  
> > "bool" : {  
> > "must" : { "field" : {  
> > "campaign\_group\_id" : 187 } },  
> > "must" : { "field" : {  
> > "optimizer\_id" : 79 } },  
> > "must" : { "field" : { "keyword\_id"  
> > : 638489 } }  
> > }  
> > },  
> > "filter": {  
> > "numeric\_range": { "dw\_date\_marker\_id": { "from": 1022, "to":  
> > 1022,"include\_lower" : true,"include\_upper":true } }  
> > }  
> > }  
> > },  
> > "facets" : {  
> > "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> > "terms\_stats" : {  
> > "key\_field" : "keyword\_id",  
> > "value\_script" :  
> > "\_source.actual\_click\_cost\_last\_24\_hours"  
> > }  
> > }  
> > }  
> > }'
> 
> > results:
> 
> > "facets" : {  
> > "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> > "\_type" : "terms\_stats",  
> > "missing" : 0,  
> > "terms" : [ {  
> > "term" : 638489,  
> > "count" : 2,  
> > "total\_count" : 2,  
> > "min" : 1.75,  
> > "max" : 2.54,  
> > "total" : 4.29,  
> > "mean" : 2.145
> 
> > On Oct 11, 10:31 pm, Dan Young [danoyo...@gmail.com](mailto:danoyo...@gmail.com) wrote:
> > 
> > > I'm trying to run a term\_stats facet to calculate some values with a  
> > > grouping, but am curious why the values returned don't add to what I  
> > > expect....I might be missing something on how the term\_stats is suppose  
> > > to  
> > > work.
> 
> > > Here's my query:
> 
> > > curl -s -XGET '  
> > > [http://localhost:9200/my\_index/my\_type/\_search?pretty=true](http://localhost:9200/my_index/my_type/_search?pretty=true)'  
> > > -d '  
> > > {  
> > > "query": {  
> > > "filtered": {  
> > > "query": {  
> > > "bool" : {  
> > > "must" : { "field" : { "campaign\_group\_id" : 187 } },  
> > > "must" : { "field" : { "optimizer\_id" : 79 } },  
> > > "must" : { "field" : { "keyword\_id" : 638489 } }  
> > > }  
> > > },  
> > > "filter": {  
> > > "numeric\_range": { "dw\_date\_marker\_id": { "from": 1022, "to":  
> > > 1022,"include\_lower" : true,"include\_upper":true } }}  
> > > }
> 
> > > },  
> > > "facets" : {  
> > > "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> > > "terms\_stats" : {  
> > > "key\_field" : "keyword\_id",  
> > > "value\_field" : "actual\_click\_cost\_last\_24\_hours"  
> > > }  
> > > }  
> > > }
> 
> > > }'
> 
> > > I have two records that meet the filter criteria. The two values for  
> > > the actual\_click\_cost\_last\_24\_hours in the documents are as follows:
> 
> > > doc1:  
> > > actual\_click\_cost\_last\_24\_hours: 2.54
> 
> > > doc2:  
> > > actual\_click\_cost\_last\_24\_hours: 1.75
> 
> > > When I run the query, these are the results of the facet:  
> > > .....  
> > > .....  
> > > "facets" : {  
> > > "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> > > "\_type" : "terms\_stats",  
> > > "missing" : 0,  
> > > "terms" : [ {  
> > > "term" : 638489,  
> > > "count" : 2,  
> > > "total\_count" : 2,  
> > > "min" : 1.0,  
> > > "max" : 2.0,  
> > > "total" : 3.0,  
> > > "mean" : 1.5  
> > > } ]  
> > > }  
> > > }
> 
> > > }
> 
> > > Why wouldn't the total be 2.54+1.75=4.29 vs 3.0? it seems that the .54  
> > > and  
> > > .75 are being lost.....how can if prevent the rounding?
> 
> > > Regards,
> 
> > > Dan

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [October 14, 2011, 12:02pm UTC](https://discuss.elastic.co/t/terms-stats-question-problem/5571/6 "2011-10-14T12:02:29Z")

</div>

Strange..., well, ping if it happens again. A recreation would help with  
actual curl requests for what you do, even for the mapping / indexing hte  
data.

On Thu, Oct 13, 2011 at 5:24 AM, Dan Young [danoyoung@gmail.com](mailto:danoyoung@gmail.com) wrote:

> Ok Shay,
> 
> Something weird....possibly on my side. I deleted my index, recreated  
> it and the mapping, reindexed the data and now the query works and  
> returns the correct results.
> 
> curl -s -XGET '[http://localhost:9200/daily\_ad\_network\_keywords\_2011/](http://localhost:9200/daily_ad_network_keywords_2011/)  
> daily\_ad\_network\_keyword/\_search?pretty=true' -d '  
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "bool" : {  
> "must" : { "field" : {  
> "campaign\_group\_id" : 187 } },  
> "must" : { "field" : {  
> "optimizer\_id" : 79 } },  
> "must" : { "field" : { "keyword\_id"  
> : 638489 } }  
> }  
> },  
> "filter": {  
> "numeric\_range": { "dw\_date\_marker\_id": { "from": 1022, "to":  
> 1022,"include\_lower" : true,"include\_upper":true } }  
> }  
> }  
> },  
> "facets" : {  
> "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> "terms\_stats" : {  
> "key\_field" : "keyword\_id",  
> "value\_field" : "actual\_click\_cost\_last\_24\_hours"  
> }  
> }  
> }  
> }'
> 
> Results:
> 
> .....  
> .....  
> "facets" : {  
> "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> "\_type" : "terms\_stats",  
> "missing" : 0,  
> "terms" : [ {  
> "term" : 638489,  
> "count" : 2,  
> "total\_count" : 2,  
> "min" : 1.75,  
> "max" : 2.54,  
> "total" : 4.29,  
> "mean" : 2.145  
> } ]  
> }  
> }  
> }
> 
> This is weird....I'm using the bulk API via the REST interface, where  
> I put the message on rabbitmq and then have the messages consumed via  
> the rabbitmq river. I'm going to do some additional testing....but  
> this is strange...
> 
> On Oct 12, 3:14 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:
> 
> > That should not really be the case, can you gist a recreation (including  
> > indexing sample data)? Seehttp://www.elasticsearch.org/help.
> > 
> > On Wed, Oct 12, 2011 at 6:39 AM, Dan Young [danoyo...@gmail.com](mailto:danoyo...@gmail.com) wrote:
> > 
> > > Looks like if I use the \_source field the values appear to be correct  
> > > now:
> > 
> > > curl -s -XGET '[http://localhost:9200/my\_index/my\_type/\_search](http://localhost:9200/my_index/my_type/_search)?  
> > > pretty=true' -d '  
> > > {  
> > > "query": {  
> > > "filtered": {  
> > > "query": {  
> > > "bool" : {  
> > > "must" : { "field" : {  
> > > "campaign\_group\_id" : 187 } },  
> > > "must" : { "field" : {  
> > > "optimizer\_id" : 79 } },  
> > > "must" : { "field" : {  
> > > "keyword\_id"  
> > > : 638489 } }  
> > > }  
> > > },  
> > > "filter": {  
> > > "numeric\_range": { "dw\_date\_marker\_id": { "from": 1022, "to":  
> > > 1022,"include\_lower" : true,"include\_upper":true } }  
> > > }  
> > > }  
> > > },  
> > > "facets" : {  
> > > "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> > > "terms\_stats" : {  
> > > "key\_field" : "keyword\_id",  
> > > "value\_script" :  
> > > "\_source.actual\_click\_cost\_last\_24\_hours"  
> > > }  
> > > }  
> > > }  
> > > }'
> > 
> > > results:
> > 
> > > "facets" : {  
> > > "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> > > "\_type" : "terms\_stats",  
> > > "missing" : 0,  
> > > "terms" : [ {  
> > > "term" : 638489,  
> > > "count" : 2,  
> > > "total\_count" : 2,  
> > > "min" : 1.75,  
> > > "max" : 2.54,  
> > > "total" : 4.29,  
> > > "mean" : 2.145
> > 
> > > On Oct 11, 10:31 pm, Dan Young [danoyo...@gmail.com](mailto:danoyo...@gmail.com) wrote:
> > > 
> > > > I'm trying to run a term\_stats facet to calculate some values with a  
> > > > grouping, but am curious why the values returned don't add to what I  
> > > > expect....I might be missing something on how the term\_stats is  
> > > > suppose  
> > > > to  
> > > > work.
> > 
> > > > Here's my query:
> > 
> > > > curl -s -XGET '  
> > > > [http://localhost:9200/my\_index/my\_type/\_search?pretty=true](http://localhost:9200/my_index/my_type/_search?pretty=true)'  
> > > > -d '  
> > > > {  
> > > > "query": {  
> > > > "filtered": {  
> > > > "query": {  
> > > > "bool" : {  
> > > > "must" : { "field" : { "campaign\_group\_id" : 187 } },  
> > > > "must" : { "field" : { "optimizer\_id" : 79 } },  
> > > > "must" : { "field" : { "keyword\_id" : 638489 } }  
> > > > }  
> > > > },  
> > > > "filter": {  
> > > > "numeric\_range": { "dw\_date\_marker\_id": { "from": 1022, "to":  
> > > > 1022,"include\_lower" : true,"include\_upper":true } }}  
> > > > }
> > 
> > > > },  
> > > > "facets" : {  
> > > > "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> > > > "terms\_stats" : {  
> > > > "key\_field" : "keyword\_id",  
> > > > "value\_field" : "actual\_click\_cost\_last\_24\_hours"  
> > > > }  
> > > > }  
> > > > }
> > 
> > > > }'
> > 
> > > > I have two records that meet the filter criteria. The two values for  
> > > > the actual\_click\_cost\_last\_24\_hours in the documents are as follows:
> > 
> > > > doc1:  
> > > > actual\_click\_cost\_last\_24\_hours: 2.54
> > 
> > > > doc2:  
> > > > actual\_click\_cost\_last\_24\_hours: 1.75
> > 
> > > > When I run the query, these are the results of the facet:  
> > > > .....  
> > > > .....  
> > > > "facets" : {  
> > > > "actual\_click\_cost\_last\_24\_hours\_stats" : {  
> > > > "\_type" : "terms\_stats",  
> > > > "missing" : 0,  
> > > > "terms" : [ {  
> > > > "term" : 638489,  
> > > > "count" : 2,  
> > > > "total\_count" : 2,  
> > > > "min" : 1.0,  
> > > > "max" : 2.0,  
> > > > "total" : 3.0,  
> > > > "mean" : 1.5  
> > > > } ]  
> > > > }  
> > > > }
> > 
> > > > }
> > 
> > > > Why wouldn't the total be 2.54+1.75=4.29 vs 3.0? it seems that the  
> > > > .54  
> > > > and  
> > > > .75 are being lost.....how can if prevent the rounding?
> > 
> > > > Regards,
> > 
> > > > Dan

---

<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 6, 2017, 3:51am UTC](https://discuss.elastic.co/t/terms-stats-question-problem/5571/7 "2017-07-06T03:51:58Z")

</div>


