Script aggregation yields wrong (but close) answer

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.

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:

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:

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"
        }
      }
    }
  }
}
1 Like

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

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