# BigDecimal Result in scripted\_metric aggregation is returned as string

**URL:** <https://discuss.elastic.co/t/bigdecimal-result-in-scripted-metric-aggregation-is-returned-as-string/55875>\
**Category:** Elasticsearch\
**Created:** [July 19, 2016, 12:29pm UTC](https://discuss.elastic.co/t/bigdecimal-result-in-scripted-metric-aggregation-is-returned-as-string/55875 "2016-07-19T12:29:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hexer338](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hexer338/32/47413_2.png) [@hexer338](https://discuss.elastic.co/u/hexer338)\
**Post date:** [July 19, 2016, 12:29pm UTC](https://discuss.elastic.co/t/bigdecimal-result-in-scripted-metric-aggregation-is-returned-as-string/55875/1 "2016-07-19T12:29:07Z")

</div>

Hi,

When result of scripted\_metric aggregation is instance of BigDecimal, the returned value from the aggregation is stringified Number. Whereas if the result of scripted\_metric is Integer/Double , the returned value is Number.

#### Case 1:

**Elasticsearch Query**

```auto
{
    "size": 0,
    "aggs": {
        "metric": {
            "scripted_metric": {
                "map_script": "",
                "reduce_script": "34.23"
            }
        }
    }
}

```

**Elasticsearch Result**

```auto
{
   "took": 11,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 19496,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "metric": {
         "value": 34.23
      }
   }
}

```

#### Case 2:

**Elasticsearch Query**

```auto
{
    "size": 0,
    "aggs": {
        "metric": {
            "scripted_metric": {
                "map_script": "",
                "reduce_script": "34/23"
            }
        }
    }
}

```

**Elasticsearch Result**

```auto
{
   "took": 16,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 19496,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "metric": {
         "value": "1.4782608696"
      }
   }
}

```

In case1, _metric.value_ is 34.23 which is a number and in case2, _metric.value_ is "1.4782608696".

**Can someone explain why this happens ?**

---

<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 19, 2016, 2:02pm UTC](https://discuss.elastic.co/t/bigdecimal-result-in-scripted-metric-aggregation-is-returned-as-string/55875/2 "2016-07-19T14:02:29Z")

</div>

It's because Groovy defaults to using BigDecimal for all math operations involving floating points. And to prevent loss of precision, it encodes the BigDecimal as a string (since, theoretically, it could have much more precision than a double).

That said... don't use BigDecimal if you can help it. They are slow, and have plenty of their own edgecases. It's pretty silly that Groovy chose to make them default...

If you force the value to be a Double you should be better off:

```auto
{
    "size": 0,
    "aggs": {
        "metric": {
            "scripted_metric": {
                "map_script": "",
                "reduce_script": "34/23D"
            }
        }
    }
}

```

---

<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, 10:34pm UTC](https://discuss.elastic.co/t/bigdecimal-result-in-scripted-metric-aggregation-is-returned-as-string/55875/3 "2017-07-05T22:34:23Z")

</div>


