The sum of my Elastic i coming as a whole number and that field had values that had decimals

in my elastic Cost field has decimals but when doing aggregation of it , its coming as whole number, am expecting the aggregation sum to come as decimals also

here is my query

        aggregation: { "dateAggs": { "terms": { "field": "Date", "format": "MMM YYYY", "order": { "_key": "asc" } }, "aggs": { "costSum": { "sum": { "field": "Cost" } } } } },```

Hi there @Collins_Mbathi, welcome to the Elastic community and thanks for posting!

Could you please provide a little more information - perhaps the mappings in your index and more information on the data you've indexed, more information on the query you're running, and what you're getting back?

I put the snippet you provided into a simple test indexing using Elasticsearch 8.14.1:

PUT /test
{
  "mappings": {
    "properties": {
      "Date": {
        "type": "date"
      },
      "Cost": {
        "type": "float"
      }
    }
  }
}


POST test/_doc/
{
  "Date": "2024-06-01",
  "Cost": 12.34
}
POST test/_doc
{
  "Date": "2024-07-02",
  "Cost": 56.78
}

GET test/_search
{
  "size": 0,
  "aggs": {
    "dateAggs": {
      "terms": {
        "field": "Date",
        "format": "MMM YYYY",
        "order": {
          "_key": "asc"
        }
      },
      "aggs": {
        "costSum": {
          "sum": {
            "field": "Cost"
          }
        }
      }
    }
  }
}

Using this script, I see that costSum is returning floating point values.

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "dateAggs": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 1717200000000,
          "key_as_string": "Jun 2024",
          "doc_count": 1,
          "costSum": {
            "value": 12.34000015258789
          }
        },
        {
          "key": 1719878400000,
          "key_as_string": "Jul 2024",
          "doc_count": 1,
          "costSum": {
            "value": 56.779998779296875
          }
        }
      ]
    }
  }
}

More information about what you're seeing vs. what you expect, and how to reproduce it, will be helpful for us to help you.

Thanks!

agreed it would be helpful to see more information here.

Terminology is a bit wrong, you with "decimals" when I think you mean a floating point numbers. So one thing to be sure of is your mapping for the cost field.

Also, precisely where are you seeing the aggrgation "coming as whole number", in some other tool/UI, or within the actual json response?

Please post the actual aggregation query and the actual response from elasticsearch.

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