# Does the term aggregation also return approximate value when doing sum/avg aggregation?

**URL:** <https://discuss.elastic.co/t/does-the-term-aggregation-also-return-approximate-value-when-doing-sum-avg-aggregation/169317>\
**Category:** Elasticsearch\
**Created:** [February 21, 2019, 3:22am UTC](https://discuss.elastic.co/t/does-the-term-aggregation-also-return-approximate-value-when-doing-sum-avg-aggregation/169317 "2019-02-21T03:22:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kramer\_Li](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kramer_li/32/46872_2.png) [@Kramer\_Li](https://discuss.elastic.co/u/Kramer_Li)\
**Post date:** [February 21, 2019, 3:22am UTC](https://discuss.elastic.co/t/does-the-term-aggregation-also-return-approximate-value-when-doing-sum-avg-aggregation/169317/1 "2019-02-21T03:22:22Z")

</div>

I understand that term aggregation will return approximate result regarding the doc count.  
But how about the metric aggregation after term aggregation? For example, sum/avg.  
Will these also be approximate too?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [February 21, 2019, 10:06am UTC](https://discuss.elastic.co/t/does-the-term-aggregation-also-return-approximate-value-when-doing-sum-avg-aggregation/169317/2 "2019-02-21T10:06:46Z")

</div>

When the doc count is approximate (because of high-cardinality fields on distributed data) then any child aggregations (sum, avg etc) will be looking at an incomplete set of docs and so the values will be inaccurate.  
To overcome this you can do an initial query to find the top terms and then run a second query with `terms` agg using `include` parameter that lists these values in an array and have the child aggs computing sum, avg etc. These values will be correct for those terms.

---

<div class="post-metadata">

**Author:** ![Kramer\_Li](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kramer_li/32/46872_2.png) [@Kramer\_Li](https://discuss.elastic.co/u/Kramer_Li)\
**Post date:** [February 21, 2019, 12:21pm UTC](https://discuss.elastic.co/t/does-the-term-aggregation-also-return-approximate-value-when-doing-sum-avg-aggregation/169317/3 "2019-02-21T12:21:41Z")

</div>

The doc also said using "shard\_size" will improve the accuracy, but did not giving any example for this parameter.  
Do you know how to use the shard\_size ？ Can you give some example please? Thanks

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [February 21, 2019, 3:11pm UTC](https://discuss.elastic.co/t/does-the-term-aggregation-also-return-approximate-value-when-doing-sum-avg-aggregation/169317/4 "2019-02-21T15:11:54Z")

</div>

Here's an example:

```
GET myindex/_search
{
  "size": 0,
  "aggs": {
	"my_agg": {
	  "terms": {
		"field": "my_field",
		"size": 10,
		"shard_size": 100
	  }
	}
  }
}

```

Pay attention to the `doc_count_error_upper_bound` in the results - when that's zero you know you're accurate.

---

<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:** [March 21, 2019, 3:11pm UTC](https://discuss.elastic.co/t/does-the-term-aggregation-also-return-approximate-value-when-doing-sum-avg-aggregation/169317/5 "2019-03-21T15:11:58Z")

</div>

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