# Script aggregation yields wrong (but close) answer

**URL:** <https://discuss.elastic.co/t/script-aggregation-yields-wrong-but-close-answer/133744>\
**Category:** Elasticsearch\
**Created:** [May 29, 2018, 7:33pm UTC](https://discuss.elastic.co/t/script-aggregation-yields-wrong-but-close-answer/133744 "2018-05-29T19:33:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rschwieb](https://avatars.discourse-cdn.com/v4/letter/r/e47c2d/32.png) [@rschwieb](https://discuss.elastic.co/u/rschwieb)\
**Post date:** [May 29, 2018, 7:33pm UTC](https://discuss.elastic.co/t/script-aggregation-yields-wrong-but-close-answer/133744/1 "2018-05-29T19:33:53Z")

</div>

I put a single item into an index with a `start` field of value 1527619504.71632. Then I ran this search with an script in the aggregation:

```
{"query": {"match_all": {}},
 "aggs": {"total": {"sum": {"script": {"source": "doc['start'].value"}}}}}

```

The aggregation result is 1527619456. It looks like possibly it could be a problem of the type being cast to something other than float.

In the elasticsearch documentation, i ran across references (two of them) to "value\_type," one in the changelog and one in the documentation which says "For those cases, it is possible to give Elasticsearch a hint using the value\_type option". However, after going over it several times I cannot figure out where the value\_type option is supposed to be used. I tried adding "value\_type":"double" . at various levels in the query above but it either does nothing or breaks.

At any rate I'd like to suggest more explicit documentation for the `value_type` feature.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [May 30, 2018, 12:32pm UTC](https://discuss.elastic.co/t/script-aggregation-yields-wrong-but-close-answer/133744/2 "2018-05-30T12:32:07Z")

</div>

The problem is not directly related to scripting or `value_type`. If you run a regular `sum` aggregation on the `start` field you'll get the same result:

```auto
GET my_index/_search
{
  "aggs": {
    "total": {
      "sum": {
        "field": "start"
      }
    }
  }
}

```

The problem is that mapping this field as type `float` is not precise enough for a number like 1527619504.71632. If you map the `start` field as type `double` instead, you will get a precise answer:

```auto
PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "start": {
          "type": "double"
        }
      }
    }
  }
}

PUT my_index/_doc/1
{
  "start": 1527619504.71632
}

GET my_index/_search
{
  "aggs": {
    "total": {
      "sum": {
        "field": "start"
      }
    }
  }
}

GET my_index/_search
{
  "aggs": {
    "total": {
      "sum": {
        "script": {
          "source": "doc['start'].value"
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![rschwieb](https://avatars.discourse-cdn.com/v4/letter/r/e47c2d/32.png) [@rschwieb](https://discuss.elastic.co/u/rschwieb)\
**Post date:** [May 30, 2018, 1:38pm UTC](https://discuss.elastic.co/t/script-aggregation-yields-wrong-but-close-answer/133744/3 "2018-05-30T13:38:15Z")

</div>

Thank you! I thought it would be something like that. Your solution has definitely fixed my problem. Have a great day!

---

<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:** [June 27, 2018, 1:38pm UTC](https://discuss.elastic.co/t/script-aggregation-yields-wrong-but-close-answer/133744/4 "2018-06-27T13:38:18Z")

</div>

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