# Getting max value of a long field without losing precision

**URL:** <https://discuss.elastic.co/t/getting-max-value-of-a-long-field-without-losing-precision/92574>\
**Category:** Elasticsearch\
**Created:** [July 11, 2017, 5:41am UTC](https://discuss.elastic.co/t/getting-max-value-of-a-long-field-without-losing-precision/92574 "2017-07-11T05:41:12Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [July 13, 2017, 12:19am UTC](https://discuss.elastic.co/t/getting-max-value-of-a-long-field-without-losing-precision/92574/2 "2017-07-13T00:19:24Z")

</div>

Yeah, this is unfortunately a hard limitation on the agg framework right now. All aggs convert the values to a double before operating on them. I'm not sure if that'll change, at least easily... it's a pretty fundamental part of how the agg framework operates.

Before anything else, I think it should be noted that losing precision _usually_ isn't a problem for IDs and the like. 64bit doubles have 52 bit mantissa's, so the maximum non-float numeric you can store is 2^53. [See this SO answer](https://stackoverflow.com/a/1848762) for a breakdown on why. So that gives you values 0-9 quadrillion (roughly) that can be stored without loss of precision. For most folks that's fine, but if you expect to have a max \> 2^53 then it's a concern.

As a workaround, you could do a search with `size:1` and sort by the value descending, which will give you the document with the highest value, and you can extract it from the source. Not a great solution, since it only really applies to min/max and not the other aggs, but it may help.

We've stayed away from BigDecimal so far because it's just really, really slow. The overhead of using it is just a no-go for performant aggs. If/when we want support for Longs (or other non-floats) I think we'd probably just integrate it as a framework feature... defaulting to non-float when applicable (min/max/sum is fine, avg never, etc)

---

_[View the full topic](https://discuss.elastic.co/t/getting-max-value-of-a-long-field-without-losing-precision/92574)._
